Guava Pack Learning---Bimap

Source: Internet
Author: User

Bimap is also the new collection class provided in guava, the alias is called Two-way map, is key-"value,value-" key, that is, you can locate the value by key, you can also use value to locate key.

This scenario is often encountered in everyday development.

In fact, Bimap is relatively simple, it is an interface, extension of the map interface, which is also the <K,V> format, but it is not allowed to have a duplicate V, This is important, when you try to put a duplicate V is an error message prompt. A V without a repeat will ensure that when you turn the map upside down, the K can be uniquely positioned, and we can look at its implementation:

Old habits, or see hashbimap, the creation of the new collection class is basically using the Create method:

 Public static <k, v> hashbimap<k, v> Create () {    return Create (+);  }   publicstatic <k, v> hashbimap<k, v> create (int  expectedsize) {     return New Hashbimap<k, v>(expectedsize);  }

Its additions and deletions are basically no different from the normal map, except that the same value can not be inserted into.

The point is that it has a reverse function:

@Override PublicBimap<v, k>inverse () {return(Inverse = =NULL) ? Inverse =Newinverse (): inverse; }  Private Final classInverseextendsAbstractmap<v, k>ImplementsBimap<v, k>, Serializable {BiMap<k, v>forward () {returnHashbimap. This; } @Override Public intsize () {returnsize; } @Override Public voidClear () {forward (). Clear (); } @Override Public BooleanContainsKey (@Nullable Object value) {returnforward (). Containsvalue (value); } @Override PublicK Get (@Nullable Object value) {returnmaps.keyornull (Seekbyvalue (Value, Smearedhash (value))); } @Override PublicK put (@Nullable V value, @Nullable k key) {returnPutinverse (value, key,false); } @Override Publick forceput (@Nullable V value, @Nullable k key) {returnPutinverse (value, key,true); } @Override PublicK Remove (@Nullable Object value) {Bientry<k, v> entry =Seekbyvalue (value, Smearedhash (value)); if(Entry = =NULL) {        return NULL; } Else{Delete (entry); Entry.previnkeyinsertionorder=NULL; Entry.nextinkeyinsertionorder=NULL; returnEntry.key; }    }

In fact, the <K,V> into a <v,k>, and this reverse is in the back you re-modify the map, it will also follow the change, so do not worry about it is a one-time, otherwise it is better to iterate to get a new map out:

 Public void Testbimap () {        hashbimap<String,String> map = hashbimap.create ();        Map.put ("1", "2");        Map.put ("3", "4");        System.out.println (Map.tostring ());        System.out.println (Map.inverse (). toString ());        Map.put ("5", "6");        System.out.println (Map.inverse (). toString ());    }

{1=2, 3=4}
{2=1, 4=3}
{2=1, 4=3, 6=5}

To this end, take a look at the last Guava Collection Class table.

Guava Pack Learning---Bimap

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.