【问题标题】:Extracting data from a HashBasedTable (a Guava collection) using index values使用索引值从 HashBasedTable(Guava 集合)中提取数据
【发布时间】:2015-04-27 10:55:46
【问题描述】:

我有一个返回 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.

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

提前致谢!

【问题讨论】:

  • 阅读文档怎么样?还是你给我们的链接?
  • 我看过了,除了我提到的那个之外,没有看到其他 get 方法
  • 1.没有索引。 2.有两个键。 3. HashBasedTable 继承自Table,这是一个二维表。 4. 很可能不是您想要的。 5. 对于索引,使用ArrayList
  • 谢谢,我把这个当作答案,请发帖
  • @Dici,我真的不理解这些讨厌的 cmets

标签: java collections guava


【解决方案1】:

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

Collection values = weightedGraph.values()

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

new ArrayList(values).get(index);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2023-03-19
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多