Templates by BIGtheme NET

Google Guava Collections – BiMap

Continue to my tour with Guava Collections, one collection that Enforcing uniqueness
in Map values
is BiMap( Bidirectional Map).

Basic questions here,
Is Bimap holds duplicate keys? – answer is No
Is Bimap holds duplicate values? – answer is No

Nice, key is unique and value is also unique. obviously is is 1-1 Mapping.
More over, this help to handle duplicate values.

If we try to put duplicate values, we have option either to throw exception or override
the actual key and value.

let me give you some back ground about example, bankToCustomer
BiMap class is having two entries.
welsforgoOak as key with value customer1 And
welsforgoSan as key with value customer2

final BiMap bankToCustomer = HashBiMap.create();
bankToCustomer.put(welsforgoOak, customer1);
bankToCustomer.put(welsforgoSan, customer2);

put() Vs forcePut() :
let us see, how to update BiMap.
If we use put() method to put new entry with key welsforgoSan with value
as customer1 . As we know, this value is already existed with key welsforgoOak – this raise IllegalArgumentException.

If we use forcePut() method, then overrides the key.

bankToCustomer.put(welsforgoSan, customer1); //IllegalArgumentException exception 
bankToCustomer.forcePut(welsforgoSan, customer1);
System.out.println("bankToCustomer.get(welsforgoSan) is: " + bankToCustomer.get(welsforgoSan)); // customer1
System.out.println("bankToCustomer.get(welsforgoOak) is: " + bankToCustomer.get(welsforgoOak)); // null

Another interesting method in Bimap is inverse(), let use see how this works.
In same example, If we required customerToBank Bimap. Simply use inverse() method.
This will do everything for us.

final BiMap customerToBank = bankToCustomer.inverse();

Here we can ensure, any customer mapped to one bank branch (1-1) . reversal also applicable.
Even if application is try to map to multiple bank branches. As we are using Bimap, that will not be allowed.

BiMapExample.java

import java.util.Map;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

public class BiMapExample {
	public static void main(String[] args) {
		final BiMap bankToCustomer = HashBiMap.create();

		// Create Banks
		final Bank welsforgoOak = new Bank("welsforgoOak", "WelsForgo Oakland",
				"OAK");
		final Bank welsforgoSan = new Bank("welsforgoSan",
				"WelsForgo Sanfrancisco", "SAN");
		final Bank welsforgoAus = new Bank("welsforgoAus",
				"WelsForgo Australia", "AUS");
		final Bank swissOak = new Bank("swissOak", "Swiss Oakland", "OAK");
		final Bank swissSan = new Bank("swissSan", "Swiss Sanfrancisco", "SAN");
		final Bank swissAus = new Bank("swissAus", "Swiss Australia", "AUS");

		// Create Customers
		final BankCustomer customer1 = new BankCustomer("C1", "Jhon", "OAK");
		final BankCustomer customer2 = new BankCustomer("C2", "Smith", "OAK");
		final BankCustomer customer3 = new BankCustomer("C3", "Jenny", "SAN");
		final BankCustomer customer4 = new BankCustomer("C4", "Peter", "SAN");
		final BankCustomer customer5 = new BankCustomer("C5", "Stuart", "AUS");
		final BankCustomer customer6 = new BankCustomer("C6", "Alex", "AUS");

		bankToCustomer.put(welsforgoOak, customer1);
		bankToCustomer.put(welsforgoSan, customer2);
		bankToCustomer.put(welsforgoAus, customer3);
		bankToCustomer.put(swissOak, customer4);
		bankToCustomer.put(swissSan, customer5);
		bankToCustomer.put(swissAus, customer6);

		System.out.println("bankToCustomer >> n==================");
		printBankToCustomerDetails(bankToCustomer);
		
		final BiMap customerToBank = bankToCustomer
				.inverse();
		System.out.println("ncustomerToBank >> n==================");
		printCustomerToBankDetails(customerToBank);
		
		System.out.println("nUpdate BitMap with forcePut() Method");
		System.out.println("======================================");
		System.out.println("bankToCustomer.get(welsforgoSan) is: " + bankToCustomer.get(welsforgoSan).getName());
		System.out.println("bankToCustomer.get(welsforgoOak) is: " + bankToCustomer.get(welsforgoOak).getName());
		//bankToCustomer.put(welsforgoSan, customer1); //IllegalArgumentException exception 
		bankToCustomer.forcePut(welsforgoSan, customer1);	
		
		System.out.println("bankToCustomer.get(welsforgoSan) is: " + bankToCustomer.get(welsforgoSan));
		System.out.println("bankToCustomer.get(welsforgoOak) is: " + bankToCustomer.get(welsforgoOak));
	}
	
	final static void printBankToCustomerDetails(final BiMap inBiMap){
		for (final Map.Entry entry : inBiMap.entrySet()) {
            final Bank bank = entry.getKey();
            final BankCustomer customer = entry.getValue();            
            System.out.println("Bank is: "+ bank.getName() + " >>>>>> Customer is: " + customer.getName());
        }
	}
	
	final static void printCustomerToBankDetails(final BiMap inBiMap){
		for (final Map.Entry entry : inBiMap.entrySet()) {            
            final BankCustomer customer = entry.getKey();
            final Bank bank = entry.getValue();
            System.out.println("Customer is: " + customer.getName() + " >>>>>> Bank is: "+ bank.getName() );
        }
	}
}

class BankCustomer {
	private String id;
	private String name;
	private String accType;

	public BankCustomer(final String inId, final String inName,
			final String inAccType) {
		id = inId;
		name = inName;
		accType = inAccType;
	}

	public String getName() {
		return name;
	}

}

class Bank {
	private String id;
	private String name;
	private String branch;

	public Bank(final String inId, final String inName, final String inBranch) {
		id = inId;
		name = inName;
		branch = inBranch;
	}

	public String getName() {
		return name;
	}
}

Select and “Run as” -> “Java Application”
Out Put :

bankToCustomer >> 
==================
Bank is: Swiss Australia >>>>>> Customer is: Alex
Bank is: WelsForgo Oakland >>>>>> Customer is: Jhon
Bank is: WelsForgo Australia >>>>>> Customer is: Jenny
Bank is: Swiss Sanfrancisco >>>>>> Customer is: Stuart
Bank is: Swiss Oakland >>>>>> Customer is: Peter
Bank is: WelsForgo Sanfrancisco >>>>>> Customer is: Smith

customerToBank >> 
==================
Customer is: Alex >>>>>> Bank is: Swiss Australia
Customer is: Jhon >>>>>> Bank is: WelsForgo Oakland
Customer is: Jenny >>>>>> Bank is: WelsForgo Australia
Customer is: Stuart >>>>>> Bank is: Swiss Sanfrancisco
Customer is: Peter >>>>>> Bank is: Swiss Oakland
Customer is: Smith >>>>>> Bank is: WelsForgo Sanfrancisco

Update BitMap with forcePut() Method
======================================
bankToCustomer.get(welsforgoSan) is: Smith
bankToCustomer.get(welsforgoOak) is: Jhon
bankToCustomer.get(welsforgoSan) is: BankCustomer@11f46b16
bankToCustomer.get(welsforgoOak) is: null 

*** Venkat – Happy leaning ****