First, Java Collection framework diagram
In the above class diagram, the real line border is the implementation class , such as Arraylist,linkedlist,hashmap, and so on, the polyline border is an abstract class , such as Abstractcollection,abstractlist,abstractmap, and the point line border is the interface , such as Collection,iterator,list.
Discover a feature, all of the above collection classes, have implemented the iterator interface, which is an interface for traversing the elements in the collection, consisting mainly of Hashnext (), Next (), remove () three methods.
One of its sub-interfaces Linkediterator added three methods on top of it, namely add (), previous (), hasprevious (). That is, if the implementation of the iterator interface, then in the collection of elements, can only be traversed backwards, the traversed element will not be traversed, usually the unordered collection is implemented by this interface, such as Hashset,hashmap, and those elements of the ordered collection, The implementation of the general is the Linkediterator interface, the implementation of this interface can be a collection of two-way traversal, either through next () access to the next element, but also through previous () access to the previous element, such as ArrayList.
Another feature is the use of abstract classes. If you want to implement a collection class yourself, implementing those abstract interfaces can be cumbersome and a lot of work. Abstract classes can be used at this time, and these abstract classes give us a lot of out-of-the-box implementations, and we just need to rewrite some methods to meet our needs or add some methods to implement the set class we need, and the workflow is greatly reduced.
Second, detailed
2.1HashSet
HashSet is a subclass of the set interface, and the main feature is that it cannot hold duplicate elements, and uses a hash of the storage method, so there is no order. There is no order here: the order in which the elements are inserted is inconsistent with the order of the outputs.
code example: Hashsetdemo
Packageedu.sjtu.erplab.collection;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Set; Public classHashsetdemo { Public Static voidMain (string[] args) {Set<String> set=NewHashset<string>(); Set.add (A); Set.add ("B"); Set.add (C); Set.add (C); Set.add ("D"); //using the iterator output collectionIterator<string> iter=Set.iterator (); while(Iter.hasnext ()) {System.out.print (Iter.next () )+" "); } System.out.println (); //use the for each output to combine for(String e:set) {System.out.print (e+" "); } System.out.println (); //using the ToString output collectionSystem.out.println (set); }}View Code
code example: Settest
Packageedu.sjtu.erplab.collection;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.InputStream;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Scanner;ImportJava.util.Set; Public classSettest { Public Static voidMain (string[] args)throwsFileNotFoundException {Set<String> words=NewHashset<string>(); //document generation via input flow//Method 1: This method does not need to throw an exceptionInputStream instream=settest.class. getResourceAsStream ("Alice.txt"); //Method 2: This method needs to throw an exception//InputStream instream = new FileInputStream ("d:\\documents\\workspace\\javastudy\\src\\edu\\sjtu\\erplab\\ Collection\\alice.txt ");Scanner in=NewScanner (instream); while(In.hasnext ()) {Words.add (In.next ()); } Iterator<String> iter=Words.iterator (); for(inti=0;i<5;i++) { if(Iter.hasnext ()) System.out.println (Iter.next ()); } System.out.println (Words.size ()); }}View Code2.2ArrayList
ArrayList is a subclass of list, which, contrary to HashSet, allows for repeating elements to be stored and therefore ordered. The order in which the elements in the collection are accessed depends on the type of the collection.
If ArrayList is accessed, the iterator will start at index 0, one iteration at a time, and an index value of 1. However, if you access the elements in HashSet, each element will appear in a random order. While it is possible to determine that all elements in the collection can be traversed during the iteration, it is not possible to predict the order in which the elements are accessed.
code example: Arraylistdemo
Packageedu.sjtu.erplab.collection;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classArraylistdemo { Public Static voidMain (string[] args) {List<String> arrlist=NewArraylist<string>(); Arrlist.add (A); Arrlist.add ("B"); Arrlist.add (C); Arrlist.add (C); Arrlist.add ("D"); //using the iterator output collectionIterator<string> iter=Arrlist.iterator (); while(Iter.hasnext ()) {System.out.print (Iter.next () )+" "); } System.out.println (); //use the for each output to combine for(String e:arrlist) {System.out.print (e+" "); } System.out.println (); //using the ToString output collectionSystem.out.println (arrlist); }}View Code2.3 Listiterator
Listiterator is an interface that inherits from iterator, which is a queue iterator. Designed to facilitate the list, providing forward/backward traversal. Compared to iterator, it adds API interfaces for adding, having a previous element, getting the previous element, and so on.
code example: Linkedlisttest (traversal with Listiterator)
Packageedu.sjtu.erplab.collection;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.ListIterator; Public classLinkedlisttest { Public Static voidMain (string[] args) {List<String> a=NewArraylist<string>(); A.add (A); A.add ("B"); A.add (C); System.out.println (a); List<String> b=NewArraylist<string>(); B.add ("D"); B.add (E); B.add ("F"); B.add ("G"); System.out.println (b); //Listiterator adds the Add (), previous (), and Hasprevious () methods based on the iteratorListiterator<string> aiter=A.listiterator (); //Common iterator only three methods, Hasnext (), Next () and remove ()Iterator<string> biter=B.iterator (); //b Merge into a, the interval crosses to insert the element in B while(Biter.hasnext ()) {if(Aiter.hasnext ()) Aiter.next (); Aiter.add (Biter.next ()); } System.out.println (a); //every two elements in B delete aBiter=B.iterator (); while(Biter.hasnext ()) {biter.next (); if(Biter.hasnext ()) {biter.next ();//Remove is paired with Next, and remove always deletes the pre-orderBiter.remove (); }} System.out.println (b); //remove elements from all B in aA.removeall (b); System.out.println (a); }}View Code
2.4HashMap
Refer to Next Blog: HashMap implementation principle
Third, comparative analysis
comparative analysis of common collection classes in Java
| Collection name |
Specific collection name |
is ordered |
Whether elements are allowed to repeat |
| Collection |
Whether |
Is |
| List |
Is |
Is |
| Set |
Abstractset |
Whether |
Whether |
|
HashSet |
|
TreeSet |
Is |
| Map |
Abstractmap |
Whether |
Use Key-value to map and store data
Key must be unique, value can be repeated |
|
HashMap |
|
TreeMap |
Is |
Java Basic Learning--4, collection class detailed