(ii) Overview of the Java Collection framework

Source: Internet
Author: User
Tags comparable repetition set set

A set of frame diagrams

Description: For the above frame diagram has the following points

1. All collection classes are located under the java.util package. The Java collection class is mainly derived from two interfaces:Collection and map, Collection and map are the root interfaces of the Java Collection framework, which in turn contains some sub-interfaces or implementation classes.
2. The collection interface: 6 interfaces (short dashed lines), representing different collection types, is the basis of the collection framework.
3. Abstract class: 5 abstract classes (long dashed lines), partial implementations of the collection interface. Can be extended to custom collection classes.
4. Implementation class: 8 implementation Class (solid line representation), the specific implementation of the interface.
5. The Collection interface is a set of objects that are allowed to be duplicated.
6. The set interface inherits Collection, and the collection element is not duplicated.
7. The List interface inherits Collection, allows repetition, and maintains element insertion order.
8. The map interface is a key-value object and has no relation to the collection interface.
9.Set, list, and map can be seen as the three main categories of collections:
The list collection is an ordered collection, and the elements in the collection can be repeated, and accessing the elements in the collection can be accessed based on the index of the element.
The set collection is an unordered collection, and the elements in the collection cannot be duplicated, and access to the elements in the collection can only be accessed based on the element itself (which is why the elements in the collection are not allowed to be duplicated).
The map collection holds elements of the key-value pair form, which can only be accessed by the key of each element when accessing its value.

Second, the overall analysis

General Description:
Look at the frame chart above, first grab its trunk, i.e. collection and map.

1, collection is an interface, is a highly abstracted collection, it contains the basic operation and properties of the collection . The collection contains the list and set of the two major branches.
(1) List is an ordered queue, and each element has its index. The index value of the first element is 0. The list implementation class has LinkedList, ArrayList, Vector, Stack.

(2) Set is a set that is not allowed to have duplicate elements. The implementation class for set has Hastset and TreeSet. HashSet relies on HashMap, which is actually implemented through HashMap; TreeSet relies on treemap, which is actually implemented by TreeMap.

2, map is a mapping interface, that is, key-value key value pairs. Each element in the map contains "one key" and "key corresponding to value". Abstractmap is an abstract class that implements most of the APIs in the map interface. And Hashmap,treemap,weakhashmap are inherited from Abstractmap. Although Hashtable inherits from dictionary, it implements the map interface.

3, Next, see iterator. It is a tool for iterating through a collection, that is, we usually traverse the collection through the iterator iterator. We say that collection relies on iterator because the collection implementation class implements the iterator () function and returns a iterator object. Listiterator is specifically designed to traverse the list.

4. Then look at enumeration, which is the abstract class introduced by JDK 1.0. Functions, like iterator, also traverse collections, but enumeration are less powerful than iterator. In the above block diagram, enumeration can only be used in Hashtable, Vector, and stack.

5, finally, see Arrays and collections. These are the two tool classes that manipulate arrays and collections.

With the overall framework above, we then analyze each class separately.

Third, collection interface

The collection interface is the root interface that handles the collection of objects, which defines a number of methods for manipulating elements. Collection interface has two main sub-interfaces List and Set, note that map is not collection sub-interface, this should be kept in mind .

1.List interface

The list collection represents an ordered collection, and each element in the collection has its corresponding sequential index. The list collection allows you to use duplicate elements, which can be indexed to access the collection elements at the specified location.

The list interface inherits from the collection interface, which can define an ordered set of allowed duplicates . Because the elements in the list are ordered, we can access the elements in the list by using an index (the position of the element in the list, like an array subscript), which is similar to an array of java.

The list interface is a direct interface to the collection. The list represents an ordered collection, which maintains the order of elements in a particular order of insertion. The user can precisely control where each element in the list is inserted, and can access the element based on the integer index of the element (in the list) and search for the elements in the list. The implementation of the list interface collection mainly includes: ArrayList, LinkedList, Vector, Stack.

(1) ArrayList

ArrayList is a dynamic array and is the most common collection we use. It allows any element that conforms to the rule to be inserted even including null. Each ArrayList has an initial capacity (10), which represents the size of the array. As the elements in the container continue to increase, the size of the container increases as well. A capacity check is carried out each time an element is added to the container, and the expansion is performed when the overflow is fast. So if we are clear about the number of elements inserted, it is better to specify an initial capacity value, to avoid excessive expansion operations and waste time, efficiency.

Size, IsEmpty, get, set, iterator, and listiterator operations are all run at a fixed time. The add operation runs at the allocated fixed time, that is, adding n elements requires an O (n) time (because of the expansion, this is not just adding elements that can be as simple as allocating fixed-time overhead).

ArrayList specializes in random access. At the same time ArrayList is not synchronous.

(2) LinkedList

The same implementation of the list interface LinkedList and ArrayList different,ArrayList is a dynamic array, and LinkedList is a doubly linked list . So it provides an additional Get,remove,insert method at the first or the end of the LinkedList in addition to the basic operating method of the ArrayList.

Due to the way the implementation is different,LinkedList cannot be accessed randomly , and all of its operations are performed according to the needs of the doubly linked list. An operation indexed in a list traverses the list from the beginning or end (from one end near the specified index). The advantage of this is that you can insert and delete operations in the list at a lower cost.

As with ArrayList,LinkedList is also unsynchronized . If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:
List List = Collections.synchronizedlist (new LinkedList (...));

(3) Vector

Similar to ArrayList, but vectors are synchronous . So vector is a dynamic array of thread safety . It operates almost the same as ArrayList.

(4) Stack

Stack inherits from Vector and implements a last-in-first-out stack. The stack provides 5 additional ways to make the vector available as a stack. The basic push and pop methods, and the Peek method to get the stack top element, the empty method tests if the stack is empty, and the search method detects the position of an element on the stack. Stack has just been created as an empty stack.

2.Set interface

Set is a collection that does not include duplicate elements. It maintains its own internal ordering, so random access doesn't make any sense. As with list, it also allows NULL to exist but only one . Because of the particularity of the set interface, all elements in the incoming set set must be different , and it is important to note that any mutable object that causes E1.equals (E2) ==true when manipulating elements in the collection is bound to produce some problems. The set interface has three concrete implementation classes, namely, the hash set hashset, the chain hash set linkedhashset and the tree set TreeSet.

Set is a collection that does not contain duplicate elements, unordered, that is, any of the two elements E1 and E2 have e1.equals (E2) =false, and set has a maximum of one null element. It is important to note that although the elements in the set are not in order, the position of the element in the set is determined by the hashcode of the element, and its exact position is actually fixed.

It is also important to note that there are special requirements for non-repetition in the set interface.
As an example: Object A and Object B, which are different two objects, normally they can be put into the set, but if both objects A and B both rewrite the hashcode and Equals methods, and the overridden hashcode and equals methods are the same. Then A and B cannot be put into the set set at the same time, that is, the de-weight and hashcode in the set set are directly related to the Equals method.

For a better understanding, consider the following example:

public class test{public static void Main (string[] args) {      set<string> set=new hashset<string> ();      Set.add ("Hello");      Set.add ("World");      Set.add ("Hello");      System.out.println ("The size of the collection is:" +set.size ());      SYSTEM.OUT.PRINTLN ("The element in the collection is:" +set.tostring ());   

Operation Result:

The size of the collection is: 2
The elements in the collection are: [World, Hello]

Parsing: Because the hashcode and equals methods are overridden in the string class, they are used to compare whether strings stored by a pointed string object are equal. So the second hello here is not to be added.

Let's look at an example:

public class Testset {public        static void Main (string[] args) {                set<string> books = new Hashset<string> ;();        Add a String Object        Books.add (new String ("Struts2 authoritative guide"));                Add a String object again,        //Because the two string objects compare equality by the Equals method, the addition fails, returning false        Boolean result = Books.add (The new string (" Struts2 authoritative guide "));                SYSTEM.OUT.PRINTLN (result);                The following output sees only one element of the collection        System.out.println (books);}        }

Operation Result:

False
[Struts2 authoritative Guide]

Note: In the program, the book Collection two times the string object is obviously not an object (the program through the New keyword to create a string object), when using the = = operator to determine the return of false, using the Equals method to return true, so it cannot be added to the Set collection, Only one element can be output at a final.

(1) HashSet

HashSet is a collection of elements that do not have duplicates . It is implemented by HashMap and does not guarantee the order of the elements (there is no order here: the order in which the elements are inserted is inconsistent with the order of the output), and the hashset allows the use of the null element . HashSet is unsynchronized, and if more than one thread accesses a hash set at the same time, and at least one of them modifies the set, it must remain externally synchronized. HashSet stores the elements of a collection by a hash algorithm, so it has good access and lookup performance.

HashSet is implemented in roughly the following way, through a hashmap storage element, where the element is stored in the HashMap key, and value uses a uniform object.

HashSet in the use and understanding of the error prone:

Storing null values in A.hashset
Null values are allowed in hashset, but only a null value can be stored in the hashset.

The location of the stored element in the B.hashset is fixed
The elements stored in HashSet are unordered, this is nothing to say, but because HashSet is based on the hash algorithm, using hashcode, so the position of the corresponding elements in the hashset is fixed.

C. Variable objects (Mutable object) must be operated with care. If a mutable element in a set changes its state, causing Object.Equals (Object) =true will cause some problems.

(2) Linkedhashset

Linkedhashset inherits from HashSet, its bottom layer is based on Linkedhashmap to realize , orderly, non-synchronous . The Linkedhashset collection also determines where the element is stored, based on the hashcode value of the element, but it maintains the order of the elements using the linked list . This makes the elements appear to be saved in an insertion order, that is, when the collection is traversed, Linkedhashset will access the elements of the collection in the order in which they are added.

(3) TreeSet

TreeSet is an ordered set , the bottom layer is based on TREEMAP implementation , non-thread-safe . TreeSet can ensure that the collection element is in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting , in which the natural sort is the default sorting method. When we construct TreeSet, if we use a constructor without parameters, the natural comparator is used treeset; If you need to use a custom comparer, you need to use a parameter with a comparer.

Note: The TreeSet collection does not compare elements through the hashcode and equals functions. It is determined by the compare or Comparaeto function to determine whether the elements are equal. The Compare function determines the ID of two objects by the same ID as the repeating element and is not added to the collection.

Four, Map interface

Unlike the list and set interfaces, a map is a collection of key-value pairs that provide a key-to value mapping. At the same time it did not inherit collection. In map it guarantees a one by one correspondence between key and value. This means that a key corresponds to a value, so it cannot have the same key value, but the value can be the same.

1.HashMap

Implemented in a hash table data structure, the location of the object is computed by a hash function, which is designed for fast querying, which defines an array of hash tables (entry[] table), which converts the hash address of an element into an index stored in a group by a hash conversion function, if there is a conflict, A hash list is used to string all elements of the same hash address, possibly by looking at the source of the Hashmap.entry, which is a single-linked list structure.

2.LinkedHashMap

Linkedhashmap is a subclass of HashMap, which preserves the order of insertions , and chooses Linkedhashmap if the order of the output needs to be the same as the input.
Linkedhashmap is a hash table and link list implementation of the map interface with a predictable iteration order. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant.
The difference between LINKEDHASHMAP implementations and HashMap is that the latter maintains a double-link list that runs on all items. This list of links defines the order of iterations, which can be in the order of insertion or in the order of access.
The order of the elements in the linked list can be divided into: linked lists in the order of insertion, and linked lists in the order of access (called Get methods). The default is to sort by insert order , if you specify in order of access, then when the Get method is called, the element of this access is moved to the tail of the linked list, and the linked list can be sorted in order of access.
Note that this implementation is not synchronous . If more than one thread accesses the linked hash map at the same time, and at least one of the threads modifies the mapping from the structure, it must remain externally synchronized.
Because Linkedhashmap needs to maintain the insertion order of the elements, performance is slightly lower than hashmap performance, but it will perform well when iterating through all the elements in the map, because it maintains the internal order in the list.

3.TreeMap

TreeMap is an ordered set of Key-value, non-synchronous , based on the red-black trees (red-black tree) implementation , each key-value node as a node of the red-black tree. TreeMap will be sorted when stored, the Key-value key value pairs will be sorted according to Key , and the ordering method is divided into two kinds, one is natural sort , and the other is custom sort , depending on the construction method used.

Natural sort: All keys in TreeMap must implement the comparable interface , and all keys should be objects of the same class , otherwise the classcastexception exception will be reported.

Custom sort: When defining treemap, create a comparator object that sorts all the key values in all TreeMap. It is not necessary to implement the comparable interface for all keys in TreeMap when using custom sorting.

TreeMap the criteria for determining the equality of two elements: two keys return 0 through the CompareTo () method, which is considered equal to the two keys.

If you use a custom class as the key value in TreeMap, and you want TreeMap to work well, you must override the Equals () method in your custom class , and the criterion for determining equality in TREEMAP is: Two keys through Equals () The CompareTo method returns True, and the comparison with the () method should be returned as 0.

Five similarities and differences.

1.ArrayList and LinkedList

(1) ArrayList is a data structure based on dynamic array, LinkedList data structure based on linked list.
(2) for random access get and set,arraylist are definitely better than LinkedList, because linkedlist to move the pointer.
(3) for new and deleted operations Add and remove,linedlist are the dominant, because ArrayList to move the data.
This point depends on the actual situation. If you insert or delete only a single piece of data, the ArrayList speed is better than LinkedList. But if bulk random insert deletes data, LinkedList speed is much better than ArrayList. Because ArrayList each insertion of data, you move the insertion point and all subsequent data.

2.HashTable and HashMap

Same point:

(1) All implementations of the map, cloneable, java.io.Serializable interface.
(2) is a hash table that stores "key-value pairs (key-value)", and they are all implemented using the Zipper method.

Different points:

(1) Historical reasons: Hashtable is based on the old Dictionary class, and HashMap is an implementation of the map interface introduced by Java 1.2.
(2) Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous.
(3) Null value processing: HashMap key, value can be null,hashtable key, value is not null.
(4) The base class is different: HashMap inherits from Abstractmap, and Hashtable inherits from dictionary.

Dictionary is an abstract class that inherits directly from the object class and does not implement any interfaces. The dictionary class was introduced in JDK 1.0. Although dictionary also supports basic operations such as "Add Key-value key-value pair", "Get Value", "Get Size", but its API function is less than map, and dictionary is usually through enumeration (enumeration class) to Traverse, The map is traversed by iterator (an iterative M-device). However, since Hashtable also implements the map interface, it supports enumeration traversal and iterator traversal.
Abstractmap is an abstract class that implements most of the API functions of the map interface and provides great convenience for the specific implementation classes of the map. It is a new class for JDK 1.2.

(5) The supported traversal types are different: HashMap only supports iterator (iterator) traversal. Hashtable supports both iterator (iterators) and enumeration (enumerators) traversal.

3.HashMap, Hashtable, Linkedhashmap and TreeMap comparisons

Hashmap is the most commonly used map, which stores data according to the Hashcode value of the key, which can be obtained directly from the key, with fast access speed . When traversing, the order in which the data is obtained is completely random . HashMap only one record is allowed to have a key of NULL; Allows the value of multiple records to be null; HashMap does not support thread synchronization , where multiple threads can write HashMap at any one time, and may result in inconsistent data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing.

Hashtable is similar to HashMap, except that it does not allow the record key or value to be null ; it supports thread synchronization , where only one thread can write Hashtable at any one time. It also causes the Hashtale to be slow to write.

Linkedhashmap Saves the insertion Order of records , when traversing linkedhashmap with iterator, the first record must be inserted first , or it can be constructed with parameters and sorted by the number of applications. The traversal will be slower than HashMap, but there is an exception, when the HashMap capacity is large, the actual data is small, the traversal may be slower than Linkedhashmap, because the Linkedhashmap traverse speed is only related to the actual data, and capacity-independent, And HashMap's traverse speed is related to his capacity. if the order of the output needs to be the same as the input, then it can be implemented by using LINKEDHASHMAP, which can also be arranged in the order of reading, as can be applied in the connection pool. The difference between LINKEDHASHMAP implementations and HashMap is that the latter maintains a doubly linked list that runs on all items. This list of links defines the order of iterations, which can be in the order of insertion or in the order of access. For Linkedhashmap, it inherits all the elements with HashMap, the underlying use of a hash table, and a doubly linked list . Its basic operation is similar to the parent class HashMap, which implements its own list of linked properties by overriding the methods associated with the parent class.

TreeMap implements SORTMAP interface, internal implementation is red black tree . The records that can be saved are sorted by key , by default, by ascending order of key values , or by specifying a sort comparer, and when traversing treemap with iterator, the resulting records are sorted out. TreeMap does not allow the value of key to be null. is not synchronized.

     in general, we use the most is hashmap,hashmap inside the key value of the pair is taken out at random, it is based on the hashcode value of the key to store data, according to the key can directly get its value, With fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a map.
     TreeMap takes out the sorted key-value pairs. But if you want to traverse the key in natural or custom order , then TreeMap is better.
     Linkedhashmap is a subclass of HashMap, if required output is the same order as input, then Linkedhashmap can be implemented, It can also be arranged in the reading order, as can be applied in the connection pool.

Import Java.util.hashmap;import java.util.iterator;import Java.util.linkedhashmap;import java.util.TreeMap;public  Class Maptest {public static void main (string[] args) {//hashmap hashmap<string,string> HashMap =        New HashMap ();        Hashmap.put ("4", "D");        Hashmap.put ("3", "C");        Hashmap.put ("2", "B");        Hashmap.put ("1", "a");        iterator<string> Iteratorhashmap = Hashmap.keyset (). Iterator ();        System.out.println ("hashmap-->");            while (Iteratorhashmap.hasnext ()) {Object key1 = Iteratorhashmap.next ();        System.out.println (Key1 + "--" + hashmap.get (key1));        }//linkedhashmap linkedhashmap<string,string> Linkedhashmap = new Linkedhashmap ();        Linkedhashmap.put ("4", "D");        Linkedhashmap.put ("3", "C");        Linkedhashmap.put ("2", "B");        Linkedhashmap.put ("1", "a");        iterator<string> Iteratorlinkedhashmap = Linkedhashmap.keyset (). Iterator (); SYstem.out.println ("linkedhashmap-->");            while (Iteratorlinkedhashmap.hasnext ()) {Object Key2 = Iteratorlinkedhashmap.next ();        System.out.println (Key2 + "--" + linkedhashmap.get (Key2));        }//treemap treemap<string,string> TreeMap = new TreeMap ();        Treemap.put ("4", "D");        Treemap.put ("3", "C");        Treemap.put ("2", "B");        Treemap.put ("1", "a");        iterator<string> Iteratortreemap = Treemap.keyset (). Iterator ();        System.out.println ("treemap-->");            while (Iteratortreemap.hasnext ()) {Object Key3 = Iteratortreemap.next ();        System.out.println (Key3 + "--" + treemap.get (Key3)); }    }}

Output Result:

Hashmap-->3--c2--b1--a4--dlinkedhashmap-->4--d3--c2--b1--atreemap-->1--a2--b3--c4--d

4.HashSet, Linkedhashset, treeset comparison

Set interface
Set does not allow the same element to be included , and if an attempt is made to add two identical elements to the same collection, the Add method returns False.
The set determines that two objects are the same, not using the = = operator, but according to the Equals method . That is, as long as two objects are returned True,set with the Equals method, the two objects will not be accepted.

HashSet
HashSet has the following features:
, the order of the elements is not guaranteed and the sequence is subject to change.
--not synchronous.
The collection element can be null, but only one null is placed.
When an element is deposited into a hashset union, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines where the object is stored in hashcode based on the HashSet value. To put it simply, the HashSet set determines that two elements are equal by two objects that are compared by the Equals method, and that the return value of the Hashcode () method of two objects is also equal.
Note that if you want to put an object in HashSet and override the Equals method of the corresponding class for that object, you should also override its Hashcode () method. The rule is that if two objects return true through the Equals method, their hashcode should also be the same. In addition, the attribute used in the object as the equals comparison criterion should be used to calculate the value of the hashcode.

The

linkedhashset
    linkedhashset Collection is also depending on the element's Hashcode value to determine where the element is stored , but it maintains the order of elements using a linked list. This makes the elements appear to be saved in an insertion order, that is, when the collection is traversed, Linkedhashset will access the elements of the collection in the order in which they are added.
    Linkedhashset performance is better than hashset when iterating through all the elements in the set, but the performance is slightly inferior to hashset when inserted.

The

TreeSet class
    TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection element is in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. The object that is added to the treeset should be the same class. The
    treeset determines that two objects are unequal by two objects that return false through the Equals method, or 0 is not returned by the CompareTo method.
Natural Sort
    Natural sort Use the CompareTo (Object obj) method of the element you want to sort to compare the size relationships between elements, and then arrange the elements in ascending order.
    Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that implements the object of the interface to compare the size. The Obj1.compareto (obj2) method returns 0, indicating that the two objects being compared are equal, and if a positive number is returned, the OBJ1 is greater than obj2, and if it is negative, obj1 is less than obj2. If we always return true for the Equals method of two objects, then the CompareTo method returned by the two objects should return 0.
Custom Sort
    natural sorting is based on the size of the collection elements, in ascending order, if you want to customize the sorting, you should use the comparator interface to implement the int compare (T o1,t O2) method.

Package com.test;  Import Java.util.HashSet;  Import Java.util.LinkedHashSet;    Import Java.util.TreeSet; /** * @description Several set comparisons * HashSet: a hash table stores information by using a mechanism called hashing, and the elements are not stored in a particular order; * Linkedhashset: Maintains a link to the collection in the order in which the elements are inserted   Tables that allow iterations in the collection in the order in which they are inserted; * TreeSet: Provides an implementation that uses a tree structure to store a set interface, where objects are stored in ascending order, and the time to access and traverse is fast. * @author Zhou-jingxian * * */public class Setdemo {public static void main (string[] args) {Hashse          t<string> hs = new hashset<string> ();          Hs.add ("B");          Hs.add ("A");          Hs.add ("D");          Hs.add ("E");          Hs.add ("C");          Hs.add ("F");                    System.out.println ("HashSet order: \ n" +hs);          Linkedhashset<string> LHS = new linkedhashset<string> ();          Lhs.add ("B");          Lhs.add ("A");          Lhs.add ("D");          Lhs.add ("E");          Lhs.add ("C");          Lhs.add ("F");                    System.out.println ("Linkedhashset order: \ n" +LHS); treeset<string> ts = new TreeseT<string> ();          Ts.add ("B");          Ts.add ("A");          Ts.add ("D");          Ts.add ("E");          Ts.add ("C");          Ts.add ("F");      System.out.println ("TreeSet order: \ n" +ts); }  }

(ii) Overview of the Java Collection Framework

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.