1.java Collection Class Diagram
1.1
1.2
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 first iterator interface, then in the collection of elements, you can only traverse 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, which implements a set of interfaces that can be traversed in both directions, accesses the next element through next (), and accesses the previous element through previous (), 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.
1.3
2. 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;PublicClassHashsetdemo {PublicStaticvoidMain (string[] args) {set<string> set=New hashset<string>(); Set.add ("a"); Set.add ("B"); Set.add ("C" ); // use Iterator Output collection iterator<string> Iter=set.iterator (); while (Iter.hasnext ()) {System.out.print (Iter.next () + "" );} System.out.println (); //for< Span style= "color: #000000;" > (String e:set) {System.out.print (e+ "" // Use the ToString output collection
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;PublicClasssettest {PublicStaticvoid Main (string[] args)ThrowsFileNotFoundException {set<string> words=New hashset<string>();//Document generation via input flow//Method 1: This method does not need to throw an exception InputStream 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=New Scanner (instream); while (In.hasnext ()) {Words.add (In.next ());} Iterator<string> iter=words.iterator (); For (int i=0;i<5;i++) { if(Iter.hasnext ())) System.out.println (Iter.next ());} System.out.println (Words.size ()); }}
2.2ArrayList
ArrayList is a sub-class of list, it and HashSet idea, allow to store repeating elements, so 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: ArraylistdemoView Code2.3LinkedList
LinkedList is an ordered sequence that can efficiently insert and delete operations at any location.
code example: Linkedlisttest
PackageEdu.sjtu.erplab.collection;ImportJava.util.ArrayList;ImportJava.util.Iterator;ImportJava.util.List;ImportJava.util.ListIterator;PublicClasslinkedlisttest {PublicStaticvoidMain (string[] args) {list<string> a=New arraylist<string>(); A.add ("a"); A.add ("B"); A.add ("C"); System.out.println (a); List<string> b=New arraylist<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 on iterator basis listiterator<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 Bwhile (Biter.hasnext ()) { if (Aiter.hasnext ()) Aiter.next (); Aiter.add ( Biter.next ()); } System.out.println (a); // delete a biter= every two elements in B b.iterator (); whileif remove and Next are paired, remove always deletes the preamble Biter.remove ();} System.out.println (b); // Delete all the elements of B in a
2.4HashMap
Refer to a previous blog: HashMap implementation principle
2.5WeekHashMapDemo
PackageEdu.sjtu.erplab.collection;ImportJava.util.WeakHashMap;PublicClassWeekhashmapdemo {PublicStaticvoidMain (string[] args) {int size = 100;if (Args.length > 0) {size = Integer.parseint (args[0]); } key[] keys =NewKey[size]; Weakhashmap<key, value> WHM =New Weakhashmap<key, value>();for (int i = 0; i < size; i++) {Key k =NewKey (integer.tostring (i)); Value v =NewValue (integer.tostring (i));if (i% 3 = = 0) {Keys[i] = k;//Strong references} whm.put (k, v);//All key values are put into weakhashmap} System.out.println (WHM); System.out.println (Whm.size ()); System.GC ();Try{//Give the processor time to the garbage collector for garbage collection Thread.Sleep (4000); }Catch(Interruptedexception e) {E.printstacktrace ();} System.out.println (WHM); System.out.println (Whm.size ()); }}ClassKey {String id;PublicKey (String ID) {This.id =Id }PublicString toString () {ReturnId }PublicIntHashcode () {ReturnId.hashcode (); }PublicBooleanEquals (Object R) {Return (Rinstanceof Key) && Id.equals ((Key) R). ID);} public void Finalize () {System.out.println ("finalizing Key" + ID);}} class Value {String id; public Value (String ID) { this.id = ID;} public String toString () { return ID;} public void Finalize () {System.out.println ("finalizing Value" + ID);}
Output results
{50=50, 54=54, 53=53, 52=52, 51=51, 46=46, 47=47, 44=44, 45=45, 48=48, 49=49, 61=61, 60=60, 63=63, 62=62, 65=65, 64=64, 55 =55, 56=56, 57=57, 58=58, 59=59, 76=76, 75=75, 74=74, 73=73, 72=72, 71=71, 70=70, 68=68, 69=69, 66=66, 67=67, 85=85, 84=84 , 87=87, 86=86, 81=81, 80=80, 83=83, 82=82, 77=77, 78=78, 79=79, 89=89, 88=88, 10=10, 90=90, 91=91, 92=92, 93=93, 94=94, 9 5=95, 96=96, 97=97, 98=98, 99=99, 20=20, 21=21, 12=12, 11=11, 14=14, 13=13, 16=16, 15=15, 18=18, 17=17, 19=19, 8=8, 9=9, 3 1=31, 4=4, 32=32, 5=5, 6=6, 30=30, 7=7, 0=0, 1=1, 2=2, 3=3, 29=29, 28=28, 27=27, 26=26, 25=25, 24=24, 23=23, 22=22, 40=40, 41=41, 42=42, 43=43, 38=38, 37=37, 39=39, 34=34, 33=33, 36=36, 35=35}100finalizing key 98Finalizing key 97Finalizing key 95Finalizing key 94Finalizing key 92Finalizing key 91Finalizing key 89Finalizing key 88Finalizing key 86Finalizing key 85F Inalizing key 83Finalizing key 82Finalizing key 80Finalizing key 79Finalizing key 77Finalizing key 76Finalizing key 74Fina Lizing Key 73FinalizingKey 71Finalizing key 70Finalizing key 68Finalizing key 67Finalizing key 65Finalizing key 64Finalizing key 62Finalizing key 61Finalizing key 59Finalizing key 58Finalizing key 56Finalizing key 55Finalizing key 53Finalizing key 52Finalizing key 50 Finalizing key 49Finalizing key 47Finalizing key 46Finalizing key 44Finalizing key 43Finalizing key 41Finalizing key 40Fin Alizing key 38Finalizing key 37Finalizing key 35Finalizing key 34Finalizing key 32Finalizing key 31Finalizing key 29Finali Zing Key 28Finalizing key 26Finalizing key 25Finalizing key 23Finalizing key 22Finalizing key 20Finalizing key 19Finalizin G Key 17Finalizing key 16Finalizing key 14Finalizing key 13Finalizing key 11Finalizing key 10Finalizing key 8Finalizing Ke Y 7Finalizing key 5Finalizing key 4Finalizing key 2Finalizing key 1{54=54, 51=51, 45=45, 48=48, 60=60, 63=63, 57=57, 75=75 , 72=72, 69=69, 66=66, 84=84, 87=87, 81=81, 78=78, 90=90, 93=93, 96=96, 99=99, 21=21, 12=12, 15=15, 18=18, 9=9, 6=6, 30=30 , 0=0, 3=3, 27=27, 24=24, 42=42, 39=39, 33=33, 36=36}34
Question: Why value is not recycled.
3. Compare
|
|
is ordered |
Whether elements are allowed to repeat |
Collection |
Whether |
Is |
List |
Is |
Is |
Set |
Abstractset |
Whether |
Whether |
|
HashSet |
|
TreeSet |
Yes (with binary sort tree) |
Map |
Abstractmap |
Whether |
Use Key-value to map and store data, key must be unique, value can be repeated |
|
HashMap |
|
TreeMap |
Yes (with binary sort tree) |
The Java collection class is detailed