The JAVA Collection is intended to be of fixed size for Array arrays, and the same array can only store data of the same type (basic type/reference type) a java set can store a set of data with an unfixed number of operations.
All JAVA collections are located in the java. util package! JAVA collections can only store reference data types, but cannot store basic data types. JAVA collections are mainly divided into three types: Set (Set) List (List) Map () Collection Interface Collection is the most basic Set interface, declared general methods applicable to JAVA collections (including Set and List only. Both Set and List inherit Conllection. Map has no Collection interface method: boolean add (Object o): Reference void clear () to add an Object to the Set (): delete all objects in the set, that is, the reference boolean isEmpty () of these objects is no longer held: determines whether the set is null boolean contains (Object o ): iterartor iterator (): returns an Iterator Object that can be used to traverse the element boolean remove (Object o) in the set ): delete an Object reference int size () from the set: returns the number of elements in the Set Object [] toArray (): returns an array, which contains all elements in the set about: both the Iterator () and toArray () methods are used for all elements of the set. The former returns an Ite Rator object, which returns an array containing all elements in the set. The Iterator interface declares the following method: hasNext (): determines whether the elements in the set have been traversed. If not, true next () is returned. The next element remove () is returned (): delete the last element returned by the next () method from the collection. Set: Set is the simplest Set. Objects in the set are not sorted in a specific way and there are no repeated objects. The Set interface mainly implements two implementation classes: HashSet class, which uses the hash algorithm to access objects in the Set. The access speed is faster. The TreeSet: TreeSet class implements the SortedSet interface, sorts objects in a set. Set usage: stores object references. No repeated object Set set = new HashSet (); String s1 = new String ("hello"); String s2 = s1; string s3 = new String ("world"); set. add (s1); set. add (s2); set. add (s3); System. out. println (set. size (); // The number of objects in the print set is 2. How does the add () method of Set determine whether the object has been stored in the collection? Boolean isExists = false; Iterator iterator = set. iterator (); while (it. hasNext () {String oldStr = it. next (); if (newStr. equals (oldStr) {isExists = true ;}} List (List): the feature of List is that its elements are stored linearly, and repeated objects can be stored in the set. The main implementation classes of the List interface include ArrayList (), which indicates that the length can be changed to an array. Random Access to elements is allowed, and it is slow to insert and delete elements to and from ArrayList. Linked List (): uses the Linked List Data Structure in implementation. Fast insertion and deletion, and slow access. For random access to a List, only random elements are retrieved at a specific location. The get (int index) method of List puts back the object at the index location specified by the index parameter in the set. The subscript starts from "0.The two most basic methods to retrieve all objects in a set: 1: Use the for loop and get () Methods: for (int I = 0; I System. out. println (list. get (I);} 2: Use Iterator: Iterator it = list. iterator (); while (it. hashNext) {System. out. println (it. next);} Map (ing): Map is a set that maps key objects and value objects. Each element of Map contains a pair of key objects and value objects. Map does not inherit from the Collection interface when retrieving elements from the Map set, as long as the key object is given, the corresponding value object is returned. Common Methods of Map: 1. add and delete: Object put (Object key, Object value): add the Element Object to the set remove (Object key ): delete KEY-related element void putAll (Map t): Add all elements from a specific image to the image void clear (): delete all mappings from the image 2 query operation: object get (Object key): obtains the key objects in the Map set related to the key of the keyword. duplicate key objects are not allowed. That is to say, any two key objects use equals () the comparison result is false. however, you can map any number of keys to the same value object. Conllections, for more information, see the JDK help documentation. There are many other Map applications. Specifically, Conllections provides many List/Map practical methods, it is very useful for normal development. The next meeting will be continuously modified! Boolean containsKey (Object key): key boolean containsValue (Object value): determines whether the image has a value int size (): returns the number of mappings in the current image boolean isEmpty (): determines whether there is any ing in the image this article from the CSDN blog, reprinted please indicate the source: http://blog.csdn.net/wordinput/archive/2010/01/22/5223876.aspx List by object into the Order of saving objects, no sorting or editing operations are performed. Set accepts each object only once and uses its own internal sorting method (generally, you only care about whether an element belongs to the Set but not its order-otherwise, you should use the List ). Map also saves a copy of each element, but this is based on the "key". Map also has built-in sorting, so it does not care about the order of element addition. If the order of adding elements is important to you, you should use LinkedHashSet or LinkedHashMap. in fact, there are two functional methods of List: one is the basic ArrayList, which has the advantages of random access elements and the other is the more powerful sort List, it is not designed for fast random access, but a more general method. List: Order is the most important feature of List: it ensures that the specific order of elements is maintained. List adds many methods to the Collection so that elements can be inserted and removed from the List (this is only recommended for using the List .) A List can generate ListIterator, which can be used to traverse the List in two directions, or to insert and remove elements from the List. ArrayList: List implemented by arrays. Quick and Random Access to elements is allowed, but it is slow to insert and remove elements into the List. ListIterator should only be used to traverse the ArrayList from the forward, rather than to insert and remove elements. This is much higher than the overhead of the shortlist. Optimize List: the sequential access is optimized, and the overhead of insert and delete to the List is not large. Random Access is relatively slow. (Use ArrayList instead .) The following methods are available: addFirst (), addLast (), getFirst (), getLast (), removeFirst (), and removeLast (), these methods (not defined in any interface or base class) allow the consumer list to be used as a stack, queue, and bidirectional queue. The Set function method Set has the same interface as the Collection, so there is no additional function, unlike the previous two different lists. Set is actually a Collection, but the behavior is different. (This is a typical application of inheritance and Polymorphism: different behavior .) Set does not store repeated elements (it is more responsible for determining how to judge the same element). Set: Each element stored in the Set must be unique because the Set does not store repeated elements. The equals () method must be defined for the element added to the Set to ensure the uniqueness of the object. Set has the same interface as Collection. The Set interface does not guarantee the order of maintenance elements. HashSet: A Set designed for quick search. The object stored in the HashSet must define hashCode (). TreeSet: The Set that stores the order. The bottom layer is the tree structure. It can be used to extract ordered sequences from the Set. LinkedHashSet: query speed with a HashSet, and internal use of the linked list to maintain the order of elements (insert order ). When you use the iterator to traverse the Set, the result is displayed in the order of element insertion. Map function method put (Object key, Object value) Add a "value" (desired) and a "key" associated with the "value) (use it for search ). The get (Object key) method returns the "value" associated with the given "key ". You can use containsKey () and containsValue () to test whether a Map contains a "key" or "value ". The standard Java class library contains several different maps: HashMap, TreeMap, LinkedHashMap, WeakHashMap, and IdentityHashMap. They all have the same basic interface Map, but the behavior, efficiency, sorting policy, saving object lifecycle and determining "key" equivalent policies are different. Execution efficiency is a big problem of Map. Let's look at what get () is going to do, and we will understand why searching for "keys" in ArrayList is quite slow. This is where HashMap improves the speed. HashMap uses a special value called hash code to replace the slow key-to-key search. The hash code is a "relatively unique" used to represent the int value of an object. It is generated by converting certain information of the object. All Java objects can generate hash codes, because hashCode () is a method defined in the base class Object. HashMap uses the hashCode () of an object for quick query. This method can significantly improve the performance. Map: maintain the relevance of "key-value pairs", so that you can use "key" to find "value" HashMap: Map is implemented based on the hash list. The overhead of inserting and querying "key-value pairs" is fixed. You can use the constructor to set the capacity and load factor to adjust the container performance. LinkedHashMap: similar to HashMap, but when traversing it iteratively, the order in which "key-value pairs" are obtained is the insertion order, or the least recently used (LRU) Order. It is only a little slower than HashMap. It is faster during iterative access because it uses the linked list to maintain the internal order. TreeMap: based on the data structure of the red and black trees. When you view "keys" or "key-value pairs", they are sorted (the order is determined by Comparabel or Comparator ). TreeMap features that the results you get are sorted. TreeMap is the only Map with subMap () method. It can return a subtree. WeakHashMao: weak key Map. objects used in Map are also allowed to be released. This is designed to solve special problems. If a reference other than map points to a key, the key can be recycled by the garbage collector. IdentifyHashMap: Use = to replace the hash map used by equals () to compare the "key. Designed to solve special problems.