Turn from:
Http://www.cnblogs.com/mingcn/archive/2010/10/22/JavaContainer.html
Java Review notes--Summary of container knowledge points
There are two types of containers in Java, one is single-valued collection, and the other is a map for storing key-value pairs.
Collection
There are two types, set and list, except that set is not repeatable , and list is repeatable.
Set
There are two commonly used: HashSet and TreeSet, whose internal implementation is a map, whose elements are equivalent to the key in the map, and value is an object constant
| 1234567 |
privatetransient HashMap<E,Object> map; private static final Object PRESENT = new Object(); public HashSet() { map = newHashMap<E,Object>();} |
| 1234567 |
privatetransient NavigableMap<E,Object> m; private static final Object PRESENT = new Object(); public TreeSet() { this(newTreeMap<E,Object>());} |
All operations on the set are eventually passed on to the map, and details are described in the map below
List
A simple extension of the collection interface allows you to put any object into a list container and remove it from it when needed.
Commonly used are ArrayList and LinkedList, which are sequential, repeatable collection types
ArrayList: As the name implies, a list of data, in fact, encapsulates an array , so it's extremely fast to access.
| 123 |
privatetransient Object[] elementData; private intsize; |
It then encapsulates some common operations such as inserting, deleting, and so on for an array.
Speaking of ArrayList had to mention arrays class and array []
ArrayList can store different types of objects (although this is generally not recommended), and arrays can only be of the same type
ArrayList can dynamically increase the length, but the efficiency is not high, and the array can only be fixed length, but very efficient. Whenever you execute a method that adds elements such as Add/insert, the array length is checked first, and if it is, it will rebuild an array with 3/twice times the current capacity, copy the old elements into the new array, and discard the old array, at which point the expansion operation It should be compared with the effect of efficiency.
ArrayList provides common methods, add/get/indexof/remove, and so on, while the corresponding arrays does not provide the add and remove methods, the Query element index first sort and then BinarySearch
The ArrayList sort requires an external method Collections.sort (..), and the array sort uses Arrays.sort (..), both of which can use natural order (implement comparable) or specify with comparator
LinkedList: It is clear that the chain list, the internal implementation is the lead node of the two-way list, suitable for in the middle of the list need to frequently insert and delete operations
| 123456789101112 |
privatetransientEntry<E> header = new Entry<E>(null, null, null);privatetransientintsize = 0;privatestaticclass Entry<E> { E element; Entry<E> next; Entry<E> previous;//..} |
Map
is a container that associates a key with a value object, analogous to collection, which can be said: The ID of a value in a collection object is its index value in the container, and in a map, the ID of a value is its corresponding key. This allows us to use the map without limiting the index value of the int type, which can be indexed with any type of object. It is because of the index attribute of key that it is not allowed to have a key of the same value in the map (the set's inner implementation is the set of keys in the map, so the elements of the set cannot be duplicated). Of course, value can be duplicated and can even be the same reference. When the map processes the same key, the new value is stored, and the old value is replaced and returned .
HashMap: A hash algorithm is used to quickly find an element.
The hash value of the key is used as the memory index, the internal implementation is an appropriate length of the chain array , the hash value of key corresponding to the array subscript, and then indirectly corresponding to the memory, is a more efficient way to access. Key Hashcode () in the same case, placed in the same single list structure.
The type of key in a hashmap should override the Hashcode () method to ensure that the same value is returned when the two object equals is true, otherwise it will be treated as a different key at the time of the repeat check, which can cause unintended consequences.
The method of judging repetition in a hash container:
First the hash (Key.hashcode ()) is the same, the hash (int) is an internal algorithm, the specifics are not discussed
is different, it does not repeat
The same, the
Compare whether two keys are the same reference
Yes, then repeat
No, call the Equals method again
Returns true to repeat
Returns false to not repeat
TreeMap: The tree storage structure is stored sequentially, so it has some extended methods, such as Firstkey (), Lastkey (), and so on, you can also specify a range from the TreeMap to get its child map.
An internal implementation is a two-fork sort tree in which the sequential traversal result is an ascending sequence. So ask for his key must be comparable or create treemap when you specify comparator.
When key implements the Comparable<e> interface, the Comparato (e-E) method must be implemented, and when an external comparator (comparator<t>) is used, the comparator<t> is implemented compare (t T1, T T2) method
Related knowledge
Generics (Generic)
Generics allow coding to define some mutable types, but they must be declared before they are used.
| 123456789101112131415 |
classtestGeneric<T> { T t; testGeneric(T t) { this.t = t; }}classtest { testGeneric<String> tgs = newtestGeneric("Hi,Gineric!"); testGeneric<Integer> tgi = newtestGeneric(100);//AutoBoxing { System.out.println(tgs.t);//Output:Hi,Gineric! System.out.println(tgi.t);//AutoUnBoxing&Output:100 }} |
Note that the actual type of the Java generic type parameter is eliminated at compile time (upcast to Object), so it is not possible to know the type of its type parameter at run time. The Java compiler automatically joins the encoding of the type conversion when compiling generics, so the speed of operation is not accelerated by the use of generics.
Iterators (Iterator)
Provides a way to access individual elements in a container (container) object without exposing the object's internal details.
Iterator mode is the preferred way to traverse all the elements in a container
Collection defines the iterator<e> iterator () method, the subclasses each implement the method, we call directly to
Although there is no definition in map, we can use the iterator () method of the Map.entryset ()
| 1234567 |
Iterator i = someMap.entrySet().iterator();while(i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); Object key = i.getKey(); Object value = i.getValue(); //something TODO} |
or convert to collection (Set) with enhanced for loop
| 123456 |
Set<Map.Entry> entrySet = someMap.entrySet();for(Map.Entry entry : entrySet){ Object key = entry.getKey(); Object value = entry.getValue(); //something TODO} |
Listiterator inherits the iterator and provides a way to iterate through the list.
It is important to note that the Remove method that calls iterator removes the element returned by the last call to Next () (or previous (), if possible)
If you modify the collection that the iterator points to by calling this method in a different way than when you iterate, the behavior of the iterator is indeterminate.
In other words, when calling iterator, it is best not to call collection's Add/remove and other methods
Another: the ToString () method of the container class also implements traversal by calling the iterator () method
Using an enhanced for loop for a collection is also an implicit call to the iterator () method
Stack, queue and other structures, simple operation, not to repeat
Go Java's Collection/map