Guava Learning: Guava Collection Tool-table interface

Source: Internet
Author: User

Recently learned the use of the next guava, here is a simple record of some commonly used and use of the tool class.

See the use of the table is really a bright, before the code has written a lot of map<string,map<string,string>> This format of code, this reading is very unfriendly, I don't even know what the key in the map is, but it's the context Association, and the presence of the table type completely solves the problem.

Table supports row, column, value we think of the map structure defined above as a single data table:

table<r,c,v> = = map<r,map<c,v>>

Let's take a look at a data sheet and write our code in conjunction with a data sheet:

S.N. Method & Description
1 Set<table.cell<r,c,v>> CellSet ()
Returns all row key/column key/value triples in the collection.
2 void Clear ()
Removes all mappings from the table.
3 map<r,v> column (C columnkey)
Returns a view of all mappings in a given column key.
4 Set<c> Columnkeyset ()
Returns a set of column keys that have one or more values in the table.
5 Map<c,map<r,v>> Columnmap ()
Returns a view of the mapped values for each column key associated with the row key.
6 Boolean contains (object RowKey, Object Columnkey)
Returns true if the table contains mappings to the specified row and column keys.
7 Boolean containscolumn (Object columnkey)
Returns true if the table contains a mapping with the specified column.
8 Boolean Containsrow (Object rowKey)
Returns true if the table contains a mapping relationship to the specified row key.
9 Boolean Containsvalue (Object value)
Returns true if the table contains a mapping with the specified value.
10 Boolean equals (Object obj)
Compares whether the specified object is equal to this table.
11 V get (Object RowKey, Object Columnkey)
Returns a value that corresponds to the given row and column key, and returns null if no such mapping exists.
12 int Hashcode ()
Returns the hash code in this table.
13 Boolean IsEmpty ()
Returns true if there are no mappings in the table.
14 V Put (R RowKey, C Columnkey, v value)
Associates the specified value with the specified key.
15 void Putall (table<? extends R,? extends C,? extends V> Table)
Copies all mappings from the specified table to this table.
16 V Remove (object RowKey, Object Columnkey)
If any, the mappings that are associated with the given key are deleted.
17 Map<c,v> row (R RowKey)
Returns a view of all mappings that contain the given row key.
18 Set<r> Rowkeyset ()
Returns a set of row keys that have one or more values in the table.
19 Map<r,map<c,v>> Rowmap ()
Returns a view of the mapped values for each of the associated row keys and key columns.
20 int size ()
Returns the number of value mapping relationships in the row key/column key/table.
21st Collection<v> VALUES ()
Returns all values, which may contain duplicate collections.

The following is a demo based on the table above

/** COMPANY:IBM, Microsoft, TCS * IBM, {101:mahesh, 102:ramesh, 103:suresh} *         Microsoft, {101:sohan, 102:mohan, 103:rohan} * TCS, {101:ram, 102:shyam, 103:sunil} *         * */        //Create a tableTable<string, String, string> employeetable =hashbasedtable.create (); //Initialize the table with employee detailsEmployeetable.put ("IBM", "101", "Mahesh"); Employeetable.put ("IBM", "102", "Ramesh"); Employeetable.put ("IBM", "103", "Suresh"); Employeetable.put ("Microsoft", "111", "Sohan"); Employeetable.put ("Microsoft", "Mohan", "the"); Employeetable.put ("Microsoft", "113", "Rohan"); Employeetable.put ("TCS", "121", "Ram"); Employeetable.put ("TCS", "102", "Shyam"); Employeetable.put ("TCS", "123", "Sunil"); //all row DataSystem.out.println (Employeetable.cellset ()); //All CompaniesSystem.out.println (Employeetable.rowkeyset ()); //all employee numbersSystem.out.println (Employeetable.columnkeyset ()); //all employee namesSystem.out.println (Employeetable.values ()); //all employee and employee numbers in the companySystem.out.println (Employeetable.rowmap ()); //Company and employee name corresponding to employee numberSystem.out.println (Employeetable.columnmap ()); //row+column the corresponding valueSystem.out.println (Employeetable.get ("IBM", "101")); //all of IBM's informationmap<string,string> ibmemployees = Employeetable.row ("IBM"); System.out.println ("List of IBM Employees");  for(Map.entry<string, string>Entry:ibmEmployees.entrySet ()) {System.out.println ("Emp Id:" + entry.getkey () + ", Name:" +Entry.getvalue ()); }        //all non-repeating keys in tableset<string> employers =Employeetable.rowkeyset (); System.out.print ("Employers:");  for(String employer:employers) {System.out.print (employer+ " ");        } System.out.println (); //get all companies and names with employee number 102map<string,string> Employermap = Employeetable.column ("102");  for(Map.entry<string, string>Entry:EmployerMap.entrySet ()) {System.out.println ("Employer:" + entry.getkey () + ", Name:" +Entry.getvalue ()); }

Run results

[(IBM,101) =mahesh, (ibm,102) =ramesh, (ibm,103) =suresh, (microsoft,111) =sohan, (microsoft,112) =mohan, (Microsoft,113 ) =rohan, (tcs,121) =ram, (tcs,102) =shyam, (tcs,123) =SUNIL][IBM, Microsoft, tcs][101, 102, 103, 111,, 113, 121, 123][mah Esh, Ramesh, Suresh, Sohan, Mohan, Rohan, Ram, Shyam, Sunil]{ibm={101=mahesh, 102=ramesh, 103=suresh}, Microsoft={111=soh An, 112=mohan, 113=rohan}, Tcs={121=ram, 102=shyam, 123=sunil}}{101={ibm=mahesh}, 102={ibm=ramesh, TCS=Shyam}, 103={ Ibm=suresh}, 111={microsoft=sohan}, 112={microsoft=mohan}, 113={microsoft=rohan}, 121={tcs=ram}, 123={TCS=Sunil}} Maheshlist of IBM employeesemp id:101, name:maheshemp id:102, Name:rameshemp id:103, Name:SureshEmployers:IBM Micro Soft TCS EMPLOYER:IBM, Name:RameshEmployer:TCS, Name:shyam

  

Guava Learning: Guava Collection Tool-table interface

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.