Java 选择值时搜索键
我正在使用google table以如下格式存储集合:Java 选择值时搜索键,java,guava,Java,Guava,我正在使用google table以如下格式存储集合: import com.google.common.collect.HashBasedTable; import com.google.common.collect.Lists; import com.google.common.collect.Table; public class mapdemo { public static void main(String[] args) { final Table<
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
public class mapdemo {
public static void main(String[] args) {
final Table<String, String, List<String>> values = HashBasedTable
.create();
values.put("B1", "RtyName",
Lists.newArrayList("weepn", "weepfnb", "eedgeft", "xbteehy"));
values.put("B2", "EUOPName",
Lists.newArrayList("aaa", "bbbb", "ccc", "dddd"));
System.out.println(values.get("B1", "RtyName")); // prints the list
System.out.println(values.get("B2", "XXXName")); // prints the list
}
}
我想定制这个相同的程序,比如说如果列表中有任何一个值,我从下面的一对中选择lets say
"B1", "RtyName", Lists.newArrayList("weepn", "weepfnb", "eedgeft", "xbteehy"
我选择值weep n
,然后它应该打印RtyName和B1
类似地,假设我选择值aaa,那么它应该打印XXXName和B2
因此,请建议如何实现这一点,因为现在我想搜索值。没有直接的方法根据值获取键,因此您可以迭代表的所有单元格集(行),并检查每个列表是否包含值。如果您是为了大地图而做,那么您可以考虑使用BiMap
//要迭代的代码
for(final Cell<String, String, List<String>> row : values.cellSet()){
if(row.getValue().contains(val)){
System.out.println(//desired output);
}
for(最后一个单元格行:values.cellSet()){
if(row.getValue().contains(val)){
System.out.println(//所需输出);
}
比方说,我不明白你在问什么
for(final Cell<String, String, List<String>> row : values.cellSet()){
if(row.getValue().contains(val)){
System.out.println(//desired output);
}