Templates by BIGtheme NET

Google Guava Collections – Table

Continue to my tour with Guava Collections, Table is one of the use full Guava collection,
First question is First.
When should we use Table Collection?
Suppose in some project, we are trying to index on more than one key at a time.

Let me explain this, Suppose relation between Bank – Customer is many to many(M-M).
1) Bank can holds different Customer details.
2) Same Customer can have multiple accounts in different banks.

To hold these, data. One collection that help is Table.

final Table bankAndCustomer = HashBasedTable.create(); //Table obj creation

Here Bank – object holds bank information & Customer Object holds customer information.
But, I want to know the bank address of the Customer?
Here I have two Indexes, one is bank id’s should get from Customer Object.
And using those bank id’s, i can get bank addresses from bank object – this is second key.

These type of information, we can get just like that. if that information is with Table collection.

3

TableBankAndCustomerExample.java

package com.venkatjavasource;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class TableBankAndCustomerExample {
	public static void main(String[] args) {

		// Table
		final Table bankAndBankCustomerTable = HashBasedTable
				.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");

		// Insert First row values into table
		bankAndBankCustomerTable.put(welsforgoOak, customer1,
				"welsforgoOak-> customer1");
		bankAndBankCustomerTable.put(welsforgoOak, customer3,
				"welsforgoOak-> customer3");
		bankAndBankCustomerTable.put(welsforgoOak, customer5,
				"welsforgoOak-> customer5");

		// Insert Second row values into table

		bankAndBankCustomerTable.put(welsforgoSan, customer2,
				"welsforgoSan-> customer2");
		bankAndBankCustomerTable.put(welsforgoSan, customer4,
				"welsforgoSan-> customer4");
		bankAndBankCustomerTable.put(welsforgoSan, customer6,
				"welsforgoSan-> customer6");

		// Insert Third row values into table
		bankAndBankCustomerTable.put(welsforgoAus, customer1,
				"welsforgoAus-> customer1");
		bankAndBankCustomerTable.put(welsforgoAus, customer3,
				"welsforgoAus-> customer3");
		bankAndBankCustomerTable.put(welsforgoAus, customer5,
				"welsforgoAus-> customer5");

		// Insert Fourth row values into table
		bankAndBankCustomerTable.put(swissOak, customer2,
				"swissOak-> customer2");
		bankAndBankCustomerTable.put(swissOak, customer4,
				"swissOak-> customer4");
		bankAndBankCustomerTable.put(swissOak, customer6,
				"swissOak-> customer6");

		// Insert Fifth row values into table
		bankAndBankCustomerTable.put(swissSan, customer1,
				"swissSan-> customer1");
		bankAndBankCustomerTable.put(swissSan, customer3,
				"swissSan-> customer3");
		bankAndBankCustomerTable.put(swissSan, customer5,
				"swissSan-> customer5");

		// Insert Sixth row values into table
		bankAndBankCustomerTable.put(swissAus, customer2,
				"swissAus-> customer2");
		bankAndBankCustomerTable.put(swissAus, customer4,
				"swissAus-> customer4");
		bankAndBankCustomerTable.put(swissAus, customer6,
				"swissAus-> customer6");

		// Get Bank names, where Customer1 is having account
		System.out
				.println("Bank names, where Customer1 is having account >> n==================n"
						+ bankAndBankCustomerTable.column(customer1));
		// Get List of Customers, Bank welsforgoAus is having.
				System.out
						.println("Customer names, Bank welsforgoAus is having account >> n==================n"
								+ bankAndBankCustomerTable.row(welsforgoAus));

// All Bank Names, where Customer1 having account.
final Set<Bank> customer1ToBankKeySet = bankAndBankCustomerTable.column(customer1).keySet();
final Iterator itr = customer1ToBankKeySet.iterator();
System.out
.println("nAll Bank Names, where Customer1 having account >> n==================");
while(itr.hasNext()){
	System.out.println("Banks Address is: " + ((Bank)itr.next()).getName());
}
	}
}

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;
	}
}

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;
	}
}

Out Put :

 Bank names, where Customer1 is having account >> 
==================
{com.venkatjavasource.Bank@7a5e832b=welsforgoOak-> customer1, com.venkatjavasource.Bank@56ec1e6f=welsforgoAus-> customer1, com.venkatjavasource.Bank@3f66cb16=swissSan-> customer1}
Customer names, Bank welsforgoAus is having account >> 
==================
{com.venkatjavasource.BankCustomer@2bbd83d=welsforgoAus-> customer3, com.venkatjavasource.BankCustomer@7a718e31=welsforgoAus-> customer5, com.venkatjavasource.BankCustomer@52f5bad0=welsforgoAus-> customer1}

All Bank Names, where Customer1 having account >> 
==================
Banks Address is: WelsForgo Australia
Banks Address is: Swiss Sanfrancisco
Banks Address is: WelsForgo Oakland

Basic example that helps to understands Table collection,
table-2
This example, demonstrate how the book object table is implemented,
TableExample.java

import java.util.Map;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class TableExample {
	public static void main(String[] args) {
		
		//Table 
		final Table> n==================n" + bookTable);		

		System.out.println("bookTable.rowMap() >> n==================n" + bookTable.rowMap());
		System.out.println("bookTable.rowKeySet() >> n==================n" + bookTable.rowKeySet());
		System.out.println("bookTable.row('Row1') >> n==================n" + bookTable.row("Row1"));
		

		System.out.println("bookTable.columnMap() >> n==================n" + bookTable.columnMap());
		System.out.println("bookTable.columnKeySet() >> n==================n" + bookTable.columnKeySet());
		System.out.println("bookTable.row('Col1') >> n==================n" + bookTable.column("Col1"));
		
		System.out.println("bookTable.cellSet() >> n==================n" + bookTable.cellSet());
		
		//printBankToCustomerDetails(bookTable);
	}
	
	final static void printBookDetails(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 + " >>>>>> Customer is: " + customer);
        }
	}
}

Select “Run As” -> “Java Application”
Out Put:

Book Table Details >> 
==================
{Row5={Col3=Bill Venners}, Row4={Col4=Programming in Scala, Col1=B_105, Col2=Scala}, Row3={Col4=How to develop applications with out DB, Col1=B_103, Col2=NO SQL, Col3=M Fowler}, Row2={Col4=Test Driven Development, Col1=B_102, Col2=TDD, Col3=Kent Beck}, Row1={Col4=This helps to clean java code, more useful while doing re-factoring, Col1=B_101, Col2=Java Clean, Col3=M Fowler}}
bookTable.rowMap() >> 
==================
{Row5={Col3=Bill Venners}, Row4={Col4=Programming in Scala, Col1=B_105, Col2=Scala}, Row3={Col4=How to develop applications with out DB, Col1=B_103, Col2=NO SQL, Col3=M Fowler}, Row2={Col4=Test Driven Development, Col1=B_102, Col2=TDD, Col3=Kent Beck}, Row1={Col4=This helps to clean java code, more useful while doing re-factoring, Col1=B_101, Col2=Java Clean, Col3=M Fowler}}
bookTable.rowKeySet() >> 
==================
[Row5, Row4, Row3, Row2, Row1]
bookTable.row('Row1') >> 
==================
{Col4=This helps to clean java code, more useful while doing re-factoring, Col1=B_101, Col2=Java Clean, Col3=M Fowler}
bookTable.columnMap() >> 
==================
{Col3={Row5=Bill Venners, Row3=M Fowler, Row2=Kent Beck, Row1=M Fowler}, Col4={Row4=Programming in Scala, Row3=How to develop applications with out DB, Row2=Test Driven Development, Row1=This helps to clean java code, more useful while doing re-factoring}, Col1={Row4=B_105, Row3=B_103, Row2=B_102, Row1=B_101}, Col2={Row4=Scala, Row3=NO SQL, Row2=TDD, Row1=Java Clean}}
bookTable.columnKeySet() >> 
==================
[Col3, Col4, Col1, Col2]
bookTable.row('Col1') >> 
==================
{Row4=B_105, Row3=B_103, Row2=B_102, Row1=B_101}
bookTable.cellSet() >> 
==================
[(Row5,Col3)=Bill Venners, (Row4,Col4)=Programming in Scala, (Row4,Col1)=B_105, (Row4,Col2)=Scala, (Row3,Col4)=How to develop applications with out DB, (Row3,Col1)=B_103, (Row3,Col2)=NO SQL, (Row3,Col3)=M Fowler, (Row2,Col4)=Test Driven Development, (Row2,Col1)=B_102, (Row2,Col2)=TDD, (Row2,Col3)=Kent Beck, (Row1,Col4)=This helps to clean java code, more useful while doing re-factoring, (Row1,Col1)=B_101, (Row1,Col2)=Java Clean, (Row1,Col3)=M Fowler]

*** Venkat – Happy leaning ****