Objective
The Guava class library has been briefly introduced in the Front (blog: Java code Elegant way-guava (with the relevant jar package download), the following is a brief introduction to the operation of the Set,map,list collection of the use of tools, learning to use these tools flexibly, will use our development, more time-saving, code robustness and readability, and more concise, which is what I call "code elegance", Let's develop the time to deal with the more important things, nonsense not to say, a look at the code is all clear.
Creating a generic collection more about
Create a generic collection more about list<string> stringlist=lists.newarraylist ();//Create listset<string> stringset= Sets.newhashset ();//Create setmap<string,map<string,list<string>>> Map=maps.newhashmap ();//Create Map
Creates a collection of the specified initial capacity
Creates a collection of the specified initial capacity list<string> listwithsize=lists.newarraylistwithcapacity (10);//Create a listset<string that specifies the initial size capacity > setwithsize=sets.newhashsetwithexpectedsize (10);//Create setmap<string,map<string,list<string with the specified initial size >>> mapwithsize=maps.newhashmapwithexpectedsize (10);//Create a map with the specified initialization size
When initializing, specifies that the containing element
When initialized, specifies the containing element list<string> listwithelem=lists.newarraylist ("Chen", "Lei", "Xing"); Set<string> setwithelem=sets.newhashset ("Chen", "Lei", "Xing");
Simple creation of immutable collections
Simple creation of immutable collections immutablelist<string> nolist=immutablelist.of ("AA", "BB", "cc"); immutablemap<string,string > Nomap=immutablemap.of ("Key1", "value1", "Key2", "value2");
Set set Multiset and map sets that can hold equal elements Multimap
Set set of MultiSet and map sets that can hold equal elements multimapmultiset<string> MultiSet = Hashmultiset.create (); Multiset.add ("Chen") ; Multiset.add ("Chen"); Multiset.add ("Xing"); for (String S:multiset) {System.out.println (s);} Multimap<string,string> multimap=hashmultimap.create () multimap.put ("Chen", "one"), Multimap.put ("Chen", "22" ); for (map.entry<string,string> entry:multiMap.entries ()) {System.out.println ("key:" +entry.getkey () + " Value: "+entry.getvalue ());}
Output Result:
Chenchenxingkey:chen value:22key:chen value:11
Find the differences and similarities between the 2 maps and return them in map form
Find out the differences and similarities between the 2 maps, return immutablemap<string,integer> onemap=immutablemap.of in map form ("Key1", 1, "Key2", 2); Mmutablemap<string,integer> twomap=immutablemap.of ("Key11", One, "Key2", 2); Mapdifference<string,integer> diffhadle=maps.difference (ONEMAP,TWOMAP); Map<string,integer> Commonmap=diffhadle.entriesincommon ();//{"Key2", 2}, if there is no common entry, returns a length of 0 mapmap<string ,integer> diffonleft=diffhadle.entriesonlyonleft ();//returns the different or special elements of the map on the left map<string,integer> diffOnRight= Diffhadle.entriesonlyonright ();//returns the different or special elements in the Map on the right for (map.entry<string, integer> Entry: Diffonright.entryset ()) {System.out.println ("key in co-map:" +entry.getkey () + " value:" +entry.getvalue ());}
Output Result:
Key:key11 Value:11 in co-map
Find out the different elements of the 2 set and the same elements, and return them in set form
Find out the different elements of the 2 set and the same elements, return the set<string> oneset=sets.newhashset in set form ("Chen", "Lei", "Java"); Set<string> twoset=sets.newhashset ("Chen", "Lei", "Hadoop"); Setview<string> diffsethandle=sets.difference (Oneset, twoset);//is to get different or unique elements on the left, if none, return a set of length 0 set<string > diffimmutable=diffsethandle.immutablecopy ();//Returns a set copy of the unique element collection in the left set of the immutable iterator iter= Diffsethandle.iterator (); while (Iter.hasnext ()) {System.out.println ("different elements of Set:" +iter.next (). toString ());} Setview<string> commonset=sets.intersection (Oneset, Twoset); Set<string> commonimmutable=commonset.immutablecopy ();//Returns an immutable set of 2 set copies of the same element set
Output Result:
Different elements of Set: Java
List reversal Segmentation
Lists Other methods of operation List<string> listdemo=lists.newarraylist ("Chen", "Lei", "Xing"); List<string> Listrever=lists.reverse (Listdemo);//Returns the inverted view of the specified list, so the size is immutable list<string> listreverchange =immutablelist.copyof (listrever). Reverse ()///Of course this re-gets a new inverted List, so the size is variable list<list<string>> Listlist=lists.partition (listdemo,2);//Divide list by the specified size {{"Chen", "Lei"},{"Xing"}}
Collection manipulation tools in the Org.apache.commons.collections Toolkit
Org.apache.commons.collections for Apache Open source Collection Operation Toolkit, there are also a lot of tools, there are also stored in the same key map, but unlike guava effect, interested in and related to the download of the jar package can look at my blog:
Bag Collection Tool Class (apache-commons-collections3.2 Toolkit) for use in Javause of Map helper classes in Bidimapmultimaplazymap-apache-commons-collections
Java Code Elegant Way-guava
Simple use of the strings class-code elegant Way Guava (i)
Simple use of the files class-code elegant Way Guava (ii)
The intersection difference set of the set,map,list set holds the same value reversal split, etc.-code elegance of the Tao Guava (iii)
Please specify-java my life (Chen Leixing) Source: http://blog.csdn.net/chenleixing/article/details/44708533
Finally, seriously read the netizens, the great gods, if there is a feeling I this program ape has a place to say wrong or wrong or you have a good
proposal or suggestions or ideas, but also look
You kindness alms n seconds to leave your valuable text (message), so that you, I, and the vast number of
programs apes
grow and Progress faster ....
The intersection difference set of the set,map,list set holds the same value reversal split, etc.-code elegance of the Tao Guava (iii)