Map of Guava Learning

Source: Internet
Author: User

Guava Chinese is the meaning of pomegranate, this project is an open source project of Google, contains many Google Core Java Common library.

Currently mainly includes:

  • Com.google.common.annotations
  • Com.google.common.base
  • Com.google.common.collect
  • Com.google.common.io
  • Com.google.common.net
  • Com.google.common.primitives
  • Com.google.common.util.concurrent
    Online API Doc:http://tool.oschina.net/apidocs/apidoc?api=guava

The following person classes are available:

 Public  class  person {    PrivateString name;Private intAge Public  Person(String name,intAge) { This. name = name; This. Age = Age; } PublicStringGetName() {returnName } Public int Getage() {returnAge }@Override     PublicStringtoString() {return "person{"+"Name= '"+ name +' \ '+", age="+ Age +'} '; }}
1 A unique key after converting list to Map1.1 conversion
 Public  class testmap {     Public Static void Main(String args[]) {list<person> persons = Arrays.aslist (NewPerson ("Zhang", the),NewPerson ("Wang", -),NewPerson ("Lee", -)        );/** * The converted map has a unique key * /map<string, person> map = maps.uniqueindex (persons,NewFunction<person, string> () {@Override             PublicStringApply(Person person) {returnPerson.getname ();    }        }); }}
1.2 The converted key is not unique
 Public  class testmap {     Public Static void Main(String args[]) {list<person> persons = Lists.newarraylist (NewPerson ("Zhang", the),NewPerson ("Zhang", -),NewPerson ("Lee", -)        );/** * The converted map has duplicate keys */multimap<string, person> Multimap = multimaps.index (persons,NewFunction<person, string> () { PublicStringApply(Person person) {returnPerson.getname ();    }        }); }}
2 Convert set to map
 Public  class testmap {     Public Static void Main(String args[]) {set<person> persons = Sets.newhashset (NewPerson ("Zhang", the),NewPerson ("Zhang", -),NewPerson ("Lee", -)        );/** * The value of set is key to calculate a value * /Map<person, string> map = maps.asmap (persons,NewFunction<person, string> () { PublicStringApply(Person person) {returnPerson.getname ();    }        }); }}
3 Convert the value of map
 Public  class testmap {     Public Static void Main(String args[]) {list<person> persons = Lists.newarraylist (NewPerson ("Zhang", the),NewPerson ("Wang", the),NewPerson ("Lee", -)        ); map<string, person> map = maps.uniqueindex (persons,NewFunction<person, string> () { PublicStringApply(Person person) {returnPerson.getname (); }        });/** * Use key and value as input to calculate a new value */map<string, integer> map2 = maps.transformentries (map,NewMaps.entrytransformer<string, Person, integer> () {@Override             PublicIntegerTransformentry(String s, person person) {returnPerson.getage (); }        });/** * Use function to calculate a new value */map<string, double> map3 = maps.transformvalues (map,NewFunction<person, double> () {@Override             PublicDoubleApply(Person person) {return(Double) Person.getage ();    }        }); }}
4 Filter the map to match the criteria in the map
 Public  class testmap {     Public Static void Main(String args[]) {list<person> persons = Lists.newarraylist (NewPerson ("Zhang", the),NewPerson ("Wang", the),NewPerson ("Lee", -)        ); map<string, person> map = maps.uniqueindex (persons,NewFunction<person, string> () { PublicStringApply(Person person) {returnPerson.getname ();        }        }); Map = maps.filterentries (map,NewPredicate<map.entry<string, person>> () {@Override             Public Boolean Apply(map.entry<string, person> stringpersonentry) {returnStringpersonentry.getkey ()! =NULL&& stringpersonentry.getvalue ()! =NULL;        }        }); Map = Maps.filterkeys (map,NewPredicate<string> () {@Override             Public Boolean Apply(String s) {returns! =NULL&& s.length () >2;        }        }); Map = maps.filtervalues (map,NewPredicate<person> () {@Override             Public Boolean Apply(Person person) {returnPerson! =NULL&& person.getage () > the; }        }); for(Map.entry<string, Person> entry:map.entrySet ()) {System.out.println (Entry.getkey () +" "+ Entry.getvalue ()); }    }}

Map of Guava Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.