Templates by BIGtheme NET

Google Guava Collections – MultiMap

Most of the time we are crazy about Map in java collections, In many projects extensively
use lists & maps.

Continue to my tour with Guava collections, MultiMap
is one we should know. So let us starts with,
What is MultiMap?
A collection to map single key to either single or multiple values.

 Multimap addresses = ArrayListMultimap.create(); 

let me give you some example, to clear. Suppose we want to store address entity.
As we know person can have multiple addresses, like permanent, current address, mailing address,
office hours contact address(like office address).

Now let us think about, some collection that holds multiple values for single address key.
Yes, Multiset is one…helps us in this type of situations.

Multimap addresses = ArrayListMultimap.create();
addresses.put("adress", "address1");
addresses.put("adress", "address2");
addresses.put("adress", "address3");
//Print the address.
System.out.println("Customer Address is: "+ inBranchCustomer.getAddresses()); 

Out Put :

Customer Address is: {adress=[address1, address2, address3]} 

What about null vales or keys?
Let talk about null keys and values in MultiMap, interesting…
Questions like how many null values are allowed? or how many null keys are allowed?, simple but quite interesting. Yammy…let us have simple example.

Multimap addresses = ArrayListMultimap.create();
addresses.put("adress", "address1");
addresses.put("adress", "address2");
addresses.put("adress", "address3");		
addresses.put("adress", null); //adding first null value
addresses.put("adress", null); //adding second null value
		
addresses.put(null, null); //Adding first null key and value 
addresses.put(null, null); //Adding second null key and value
addresses.put(null, "addressForNull"); //Adding third null key and some address value

Out Put :

Customer Address is: 
{null=[null, null, addressForNull], adress=[address1, address2, address3, null, null]}

I am just summering, null is allowed as key, and this null key can holds multiple null’s as values.
Nice one :-)

Basic Operations :
Let us see some basic operations, retrieve, update and delete values in MultiMap.

Retrieve :

Multimap addresses = ArrayListMultimap.create();
addresses.put("adress", "address1");
addresses.put("adress", "address2");
addresses.put("adress", "address3");
		
addresses.put("dependentAdress", "dependentAdress1"); //First dependents address
addresses.put("dependentAdress", "dependentAdress2"); //Second dependents address
		
final Set addressKeyMultiSet = addresses.keySet();		
final Iterator addressKayMultiSetItr = addressKeyMultiSet.iterator();
while(addressKayMultiSetItr.hasNext()){
	final String key = addressKayMultiSetItr.next();
	System.out.println("Key is: " + key + "Associated Value is:" + addresses.get(key));
	final Collection valueList = addresses.get(key);
			
	for(String s: valueList){
		System.out.println("Value is :" + s );
	}
}
// Multiset keys(); this will returns all the keys and no.of times those keys are repeated.		
System.out.println( "addresses.keys() >>> " + addresses.keys()); 
// Set keySet(); this will return unique key set.
System.out.println( "addresses.keySet() >>> " + addresses.keySet()); 

Out Put :

 Key is: adressAssociated Value is:[address1, address2, address3]
Value is :address1
Value is :address2
Value is :address3
Key is: dependentAdressAssociated Value is:[dependentAdress1, dependentAdress2]
Value is :dependentAdress1
Value is :dependentAdress2
addresses.keys() >>> [adress x 3, dependentAdress x 2]
addresses.keySet() >>> [adress, dependentAdress] 

Create, Update & Delete on MultiMap :
Now, let us see different ways of creating MultiMap,

// Key behave like HashMap And Values behave like ArrayList
Multimap addresses = ArrayListMultimap.create();
// Key behave like HashMap And Values behave like HashSet
Multimap addresses = HashMultimap.create();
// Key behave like LinkedHashMap* And Values behave like LinkedList*
Multimap addresses = LinkedListMultimap.create();
// Key behave like TreeMap And Values behave like TreeSet
Multimap addresses = TreeMultimap.create();

Complete example that helps to understand,
MultiMapExample.java

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultiMapExample {
	public static void main(String[] args) {
		Multimap phoneNumbers = ArrayListMultimap.create();
		phoneNumbers.put("phoneNumber", "1111");
		phoneNumbers.put("phoneNumber", "2222");
		phoneNumbers.put("phoneNumber", "3333");

		Multimap addresses = ArrayListMultimap.create();
		addresses.put("adress", "address1");
		addresses.put("adress", "address2");
		addresses.put("adress", "address3");		
		addresses.put("adress", null); //adding first null value
		addresses.put("adress", null); //adding second null value
		
		addresses.put(null, null); //Adding first null key and value 
		addresses.put(null, null); //Adding second null key and value
		addresses.put(null, "addressForNull"); //Adding third null key and some address value
		

		Customer branchCustomer = new Customer("B102101", "Jhon", phoneNumbers,
				addresses);

		System.out
				.println("MultiMap Examples >> Retrive all Customer Detailsn====================================================");
		printCustomerDetails(branchCustomer);

		// Customer requested for address and phone numbers update.
		Multimap pNumbers = branchCustomer.getPhoneNumbers();
		pNumbers.remove("phoneNumber", "2222");
		pNumbers.put("phoneNumber", "4444");
		System.out
				.println("After Update Phone Number >> Prinlt all Customer Detailsn====================================================");
		printCustomerDetails(branchCustomer);
	}

	final static void printCustomerDetails(final Customer inBranchCustomer) {
		System.out.println("Customer Id is: " + inBranchCustomer.getId());
		System.out.println("Customer Name is: " + inBranchCustomer.getName());
		System.out.println("Customer Phone Numbers is: "
				+ inBranchCustomer.getPhoneNumbers());
		System.out.println("Customer Address is: "
				+ inBranchCustomer.getAddresses());
	}
}

class Customer {
	private String id;
	private String name;
	private Multimap phoneNumbers;
	private Multimap addresses;

	public Customer(final String inId, final String inName,
			final Multimap inPhoneNumber,
			final Multimap inAddress) {
		id = inId;
		name = inName;
		phoneNumbers = inPhoneNumber;
		addresses = inAddress;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Multimap getPhoneNumbers() {
		return phoneNumbers;
	}

	public void setPhoneNumbers(Multimap phoneNumbers) {
		this.phoneNumbers = phoneNumbers;
	}

	public Multimap getAddresses() {
		return addresses;
	}

	public void setAddresses(Multimap addresses) {
		this.addresses = addresses;
	}
}

Select and “Run As” -> Java Application,

Out Put :

MultiMap Examples >> Retrive all Customer Details
====================================================
Customer Id is: B102101
Customer Name is: Jhon
Customer Phone Numbers is: {phoneNumber=[1111, 2222, 3333]}
Customer Address is: {null=[null, null, addressForNull], adress=[address1, address2, address3, null, null]}
After Update Phone Number >> Prinlt all Customer Details
====================================================
Customer Id is: B102101
Customer Name is: Jhon
Customer Phone Numbers is: {phoneNumber=[1111, 3333, 4444]}
Customer Address is: {null=[null, null, addressForNull], adress=[address1, address2, address3, null, null]} 

*** Venkat – Happy leaning ****