主页 > 软件开发  > 

Table和HashBasedTable的使用案例

Table和HashBasedTable的使用案例

-------------------

1.普通使用

package org.example.testhashbasedtable; import com.google mon.collect.HashBasedTable; import com.google mon.collect.Table; import java.util.Map; public class TestHashBasedTable { public static void main(String[] args) { Table<String, Integer, Float> table = HashBasedTable.create(); table.put("a", 1, 1.1f); table.put("a", 2, 2.2f); System.out.println(table.row("a")); System.out.println(table.column(2)); System.out.println(table.row("b")); System.out.println(table.column(1)); Map<String, Float> column = table.column(1); } } /* {1=1.1, 2=2.2} {a=2.2} {} {a=1.1} 感悟:通过r或者c进行查找,得到的是一个包含其它2个字段的map */

2.putAll 和 遍历

package org.example.testhashbasedtable; import com.google mon.collect.HashBasedTable; import com.google mon.collect.Table; public class TestHashBasedTable { public static void main(String[] args) { // 数据源1 Table<String, Integer, Float> table = HashBasedTable.create(); table.put("a", 1, 1.1f); table.put("a", 2, 2.2f); // 数据源2 Table<String, Integer, Float> table2 = HashBasedTable.create(); table2.put("a", 11, 1.1f); table2.put("aa", 2, 2.2f); // 数据源1添加到数据源2中 table2.putAll(table); System.out.println(table2); // 遍历所有的条目 for (Table.Cell<String, Integer, Float> cell : table2.cellSet()) { System.out.println(cell.getRowKey() + " " + cell.getColumnKey() + " " + cell.getValue()); } } } /* {a={11=1.1, 1=1.1, 2=2.2}, aa={2=2.2}} a 11 1.1 a 1 1.1 a 2 2.2 aa 2 2.2 */

行、列、值 就是excel中的表格的抽象。

3.putAll的话,相同行列的是会被覆盖。相当于2个Excel表的融合。

package org.example.testhashbasedtable; import com.google mon.collect.HashBasedTable; import com.google mon.collect.Sets; import com.google mon.collect.Table; import java.util.Set; public class Test2 { public static void main(String[] args) { // table1 Table<Integer, Integer, Set<Integer>> table = HashBasedTable.create(); table.put(1, 2, Sets.newHashSet(4, 5, 6)); // table2 Table<Integer, Integer, Set<Integer>> table2 = HashBasedTable.create(); table2.put(1, 2, Sets.newHashSet(6, 7, 8)); // 把table2加到table1中 table.putAll(table2); // 发现相同行列的肯定是被覆盖了 System.out.println(table); } } /* {1={2=[8, 6, 7]}} */

标签:

Table和HashBasedTable的使用案例由讯客互联软件开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Table和HashBasedTable的使用案例