First, the classification of the collection:
Second, the common collection:
1. Collections in Java COLLECTION:JDK
1. List
listlist<string> list = new arraylist<>(), List.add ("a"), List.add ("B"), List.add ("C" ); SYSTEM.OUT.PRINTLN (list); [A, B, c]
2. Map
mapmap<string,string> map = new hashmap<>() map.put ("name", "by"); Map.put ("Age", " ); SYSTEM.OUT.PRINTLN (map); {name=by, age=18}
3. Set
// Set New Hashset<> (); Set.add ("a"); Set.add ("B"// [A, b]
4. Iterator Traversal Collection
// iterator traversing the list collection Iterator Iterator = list.iterator (); while (Iterator.hasnext ()) { = (String) Iterator.next (); System.out.println (parm); if (Parm.equals ("a")) { iterator.remove (); // [B, C]
5. Traverse Map
/*** Traverse Map * * Get method: * First way: Use KeySet * need to get key and value separately, no object-oriented thought * set<k> KeySet () return all The set set of the Key object*/ Static voidTraversemap () {Map<integer, string> map =NewHashmap<>(); Map.put (1, "AAAA"); Map.put (2, "BBBB"); Map.put (3, "CCCC"); SYSTEM.OUT.PRINTLN (map); Set<Integer> KS =Map.keyset (); Iterator<Integer> it =Ks.iterator (); while(It.hasnext ()) {Integer key=It.next (); String value=Map.get (key); System.out.println ("key=" + key + "value=" +value); } }
2, Guava Collections (Google Open source tools)
1. List
// Create List list<string> list = Lists.newarraylist ("A", "B", "C"), List.add ("D"); // Invert list // [D, C, B, a] // converts a list collection to a string of a specific rule String Listresult = Joiner.on ("-"// a-b-c-d
2. Map
//Define Mapmap<string,string > Map =Maps.newhashmap (); Map.put ("Name", "by"); Map.put ("Age", "23"); SYSTEM.OUT.PRINTLN (map); //{name=by, age=23}//converts a map collection to a specific rule stringString Mapresult = Joiner.on (","). Withkeyvalueseparator ("=")). Join (map); System.out.println (Mapresult); //name=by,age=23//define the form of a list in Map (map<string,list<integer>>)Multimap<string,integer> maps =arraylistmultimap.create (); Maps.put ("Map", 1); Maps.put ("Map", 2); SYSTEM.OUT.PRINTLN (maps); //{map=[1, 2]}
3. Set
// Define set set<string> set = sets.newhashset (); Set.add ("value");
3, Trove
1. Constructs a collection of basic types
// directly constructs a collection of type int New tintarraylist (); Intlist.add (1); Intlist.add (2); Intlist.add (3); Intlist.reverse (); System.out.println (intlist);
Java Collection Learning