Java collection diagram, dotted box interface, solid wireframe is the specific class
Concrete Implementation Class
Basic use
(1) List:
List basic operation arraylist<string> arrayList = new arraylist<string> (); Arraylist.add ("Tom"); Arraylist.add ("Jerry"); Arraylist.add ("Micky"); Use Iterator to traverse elements iterator<string> it = Arraylist.iterator (); while (It.hasnext ()) {String str = it.next (); System.out.println (str); }//Insert element at specified location Arraylist.add (2, "Kate"); Direct access to elements by index for (int i = 0; i < arraylist.size (); i++) {System.out.println (Arraylist.get (i)); } list<string> sublist = new arraylist<string> (); Sublist.add ("Mike"); AddAll (collection<? extends string> c) adds all the elements in the given set Arraylist.addall (sublist); Determines if an element contains an if (Arraylist.contains ("Mike")) {System.out.println ("Mike is included in the list"); } linkedlist<string> LinkedList = new linkedlist<string> (); Linkedlist.addall (arrayList); GetSpecifies the element System.out.println (Linkedlist.get (4)); Gets the first element of System.out.println (Linkedlist.getfirst ()); Gets the last element System.out.println (Linkedlist.getlast ()); Gets and deletes the first element System.out.println (Linkedlist.pollfirst ()); Gets, but does not remove the first element System.out.println (Linkedlist.peekfirst ());
(2) Map:
Map basic Operation hashmap<string, integer> map = new hashmap<string, integer> (); add element map.put to map ("Tom", 26); Map.put ("Jack", 18); Map.put ("Micky", 17); Map.put ("Kate", 15); Get Value System.out.println ("Jack" + Map.get ("Jack") + "years old") based on key; Remove Map.Remove ("Micky"); Traverse map for (entry<string, integer> entry:map.entrySet ()) {System.out.println ("name:" + Entry. GetKey () + "Age:" + entry.getvalue ()); }//Key The same element will be overwritten map.put ("Jack", 19); Get Value System.out.println ("Jack" + Map.get ("Jack") + "years old") based on key; Determines whether a key if (Map.containskey ("Tom")) {System.out.println ("Tom") is included ("Map.get"); }//Determine whether to include a value if (map.containsvalue) {System.out.println ("the map include the value 26" ); }//Determine if the map is empty if (!map.isempty ()) {Get the map size System.out.println ("The map ' s size=" + map.size ()); }//Gets the collection of keys for (String Str:map.keySet ()) {System.out.println (str); } treemap<string, integer> TreeMap = new treemap<string, integer> (); Treemap.putall (map); The output is sorted by key value for (entry<string, integer> entry:treeMap.entrySet ()) {System.out.println ("name : "+ entry.getkey () +" Age: "+ entry.getvalue ()); Name:jack age:19//name:kate age:15//Name:tom age:26} linkedhashmap<string, integer> Linkedhashmap = new linkedhashmap<string, integer> (); add element linkedhashmap.put to map ("Tom", 26); Linkedhashmap.put ("Jack", 18); Linkedhashmap.put ("Micky", 17); Linkedhashmap.put ("Kate", 15); Keep the Insertion order for (entry<string, integer> entry:linkedHashMap.entrySet ()) {System.out.prinTLN ("Name:" + entry.getkey () + "Age:" + entry.getvalue ()); Name:tom age:26//Name:jack age:18//Name:micky age:17//Name:kate age:15}
(3) Set:
Set base operation list<integer> List = new arraylist<integer> (); List.add (3); List.add (4); hashset<integer> HashSet = new hashset<integer> (); Hashset.add (1); Hashset.add (3); Hashset.add (2); Hashset.add (6); Duplicate elements will not be added Hashset.add (3); Returns True if an element is added (Hashset.addall (list)) {System.out.println ("add success"); }//Determine if there is a collection if (Hashset.containsall (list)) {System.out.println ("the HashSet is cont Ain 3 and 4 "); } iterator<integer> it = Hashset.iterator (); while (It.hasnext ()) {System.out.print (It.next () + ""); 1 2 3 4 6//See the result is sorted, hashset according to the hash function, the integer value of the hashcode is its int value}//Swap Group object[] integers = Hashset.toarray (); for (int i = 0; i < integers.length; i++) { System.out.print ((Integer) integers[i]); }//Remove element Hashset.remove (3); treeset<string> TreeSet = new treeset<string> (); Treeset.add ("C"); Treeset.add ("A"); Treeset.add ("D"); Treeset.add ("B"); for (iterator<string> Strit = Treeset.iterator (); Strit.hasnext ();) {System.out.print (Strit.next ()); ABCD in alphabetical order} linkedhashset<string> Linkedhashset = new linkedhashset<string> (); Linkedhashset.add ("C"); Linkedhashset.add ("A"); Linkedhashset.add ("D"); Linkedhashset.add ("B"); for (iterator<string> Linkedit = Linkedhashset.iterator (); Linkedit. Hasnext ();) {System.out.print (Linkedit.next ()); CADB in order of insertion}
Overview of Java Collections