java - 迭代二维列表并将元素放入哈希表中

标签 java list hashtable closest-points

我有一个点坐标列表,想要迭代一个 2D 列表,然后处理哈希函数中的元素以生成哈希表的 key 。 我有点难以迭代 List> 点,还有如何将坐标 (x,y) 作为值传递给哈希表(键,值)?

public static List<List<Integer>> myMethod(int k, List<List<Integer>> points){

    Hashtable  pt = new Hashtable();

    for (int i = 0; i <points.size(); i++)
    {
        for (int j = 0; j < points.get(i).size(); j++)
        {
            Integer x = points.get(i);
            Integer y = points.get(j);
            pt.put(hashfunction( x, y), points.get(i));
        } 
    }

    //return list of pairs ;
}

最佳答案

for (int i = 0; i <points.size(); i++) {
        List<Integer> in = points.get(i);
        for (int j = 0; j < in.size() - 1; j++) {
            Integer x = in.get(j);
            Integer y = in.get(j + 1);
            pt.put(hashfunction(x, y), points.get(i));
        } 
}

关于java - 迭代二维列表并将元素放入哈希表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045336/

相关文章:

java - 显示由星号组成的字母的问题

java - 是否有工具可以在不加载完整 hprof 文件的情况下分析大型 Java 堆转储?

java - cucumber - java.lang.NoClassDefFoundError

c# - 使用 "private set;"阻止其他类添加到列表

python - 在 Python 中,如何将列表中的所有元素添加到集合中?

c - 将 linux.words (/usr/share/dict/words) 中的前 10 个单词写入数组

perl - 将散列中的值相加 (Perl)

java - JSON.解析: Unexpected character error in Javascript

java - 我需要数据结构来有效处理日期

c++ - 哈希表 : Double hashing when the second hash function returns a multiple of the table size