First, Introduction
the Java Collection Framework, like the IO framework, it seems so complicated. system Framework, but once you drill down into the usage of each set, you can clearly see the difference between them and their relationship. Finally, a frame diagram is presented at a glance. Since the Java collection frame is divided into two major systems collection and map systems, the reason for this separation is that the collection is stored in a linear set, and the elements of map are in the form of key-value pairs (key-value). In fact, map and collection internal implementation is the existence of links, learning to understand. This article mainly describes the usage of the collections used in collection and the differences between them.
Ii. Introduction to the collection collection framework
The next surface of this picture, is the class diagram of the collection framework, the middle omitted a few insignificant classes, the overall is detailed. Italic bold Indicates the interface (the canonical class diagram should be plus <<interface>>).
collection interface is the root interface, and the Iterable interface can be simply understood as a labeled interface, which contracts all linear sets (arrays, queues, Stacks are linear sets, and map is a two-dimensional set that cannot be traversed directly, but also defines the Hasnext (), Next (), and remove () Three traversal methods, example: ArrayList extends Abstractlist implements List, Abstractlist itself has implemented the list interface, and here again write implements list is to make the whole frame structure clearer, many sets of frame diagram in order to see the convenience of all omitted to implement the interface this dashed line, but I think need to explain.
What's the difference between set and list, let's take a look at the following class diagram for collection, list, and set. Only unique methods are listed in the class diagram of the list interface, and methods inherited from collection are not enumerated repeatedly. Why is there no set interface? The method in the set interface is exactly the same as the collection, and without adding any new methods, the drawing is no longer repeated. set again to define the method in the collection interface, one is to add the class library as a unique method of the set of the Convention, although the method definition is the same, but the specific method implementation is different, such as the set of the Add method can not add duplicate elements, list can be The second is to make the set frame structure clearer.
Main differences:
1, list must be an ordered set, The set can be ordered or unordered, where the ordered order is the same as the order in which the elements are added in the inside of the set. In fact, the set interface note does not specifically state that the set should be unordered, but does not declare "an ordered collection " like the list interface, but there is a sentence in the iterator () method annotation of the set set: the Elements are returned in no particular order (unless this set are an instance of Some class that provides a Guara Ntee). The conclusion is that the set implementation class can also be ordered.
2, List allows repeating elements, more specifically, List allows to satisfy E1.equals (E2) elements to E1, E2, and if the list itself allows null elements, usually they allow multiple null elements. The set collection does not allow duplicate elements, and if a null value is allowed, there can be at most one null value.
3, list can be precise positioning of elements, can be based on the element subscript to any location of the elements to achieve additions and deletions. Index subscripts and arrays start from 0. For example, the list interface defines the method of add (int index, e element), get (int index), set (int index, e element), and so on, which can access the specified position elements. Set can only be traversed on a collection and cannot be randomly accessed elements.
Put the list in front of the said, as if the list is better than set, not so, each has an advantage, the following detailed comparison of 5 collection classes .
three, the realization principle and the concrete difference of collection collection class 1, ArrayList
ArrayList is a relatively common collection, the reason is called array list is because its bottom is implemented with arrays, arrays we are familiar with Ah, then first look at the array of what characteristics it. In the case of an object array, the element can be null, the element can be repeated n times, and the element subscript can be accessed anywhere (not out of bounds), and these are now the ArrayList features. One big feature of arrays is that queries are fast, and here's how arrays are addressed. When an array is created, a contiguous amount of space is allocated in memory, and the array name is the first address of the memory space. such as int[] arr=new int[10]; Assuming the first address is 22005,int[3] is to access the 4th element, its address is 22005+4*sizeof (int), the int size is 4, then the result is 22005+4*4=22021, go directly to 222021 this location area to take data on the line. Obviously, the array is directly addressed, and the lookup speed is fairly fast. However, if the content lookup, you can only traverse the array, ArrayList some methods indexof (object o), LastIndexOf (object o), remove (object o) is to iterate over the array to find the element, If the amount of data is large, the performance has been tested and decreased. The simple point of understanding is that ArrayList is an encapsulated class of arrays that can be used wherever arrays are used. To conclude, the growth strategy for the ArrayList internal array is oldlength>>1, which is inaccurate for each of the original half of the growth.
2, LinkedList
linkedlist Internal is a doubly linked list, learn the data structure of the two-way linked list, delete add is very convenient, directly modify before and after two elements of the pointer is done. so LinkedList provides a lot of convenient two-way additions and deletions of elements, the following look at its class diagram, only listed the LinkedList unique methods.
You can see that there are a lot of methods like Xxxfrist () and Xxxlash (), which really give full play to the role of doubly linked lists. With these methods, you can use a linked list as a stack, queue, or double-ended queue. For example, the action stack pops the topmost element with pop () and pushes () presses into the stack. In fact, these methods are just user interface, just change the method name, in which the reason to see the source code will know.
public e peek () { final Node<E> f = first; return (f == null) ? null : f.item; } Public e poll () { final Node<E> f = first; return (f == null) ? null : unlinkfirst (f); } public boolean offer (e e) { return add (E); } public boolean offerfirst (e e) { addfirst (e); return true; } &nbSp; public boolean offerlast (e e) { addlast (e); return true; } public e peekfirst () { final node<e> f = first; return (f == null) ? null : f.item; } public e peeklast () { final node<e > l = last; return (L == null) ? null : l.item; } public E Pollfirst () { final Node<E> f = first; return (F == null) ? null : unlinkfirst (f); } public e polllast () { final Node<E> l = last; return (L == null) ? null : unlinklast (l); }
You can see that the method is tuned to go to the end of the list of elements in the access chain. This also gives us a hint that good code method naming is really important! These methods are defined separately in LinkedList, so you cannot use these methods to create objects in polymorphic ways, such as List List=new LinkedList (), so that only the methods defined in list can be called. The list also has a very pit feature is that if you want to find an element every time you want to traverse the list, the search speed is slightly slow. In addition LinkedList use a special iterator listiterator, also its unique, this iterator is special is can from two-way iteration, can be iterated from the back to the forward iteration, its implementation or to rely on the doubly linked list, the use of more convenient, it should be remembered.
3. Vector
Vector is from JDK1.0, it is the set frame in the JDK1.2 after the emergence of the collection frame, so this vector is very old, its function and ArrayList basically the same, the interior is also an array implementation. The only difference is that the vector is thread-synchronized and the ArrayList thread is out of sync. No more here, this class has been basically deprecated!
4, HashSet
The set of sets is characterized by the absence of duplicate elements, so how does the hashset implement the uniqueness of the elements? By comparing hashcode and equals, if the hashcode is the same, it will continue to compare equals, if the hashcode is different and no longer compares equals, the last example illustrates:
package com.heima.collection;import java.util.*;p Ublic class hashsetdemo {public static void main (String[] args) {a a1=new a ("Hector", 22); A a3=new a ("Paul", 33); A a4=new a ("Zeus", 100); A a5=new a ("Zeus", 100); Hashset<a> set=new hashset<a> (); Set.add (A1); Set.add (A3); Set.add (A4); Set.add (A5); for ( Iterator<a> it=set.iterator (); It.hasnext ();) {System.out.println (It.next ());}}} Class a{private int age;private string name;public a () {super ();} @Overridepublic string tostring () {return "[age=" + age + ", name = " + name + "] ";} Public a (String name, int age) {super (); this.name = name;this.age = age;} Public int getage () {return age;} Public string getname () {return name;} Public void setage (int age) {this.age = age;} Public void setname (String name) {this.name = name;} @Overridepublic int hashcode () {system.out.println ("Hashcode ..." +this.getname ()); return 60 ;} @Overridepublic boolean equals (Object obj) {//name and age are equal, truea a= (a) Obj;return this.getname (). Equals (This.getname ()) &&a.getage () ==this.getage ();}}
Operation Result:
here rewrite hashcode () and Equals (), deliberately let hashcode return a fixed value of 60, By running the results you can see that when hashcode is equal, you will continue to judge that Equals,equals is true as a repeating element, false to add, and if you do not rewrite hashcode you will find that the Equals method is not called. Because the Hasdcode and equals methods are inherited from the superclass Object,object The default hashcode is calculated based on the memory address of the object, equals is compared with "= =", so the hashcode of different objects must be different, Equals is not true either. But there are conventions in Java: If two objects are equal (equal), then you must have the same hash code (hash code), so it is common to rewrite equals and hashcode at the same time.
HashSet Another feature is the disorder, hashset inside is a hash table (also known as a hash table), each input will go through the hash function to get a fixed-length hash (Key) =hashcode, the storage location of the element is based on this hash value, That's why it seems disordered.
4, TreeSet
As mentioned earlier, the set set can also be ordered, TreeSet is the ordered collection class, from the overall framework diagram can be seen TreeSet does not directly implement the set interface but the implementation of its sub-interface sorted, which further provides a set of the overall ordering of elements, These elements are sorted by their natural order, or by the comparator that are typically provided when an ordered set is created. Yes, it is. The elements in the TreeSet must provide a collation. There are two ways to sort treeset elements, one is that the element itself has a collation that implements the comparable interface, but instead provides a comparator comparator for treeset.
Public class comparedemo {public static void main (String[] args) { Student stu1=new student ("Hector", 29); Student stu2=new student ("Guoke", 23); Student stu3=new student ("Paul", 40); Student stu4=new student ("Aple", 23); student[] stu={stu1,stu2,stu3,stu4}; Treeset<student> tree=new treeset<student> (); Tree.add (STU1); Tree.add (STU2); Tree.add (STU3 ); Tree.add (STU4); Iterator it=tree.iterator (); while (It.hasnext ()) {System.out.println (It.next ());}}} Implement comparable interface class student implements comparable{public int age;public string name;public student () {super ();} Public student (String name, int age) {super (); this.name = name;this.age = age;} Implement the CompareTo method, define the comparison rules, sort by age first, age same by name//This method is internally automatically called @overridepublic int compareto (Object o) {int res=this.age-((Student) O). Age;if (res==0) return this.name.compareto (((Student) O). name); return res;} @Overridepublic string tostring () {return "student [age=" + age + ", name=" + name + "]" + "\ n";}}
This is the first implementation method, the Student class implements the comparable interface, which makes Student own the collation. The second is to provide a comparator for TreeSet:
Class Stucomparator implements comparator<student>{@Overridepublic int compare (Student O1, Student O2) {int res=o1 . Age-o2.age;if (res==0) return O1.name.compareTo (o2.name); return res;}}
When creating TreeSet, pass the comparator over treeset<student> tree=new treeset<student> (New Stucomarator ()), and the sorting effect can be If two ways exist at the same time, in this way main! Also note that if neither of these methods is present, the program will have an abnormal classcastexception.
The second approach is more flexible, allowing one instance of TreeSet to define its own collation, regardless of which object is stored using the same sort method, and the extension of the program is the preferred way.
The second feature is that there is no duplicate element, the TreeSet bottom is a red black tree (also known as self-balancing binary tree), this data structure is slightly complex, in TreeMap detailed description.
Four, the collection characteristics comparison
Collection Collection of Java collection frames