1. Why use a set frame
When we don't know how many objects will be needed when the program runs, or need to store objects in a more complex way-you can use the Java Collection framework
2. What the Java Collection Framework contains
Interface: (parent Class) The collection interface contains the list (subclass) interface and the set (subclass) interface L
The IST interface is also included (ArrayList collection implementation class and LinkedList collection implementation class
The set interface is also included (HashSet collection implementation class and TreeSet collection implementation Class)
Interface: (parent Class) The Map interface contains (HashMap Collection implementation class and TreeMap collection implementation Class)
The *collections interface provides a variety of algorithms for sorting, traversing, and so on *java the set framework provides us with a set of well-performing, easy-to-use interfaces and classes that are located in the Java.util package
Collection interface stores a set of non-unique , unordered objects
The List interface stores a set of objects that are not unique , ordered (in order of insertion)
The set interface stores a set of unique , unordered object map interfaces that store a set of key-value objects that provide a key-to value mapping
Benefits of ArrayList collections and LinkedList collections
1. ArrayList implements a variable-length array that allocates contiguous space in memory. High efficiency for traversing elements and random access elements
2. LinkedList adopts the chain list storage mode. High efficiency when inserting and deleting elements
The list interface provides the appropriate method of remove (), contains (), which can be used directly
Common methods for IST interfaces:
A Boolean add (Object o) adds an element in the order of the end of the list, starting at the 0 start index position
void Add (int index,object o) adds an element at the specified index position. The index position must be between 0 and the number of elements in the list
int size () returns the number of elements in the list
Object get (int index) returns the element at the specified index position. The removed element is of type object and requires a forced type conversion before use
Boolean contains (Object O) determines whether the specified element exists in the list
Boolean remove (Object o) removes the element from the list
Object Remove (int index) removes the specified position element from the list, starting at index position 0
Special methods of LinkedList
void AddFirst (Object o) adds an element to the header of the list
void AddLast (Object o) adds an element at the end of the list
Object GetFirst () returns the first element in a list
Object GetLast () returns the last element in the list
Object Removefirst () deletes and returns the first element in a list
Object removelast () deletes and returns the last element in the list
common methods for map interfaces:
Object put (Object key, Object val) is stored in a "key-value pair" manner
Object get (Object key) returns the associated value according to the key, or null if the specified key does not exist
Object remove (object key) deletes the "key-value pair" that is mapped by the specified key
int size () returns the number of elements
Set KeySet () returns a collection of keys
Collection values () returns a collection of value
Boolean ContainsKey (Object key) returns True if there is a "key-value pair" that is mapped by the specified key
The similarities and differences between Vector and ArrayList:
The principle of implementation is the same, the function is the same, in many cases can be interoperable the main difference is as follows
Vector thread safety, ArrayList heavy speed, light security, thread non-secure length needs to grow, vector by default growth of one times, ArrayList growth of 50%
Similarities and differences of Hashtable and HashMap:
The same principle of implementation, the same function, in many cases can be interoperable
The main differences between the two are as follows
Hashtable inherits the Dictionary class, HashMap implements the map interface
Hashtable thread safety, HashMap thread non-secure
Hashtable null values are not allowed, HashMap allows null values
Iterator iterator
Method 1: Iterate through the For Loop and get () method mates
Method 2: Through the iterator iterator implementation traversal all the collection interfaces and classes do not provide a corresponding traversal method, but by the iterator implementation of the collection Traversal Collection Interface Iterate () method returns a iterator, Then through the iterator interface two methods can be implemented traversal
Boolean Hasnext (): Determine if there is another accessible element
Object Next (): Returns the next element to be accessed
Generic collection: Put any type of object through Add (object obj) into the list, it is considered that only the object type through get (int index) to remove elements in the list must be forced type conversion, cumbersome and prone to exception using the map put (Object Key, object value) and get (object key) have the same problem accessing the object using the next () method of iterator the same problem when getting elements
JDK5.0 is an effective way to solve this problem by introducing generics JDK5.0 using generics to overwrite all interfaces and classes in the collection framework
2, the collection framework to achieve the comparison, what interface to achieve?
Answer: Comparable/comparator
3. What is the difference between ArrayList and vectors?
Answer: These two classes all implement the list interface, is an ordered set, that is, the position of the elements in the collection is ordered, you can take an element by the position index number, and where the data is allowed to repeat, the equivalent of a dynamic array.
The difference between ArrayList and vectors consists of two main aspects:
(1) synchronization: Vector is thread-safe, that is, its methods are thread-synchronized, and ArrayList is a line program is not secure, its methods are not synchronized between the threads. If there is only one thread routines access to the collection, it is best to use ArrayList, because it is more efficient than thread-safe, and if there are multiple lines routines access to the collection, it is best to use vectors, since we do not need to consider and write thread-safe code ourselves.
(2) Data growth: ArrayList and vectors have an initial capacity size, when the number of elements stored in them exceeds the capacity, you need to increase the storage space of ArrayList and vector, each time to increase storage space, not only add a storage unit, Instead, add multiple storage units, each increasing the number of storage units in memory space utilization and program efficiency to achieve a certain balance. Vector growth is twice times the default, and ArrayList's growth strategy is not clearly defined in the documentation (from the source code to see the growth of 1.5 times times the original).
Both the ArrayList and vector can set the initial space size, and the vector can also set the amount of space to grow, while ArrayList does not provide a way to set the growth space.
4, the difference between HashMap and Hashtable?
Answer: HashMap is a lightweight implementation of Hashtable, all implemented the map interface. The main difference is that hashmap allows null keys and values, while Hashtable is not allowed. HashMap is non-thread-safe, and Hashtable is thread-safe. Therefore, in the case of only one thread access, the efficiency is higher than Hashtable. HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding. Hashtable inherits from the dictionary class, and HashMap inherits from the Abstractmap class, which is an implementation of the map interface introduced by Java1.2. The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.
5. What is the difference between List and Map?
Answer: One is a collection of single-column data, and the other is a collection of two columns of data that store key-value pairs, the data stored in the list is in order, and duplicates are allowed; the data stored in the map is not sequential, its keys cannot be duplicated, and its values can be duplicated.
6, List, Set, map implement collection interface?
Answer: List,set Yes, map is not.
7, List, MAP, set three interfaces, access to elements, what are the characteristics of each?
Answer: List holds elements in a particular order and can have repeating elements.
Set cannot have duplicate elements, internal ordering.
Map saves Key-value values, key cannot be duplicated, and value can be duplicated.
8, two objects of the same value x.equals (y) = = true, but can have different hash code, this sentence right?
Answer: Yes. If objects are to be saved in HashSet or HashMap, their equals is equal, their hashcode values must be equal. If it is not to be saved in HashSet or HashMap, then there is nothing to do with hashcode, at this time hashcode can not wait for, for example ArrayList stored objects do not implement Hashcode, of course, we have no reason not to implement, It's usually going to come true.
9, say some of the commonly used classes, packages, interfaces, please give 5?
Common classes: BufferedReader bufferedwriter filereader filewirter String Integer java.util.date,system,class,list, HashMap
Commonly used packages: Java.lang java.io java.util java.sql, javax.servlet,org.apache.strtuts.action,org.hibernate
Common interfaces: Remote List Map Document NodeList, Servlet,httpservletrequest,httpservletresponse,transaction (Hibernate), Session (Hibernate), HttpSession
Summary of basic knowledge of collection framework