教程列表
- Guava Optional类详解-处理null值
- Guava Preconditions类-检查参数
- Guava Ordering类处理排序功能
- Guava Objects类详解
- Guava Range类-范围处理
- Guava Throwables类-异常处理
- Guava 集合处理-Multiset接口
- Guava集合处理- Bimap接口
- Guava集合工具类-Table接口映射处理
- Guava字符串连接-Joiner类
- Guava字符串分割处理-Spiltter类
- Guava CharMatcher类处理JAVA char类型值
- Guava ASCII字符转换 CaseFormat类
- Guava处理java byte类型工具类-Bytes类
- Guava 处理java short实用工具类-Shorts类
- Guava处理java int实用工具类-Ints类
- Guava Longs类-long的实用工具类
- Guava Floats类-float基本类型的实用工具类
- Guava Doubles类-double基本类型的实用工具类
- Guava Chars类-char类型的实用工具类
- Guava Booleans类-布尔型基本的实用工具类
- Guava IntMath类-提供整型的Math实用方法
- Guava LongMath类-long基础类型的Math实用方法
- Guava Multimap类-映射到多个值
- Guava cache缓存工具类
开源项目
相关文章
更多最近更新
更多Guava集合工具类-Table接口映射处理
2019-04-23 23:08|来源: 网路
Table代表一个特殊的映射,其中两个键可以在组合的方式被指定为单个值。它类似于创建映射的映射。
接口声明
以下是 com.google.common.collect.Table<R,C,V> 接口的声明:
@GwtCompatiblepublic interface Table<R,C,V>
接口方法
S.N. | 方法 & 描述 |
---|---|
1 | Set<Table.Cell<R,C,V>> cellSet() 返回集合中的所有行键/列键/值三元组。 |
2 | void clear() 从表中删除所有映射。 |
3 | Map<R,V> column(C columnKey) 返回在给定列键的所有映射的视图。 |
4 | Set<C> columnKeySet() 返回一组具有表中的一个或多个值的列键。 |
5 | Map<C,Map<R,V>> columnMap() 返回关联的每一列键与行键对应的映射值的视图。 |
6 | boolean contains(Object rowKey, Object columnKey) 返回true,如果表中包含与指定的行和列键的映射。 |
7 | boolean containsColumn(Object columnKey) 返回true,如果表中包含与指定列的映射。 |
8 | boolean containsRow(Object rowKey) 返回true,如果表中包含与指定的行键的映射关系。 |
9 | boolean containsValue(Object value) 返回true,如果表中包含具有指定值的映射。 |
10 | boolean equals(Object obj) 比较指定对象与此表是否相等。 |
11 | V get(Object rowKey, Object columnKey) 返回对应于给定的行和列键,如果没有这样的映射存在值,返回null。 |
12 | int hashCode() 返回此表中的哈希码。 |
13 | boolean isEmpty() 返回true,如果表中没有映射。 |
14 | V put(R rowKey, C columnKey, V value) 关联指定值与指定键。 |
15 | void putAll(Table<? extends R,? extends C,? extends V> table) 复制从指定的表中的所有映射到这个表。 |
16 | V remove(Object rowKey, Object columnKey) 如果有的话,使用给定键相关联删除的映射。 |
17 | Map<C,V> row(R rowKey) 返回包含给定行键的所有映射的视图。 |
18 | Set<R> rowKeySet() 返回一组行键具有在表中的一个或多个值。 |
19 | Map<R,Map<C,V>> rowMap() 返回关联的每一行按键与键列对应的映射值的视图。 |
20 | int size() 返回行键/列键/表中的值映射关系的数量。 |
21 | Collection<V> values() 返回所有值,其中可能包含重复的集合。 |
Table 例子
选择使用任何编辑器创建以下java程序在 C:/> Guava
GuavaTester.java
import java.util.Map; import java.util.Set; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; public class GuavaTester { public static void main(String args[]){ //Table<R,C,V> == Map<R,Map<C,V>> /* * 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 table Table<String, String, String> employeeTable = HashBasedTable.create(); //initialize the table with employee details employeeTable.put("IBM", "101","Mahesh"); employeeTable.put("IBM", "102","Ramesh"); employeeTable.put("IBM", "103","Suresh"); employeeTable.put("Microsoft", "111","Sohan"); employeeTable.put("Microsoft", "112","Mohan"); employeeTable.put("Microsoft", "113","Rohan"); employeeTable.put("TCS", "121","Ram"); employeeTable.put("TCS", "122","Shyam"); employeeTable.put("TCS", "123","Sunil"); //get Map corresponding to IBM Map<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()); } //get all the unique keys of the table Set<String> employers = employeeTable.rowKeySet(); System.out.print("Employers: "); for(String employer: employers){ System.out.print(employer + " "); } System.out.println(); //get a Map corresponding to 102 Map<String,String> EmployerMap = employeeTable.column("102"); for(Map.Entry<String, String> entry : EmployerMap.entrySet()){ System.out.println("Employer: " + entry.getKey() + ", Name: " + entry.getValue()); } } }
验证结果
使用javac编译器编译如下类
C:\Guava>javac GuavaTester.java
现在运行GuavaTester看到的结果
C:\Guava>java GuavaTester
看到结果。
List of IBM Employees Emp Id: 102, Name: Ramesh Emp Id: 101, Name: Mahesh Emp Id: 103, Name: Suresh Employers: IBM TCS Microsoft Employer: IBM, Name: Ramesh
相关问答
更多使用Guava ForwardingList装饰集合的意外行为(Unexpected behaviour decorating a collection using Guava ForwardingList)
这种模式可行的唯一方法是,在创建装饰器后再也不会再次引用originalList 。 ForwardingList可能无法控制originalList发生的事情。 (没有装饰者可以。) 总的来说,你应该做的是创建一个工厂方法,它返回一个全新的装饰列表,永远不会让你访问原始列表。 The only way this pattern is going to work is if you never reference originalList again after you've created th
...
Guava映射存储类实例作为键和int值(Guava map storing class instance as key and int value)
ClassToInstanceMap适用于想要执行classRanking.put(FooOne.class, new FooOne()) ,即值将是键的实例。 但是0不是FooOne一个实例,它只是一个Integer 。 你只需要一个普通的Map<Class<?>, Integer> ,所以使用HashMap或其他东西。 ClassToInstanceMap is for when you want to do stuff like classRanking.put(FooOne.class,
...
集合类和IDisposable接口(Collection class and IDisposable interface)
还是GC自动执行,我不应该打扰它。 这个。 除非您拥有非托管资源(直接或通过引用其他可丢弃的其他资源),否则几乎肯定不会实现IDisposable 。 你目前的实现只会自称: public void Dispose()
{
this.Dispose();
}
...所以假设你不想调用this.Dispose() ,当Dispose()被调用时你想要做什么? 这不像处置导致垃圾收集 - 所以你想采取什么行动? 如果答案是“无”,那么你可能不应该实现IDisposable 。 (这里的例外是
...
是否有针对Guava MultiSet和Table概念的scala替代品?(Is there a scala replacement for Guava MultiSet and Table concepts?)
您可以使用Map[(R, C), V]而不是Table<R, C, V>和Map[T, Int]而不是Multiset<T> 。 您还可以向Map[T, Int]添加辅助方法Map[T, Int]如下所示: implicit class Multiset[T](val m: Map[T, Int]) extends AnyVal {
def setAdd(e: T, i: Int = 1) = {
val cnt = m.getOrElse(e, 0) + i
if (cnt
...
使用table guava for hashbasedTable(Using table guava for hashbasedTable)
番石榴贡献者在这里。 不要使用构造函数,请使用HashBasedTable.create()工厂方法。 (没有参数,或与expectedRows和expectedCellsPerRow 。) 使用table.put("A100", "B100", 5) ,就像使用两个键的Map一样。 Guava contributor here. Don't use the constructor, use the HashBasedTable.create() factory method. (With no
...
用FluentNHibernate映射接口和具体类(Mapping an interface and concrete class with FluentNHibernate)
你想用什么样的继承策略来映射? 按类或按类分层结构? 您当前的映射意味着每个班级的表格。 无论哪种方式,我认为出了问题。 Object_id是外键名称,它应该通过查看父类的名称来构建。 我的猜测是,我们不把界面视为“父母”。 首先,我建议你在我们的问题列表中提出问题,或者打开邮件列表。 那会在我们的雷达上多一点。 其次,您可以尝试明确指定列名,但我不知道可能会对我提到的父问题产生什么影响。 要指定它,你只需要做KeyColumn("Id") 。 What kind of inheritance s
...
在guava的AbstractService类中实现stop()(stop() implementation in guava's AbstractService class)
在RUNNING状态下调用stop()时,状态将更改为STOPPING并doStop()方法。 之后再次调用stop()不应该做任何事情。 doStop()实现应该调用notifyStopped() ,它将状态从STOPPING更改为TERMINATED 。 When you call stop() in the RUNNING state, the state is changed to STOPPING and the doStop() method gets called. Calling
...
使用Supplier接口的Guava Lazy Collection(Guava Lazy Collection using Supplier interface)
根据与Louis Wasserman的讨论,我决定接受他的建议并使用迭代器。 以下解决方案似乎对我有用。 class MySupplier<T> implements Supplier<T> {
private T s;
public MySupplier( T s ) {
this.s = s;
}
public T get() {
System.out.println( String.format( "Lazy loading '
...
如何处理番石榴表中的碰撞?(How to handle collisions in guava table?)
Table行为就像一个Map ,即第二个put覆盖了第一个。 你需要的是一种Multimap 。 对于一个已经存在的问题 ,已经有一个问题 ,投票给它。 目前,您可以使用 Table<Integer, Integer, SomeCollection<MyBean>>
或者a Map<SomePair<Integer, Integer>, MyBean>
我推荐后者,因为组合键比处理“多”的东西简单得多。 如果你今天感觉很酷,你甚至可以使用 Map<Long, MyBean>
只是不要告诉任何
...
实现特定接口的所有类的对象的集合(Collection of objects of all classes which implement a particular interface)
不知道你想要什么,这很难说。 但无论如何,是的,你可以创建一个集合,但为了跟踪和存储对TestInterface对象的引用,你必须控制它们的创建。 最简单的方法是使用TestInterfaceFactory (请参阅AbstractFactory模式),这也是使用实例引用保存store集合的好地方。 Not knowing what you want this for, it's hard to tell. But anyway, yes you can create a collection b
...