简体   繁体   English

java-如何在循环中在Hashset中找到值的交集?

[英]java- How to find intersection of values in Hashset in loop?

I wrote a program in Java where it retrieves records from a database table and stored them in a hashmap. 我用Java编写了一个程序,该程序从数据库表中检索记录并将其存储在哈希图中。

The keys and values of them are like the following: 它们的键和值如下所示:

Key(represent words)      Values(represent filename)

w1                          file1
w2                          file1
w3                          file2
w4                          file1
w5                          file2
w6                          file1,file2
............

The list goes on and on but this is just an idea of how it looks like.As you can see, there's no duplicate for words and they are unique. 列表不断增加,但这只是其外观的一个概念,正如您所看到的,单词没有重复且它们是唯一的。

Given that I have this hashmap info,I need to find the intersection of the key and it's next key and return the results of the intersection. 鉴于我有此哈希图信息,我需要找到键的交点和下一个键,并返回交点的结果。 The idea looks like this: 这个想法看起来像这样:

w1∩w2= file1
w2∩w3= empty
w3∩w4= empty

........and it keeps going until it reaches the finishes the final pair of keys in the hashmap. ........并且一直持续到哈希表中的最后一对密钥完成为止。

Since the pair of intersection results depends on the number of keys in the hashmap,I am guessing I'll need to use some loop to keep it iterating to return all the results. 由于这对交集的结果取决于哈希图中的键数,因此我猜我将需要使用一些循环来使其迭代以返回所有结果。

Is there a way on how to get the intersection of each subsequent keys and also a way that is optimize regardless the size of the hashmap? 有没有一种方法来获取每个后续​​键的交集,并且有一种无论哈希图大小如何都可以优化的方法?

I appreciate any suggestion. 我感谢任何建议。

Make a variable that will hold all those intersections. 制作一个将包含所有这些交点的变量。 In your loop retrieve 2 keys at a time. 在循环中一次检索2个键。 Compare each values of the 2 keys and if they are the same add the value to your intersection holder. 比较两个键的每个值,如果它们相同,则将值添加到您的交集持有人。 Repeat the steps until there is no more pairs. 重复这些步骤,直到没有更多的配对为止。

Here is the code. 这是代码。

Add this below your try/catch 在您的try / catch下面添加它

LinkedHashmap<String, Set<String>> intersectionMap = new LinkedHashmap<>();
if (map.keySet() != null) {
    String[] keys = map.keySet().toArray(new String[map.keySet().size()]);
    for (int i = 0; i < keys.length - 1; i++) {
        String key1 = keys[i];
        String key2 = keys[i + 1];
        TreeSet<String> interSection = intersection(map.get(key1), map.get(key2));
        intersectionMap.put(key1 + "∩" + key2, interSection);
    }
}

Add this helper method. 添加此辅助方法。 This method will find the intersection of the two sets. 此方法将找到两个集合的交集。 This will be the key in solving your problem. 这将是解决您的问题的关键。

public static TreeSet<String> intersection(TreeSet<String> setA, TreeSet<String> setB) {
    // An optimization to iterate over the smaller set
    if (setA.size() > setB.size()) {
        return intersection(setB, setA);
    }
    TreeSet<String> results = new TreeSet<>();
    for (String element : setA) {
        if (setB.contains(element)) {
            results.add(element);
        }
    }
    return results;
}

Yet another version with set operations: 具有设置操作的另一个版本:

Map<String>, Set<String>> intersections(Map<String, TreeSet<String>> map) {
    Map<String>, Set<String>> result = new TreeMap<>();

    List<String> words = new ArrayList<>(map.keySet());
    words.sort();
    for (int i = 0; i < words.size() - 1; ++i) {
        String wordI = words.get(i);
        Set<String> valueI = map.get(wordI);
        for (int j = i + 1, j < words.size(); ++j) {
            String wordJ = words.get(j);
            Set<String> valueJ = map.get(wordJ);

            String word = wordi + "∩" + words[j];
            Set<String> value = new TreeSet<>(valueI);
            value.retainAll(valueJ);
            result.put(word, value);
        }
    }
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM