gpt4 book ai didi

java - 使用索引值从 HashBasedTable(Guava 集合)中提取数据

转载 作者:行者123 更新时间:2023-12-01 11:36:46 24 4
gpt4 key购买 nike

我有一个返回 HashBasedTable 的方法(来自 Google 的 Guava 项目:https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Table)

我希望在创建该表后从该表中提取值。

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
/**
*
* @author yschellekens
*/
public class StackOverflow {

/**
*
* @return
*/
public static Table<Long, Long, String> getGeoTargeting() {
Table<Long, Long, String> weightedGraph = HashBasedTable.create();

weightedGraph.put(999_99_9999L, 999_99_9999L, "blabla");
return weightedGraph;
}


public static void main(String[] args) throws Exception {
Table<Long, Long, String> weightedGraph = HashBasedTable.create();
weightedGraph=getGeoTargeting();
System.out.println(weightedGraph.isEmpty());
}
}

输出:

run:
false
BUILD SUCCESSFUL (total time: 2 seconds)

我的问题是:如何从表中提取单个元素,例如(这允许我按索引提取元素):

get(int index) //as in array list

而不是像(这是 Javadoc 中 Hashbasetable 的唯一 get 方法)

get(Object rowKey, Object columnKey)
Returns the value corresponding to the given row and column keys, or null if no such mapping exists.

因为我想通过索引而不是值提取元素

提前致谢!

最佳答案

要将表中的所有值作为集合检索,请执行以下操作:

Collection values = weightedGraph.values()

要按索引访问返回集合中的任何值,请执行以下操作:

new ArrayList(values).get(index);

关于java - 使用索引值从 HashBasedTable(Guava 集合)中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com