Java container class Summary

Source: Internet
Author: User

1. description of the java container classification diagram: the simplified diagram (the crude section is the key container) is displayed on the left, and the complete container classification diagram is displayed on the right. container class interfaces and abstract container Class 2.1 indicate that container interfaces are the basis of containers. You can use interfaces to separate the container implementation from the container interface. Therefore, you can use the same method to access the container without worrying about the specific data structure of the container. Likewise, the Iterator interface allows users to access different container classes in the same way. 2.2 container interface (Collection, Map, Iterator) 1) collection interface copy Code * boolean add (Object obj): add an Object. If the set changes, true * Iterator iterator () is returned (): returns the Iterator interface Object * int size () * boolean isEmpty () * boolean contains (Object obj) * void clear () * <T> T [] toArray (T [] a) Copy Code 2) Map interface (store key-value pairs and Map values can also be a container) copy the Code * Object get (Object key) * Object put (Object key, Object value) * Set keySet (): returns the keys set Set <K> keySet ()* Set entrySet (): returns mappings set Set <Map. entry <K, V> entrySet () * containsKey () * containsValue () Copy code 3) Iterator interface * Object next () * boolean hasNext () * void remove () note: the remove function cannot be executed multiple times in a row. Otherwise, IllegalStateException (if the next method has not yet been called, or the remove method has already been called after the last call to the next method .) common usage: Iterator it = collection. iterator (); while (it. HasNext () {Object obj = it. next (); // do something} 2.3 sub-interface (List, Set, ListIterator, SortedMap, SortedSet) 1) List (sequential repeatable, in order, you can add the index parameter to the method during operations, as shown below: * boolean add (E element) * void add (int index, E element) * E set (int index, E element) * E get (int index); 2) Set (the object cannot be duplicated without order, and thus cannot be operated through the index) 3) ListIterator (Iterator for List, list is a two-way table. Therefore, some new methods are added to Iterator to allow traverse the List in either direction) * boolean h AsPrevious (); * E previous (); * int previusindex () 4) SortedMap description) sort, or sort by the comparator provided during the creation of the sorted ing (A Map that further provides a total ordering on its keys. the map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time) public interface SortedMap <K, V> extends Map <K, v> * Comparator comparator () * Object firstKey () * Object lastKey () 5) SortedSet is mainly used for sorting operations, all subclasses that implement this interface are sorted subclass copy code public interface SortedSet <E> extends Set <E> * Comparator comparator () * E first (): returns the first element * E last () * SortedSet <E> headSet (E toElement): returns less than toElement * SortedSet <E> tailSet (E fromElement) * SortedSet <E> subSet (E fromElement) Copy code 2.4 abstract container Class 1) Description: classes can be easily defined using the abstract container class, instead of implementing all the methods in the container interface container in each class. 2) include: * Abstr ActCollection public abstract class AbstractCollection <E> extends Objectimplements Collection <E> * AbstractList public abstract class AbstractList <E> extends AbstractCollection <E> implements List <E> * AbstractSet public abstract class implements actset <e> extends AbstractCollection <E> implements Set <E> * AbstactMap public abstract class AbstractMap <K, v> extends Object implements Map <K, V> * AbstractSequentialL Ist public abstract class AbstractSequentialList <E> extends actlist <E> 3. specific container class 3.1 Summary 1) collection: ArrayList, LinkedLsit, Vector, Stack TreeSet, HashSet, LinkedHashSet 2) Map: HashMap, LinkedHashMap, WeakHashMap, TreeMap, HashTable, identityHashTable (key comparison is through = instead of equals) 3.2 common container Class 1) ArrayList and sort list (both non-synchronous, thread security issues need to be considered when multithreading), Vector (synchronous ), stack 1. the List interface supports accessing elements through the index method: ArrayList random access is fast and slow; sorted List is fast. The random access is slow; the Vector achieves synchronization, so it is slower than the ArrayList. 2. The sorted list uses a two-way linked list to implement the sorted list to provide additional get, remove, and insert methods at the header or tail of the sorted list. These operations enable the queue list to be used as a stack, queue, or two-way queue (deque ). 3. arrayList does not define a growth algorithm. When a large number of elements need to be inserted, you can call the ensureCapacity method to improve the efficiency of adding. 4. vector is similar to ArrayList, but it is synchronous, and multithreading is secure (another difference is that ArrayList is increased by half by default during expansion, and Vector is doubled ). No matter whether it is a single thread or multiple threads, the Vector is 5 slower than the ArrayList. stack inherits from the Vector to implement a post-first-out Stack. 6. to implement synchronization, you can call the synchronizedList method of the Collections tool class as follows: copy the code List = Collections. synchronizedList (new ArrayList (); synchronized (list) {Iterator I = list. iterator (); // Must be in synchronized block while (I. hasNext () foo (I. next ();} Or: List list = Collections. synchronizedList (new synchronized list (); copy code 7. the definition is as follows: (Note that the destlist implements Deque) public C4. Ss ArrayList <E> extends actlist <E> implements List <E>, RandomAccess, Cloneable, Serializablepublic class catalog List <E> extends actsequentiallist <E> implements List <E>, deque <E>, Cloneable, Serializablepublic class Vector <E> extends actlist <E> implements List <E>, RandomAccess, Cloneable, Serializable 2) TreeSet, HashSet, LinkedHashSet (HashSet, treeSet is NOT thread-safe) 1. treeSet is the only implementation class of the SortedSet interface. The eSet can ensure that the Set elements are in the sorting state, which is efficient and can improve the efficiency of the program. The TreeSet can be sorted by compareTo or compare, so as long as the values are equal, even if the values are equals (different objects) cannot be added to the Collection (fails to obey Set interface) 2. hashSet is highly efficient. Unlike TreeSet, the equals of objects are compared to distinguish different objects, so that different objects can not be repeatedly added to the set. The hashCode () function is hard to determine. The default hashCode function of the object tries the memory address value of the object. The quality of the hashCode function is the key to the HashSet performance. 3. The sequence HashSet is the same as the HashSet. It also determines the storage location of the elements based on the hashCode value of the elements. However, it also uses the linked list to maintain the order of elements. During iterative access to all the elements in the Set, LinkedHashSet performs better than HashSet, but the insert performance is slightly inferior to HashSet. 4. set can insert null, up to one null 3) HashMap (non-synchronous), HashTable (thread-safe), TreeMap, WeakHashMap 1. differences between HashTable and HashMap: (for details, see HashTable and HashMap) 1) Hashtable inherits from the Dictionary class, while HashMap inherits from the AbstractMap class. Both implement the Map interface. 2) the methods in Hashtable are Synchronize, while the methods in HashMap are non-Synchronize by default. 3) in Hashtable, keys and values cannot contain null values. In HashMap, null can be used as a key, so there is only one such key; one or more keys can correspond to a value of null 4) HashTable directly uses the hashCode of the object. HashMap recalculates the hash value. 2. weakHashMap is an improved HashMap that implements "weak references" for keys. WeakHashMap uses element references instead of values as keys, that is, it must be referenced in the same way (a = B) can be found. In addition, if a key is no longer referenced by external entities, the key can be recycled by GC. 3. TreeMap is the implementation of the SortedMap Interface Based on the red and black trees. This class ensures that the ing sorts the keywords in ascending order. Different constructor methods may sort the keywords in the natural order of the key classes. 4. definition: public class Hashtable extends Dictionary implements Map, Cloneable, Serializable public class HashMap extends AbstractMap implements Map, Cloneable, Serializable public class TreeMap <K, V> extends actmap <K, v> implements NavigableMap <K, V>, Cloneable, Serializable 4. adding to the container Class 1) using abstract programming ideas, using the parent class reference to point to the subclass object during creation, and returning the abstract interface 2) if Stack and queue operations are involved, you should consider using the List, for To quickly insert and delete elements, use the random list. to quickly access elements randomly, use the ArrayList. 3) if the program uses non-synchronous classes in a single-threaded environment, the efficiency is high. 4) you can use the unmodifiableList/unmodifiableMap/unmodifiableSet/unmodifiableSortedMap/unmodifiableSortedSet in the Collections tool class to create unmodifiable lists, maps, and lists. 5) You can use Collections in the Collections tool class. synchronizedList (new ArrayList () 6) You can use Arrays. equal () checks whether two arrays are equal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.