获取这些键的值相等的键列表

发布于 2024-12-22 00:07:37 字数 298 浏览 2 评论 0 原文

我需要获取键列表,其中这些键的值与 HashMap 中的键相等。 例如,我的哈希图包含以下元素。

Key  Value
1    a,b
2    e,c
3    a,b
4    f
5    e,c
6    c

我们需要评估为

1,3 contains value (a,b)  
2,5 contains value (e,c)  
4   contains value (f)  
6   contains value (c)

Thx

I require to get the list of keys where the values are equal for those keys from a HashMap.
For example , my hashmap contains the below elements.

Key  Value
1    a,b
2    e,c
3    a,b
4    f
5    e,c
6    c

We need to evaluate as

1,3 contains value (a,b)  
2,5 contains value (e,c)  
4   contains value (f)  
6   contains value (c)

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

善良天后 2024-12-29 00:07:37

您可以反转哈希:构建一个新的哈希,其中键类型是当前映射值的类型,值类型是当前映射键类型的列表。

迭代当前地图的键,并将它们推入新地图中的正确插槽。然后您将获得所需的映射。

如果当前地图中的值现在无法直接比较,则您需要找到可以直接比较的表示形式。这完全取决于数据的性质。
一种简单的方法是对列表进行排序并使用它的 toString 表示形式作为新键。仅当底层对象的 toString 表示形式对于此目的来说是合理的时,这才有效。

You could invert your hash: build a new hash with the key type being the type of your current map's value, and the value type being a list of your current map's key type.

Iterate over your current map's keys, and push them to the right slot in your new map. You'll then have exactly the mapping you are asking for.

If the values in your current map aren't directly comparable right now, you'll need to find a representation that is. This depends completely on the nature of the data.
One simple approach is to sort the list and use it's toString representation as your new key. This only works if the toString representation of the underlying objects is sane for this purpose.

病毒体 2024-12-29 00:07:37

您可以创建其他映射,其中您的键用作值,值用作键。例如,如果您的源映射定义为 Map 创建映射 Map>。整数列表将包含具有特定值的键(来自源映射)。

You can create other map where your keys a used as values and values as keys. If for example your source map is defined as Map<Integer, String> create map Map<String, List<Integer>>. The list of integers will contain keys (from your source map) that have certain values.

百变从容 2024-12-29 00:07:37

基于 Mat 的答案,如果您需要经常执行此操作,请使用 Guava 或 Apache Commons Collections 中的双向映射类之一;例如 HashBiMapDualHashBidiMapDualTreeBidiMap。这些数据结构维护一对表示正向和反向映射的映射。

或者,对于一次性计算:

  1. Map.entries() 集合提取到数组中。
  2. 按值的顺序对数组进行排序。
  3. 迭代数组,并提取后续条目值相等的条目键。

(这应该是 O(NlogN) 时间并且需要 O(N) 额外空间......取决于所使用的排序算法。)

Building on Mat's answer, if you need to do this operation frequently, use one of the bidirectional map classes from Guava or Apache Commons Collections; e.g. HashBiMap<K,V> or DualHashBidiMap or DualTreeBidiMap. These data structures maintain a pair of maps that represent the forward and inverse mappings.

Alternatively, for a once off computation:

  1. Extract the Map.entries() collection into an array.
  2. Sort the array in order of the values.
  3. Iterate the array, and extract the entry keys for which subsequent entry values are equal.

(This should be O(NlogN) in time and require O(N) extra space ... depending on the sort algorithm used.)

眼眸里的那抹悲凉 2024-12-29 00:07:37

最基本的方法是:

  1. 获取 HashMap 的第一个键并迭代映射检查具有相同值的键。
  2. 如果找到,请从地图中删除该键并将该键存储在另一个集合中(可能是 Vector)。
  3. 然后在检查所有其他密钥后,将当前密钥添加到该集合中。
  4. 如果未找到其他密钥,请将当前密钥添加到该集合中。
  5. 然后将该集合中的键添加到具有相关值的另一个映射中。清除集合。
  6. 继续执行下一个键并执行相同操作。

完成此操作后,您将得到您想要的结果。

编辑:代码:

    HashMap comp = new HashMap(); // Calculations Done
    Vector v = new Vector(); // Temporary List To Store Keys

    // Get The List Of Keys
    Vector<Integer> keys = new Vector<Integer>();
    Iterator<Integer> it = hm.keySet().iterator();
    while(it.hasNext()) keys.add(it.next());

    // For Every Key In Map...
    for(int i = 0; i < hm.size(); i++) {
        int key = keys.get(i);
        v.add(key);  // Add the Current Key To Temporary List

        // Check If Others Exist
        for(int j = i+1; j < hm.size(); j++) {
            int nkey = keys.get(j);
            if(hm.get(key).equals(hm.get(nkey))) {
                v.add(nkey);
            }
        }

        // Store The Value Of Current Key And The Keys In Temporary List In The Comp HashMap
        String val = hm.get(key);
        String cKey = "";
        for(int x = 0; x < v.size(); x++)
            cKey += v.get(x) + ",";

        // Remove The Comma From Last Key, Put The Keys As Value And Value As Key
        cKey = cKey.substring(0, cKey.length()-1);
        comp.put(cKey, val);

        // Clear The Temporary List
        v.clear();
    }

此代码中有一个小问题:出现重复,而且最后一个重复似乎是正确的。

使用您的示例给出的输出。 (您需要进行一些格式化)。

{3=a,b, 6=c, 5=e,c, 2,5=e,c, 4=f, 1,3=a,b}

The most basic method will be to:

  1. Get the first key of the HashMap and iterate over the map checking for keys with the same value.
  2. If found, remove that key from the map and store the key in another collection (maybe a Vector).
  3. Then after all other keys are checked, add the current key to that collection.
  4. If no other keys are found, add the current key to that collection.
  5. Then add the keys in that collection to another map with the relevant value. Clear the collection.
  6. Proceed to the next key and do the same.

After doing this, you will end up with what you want.

EDIT: The Code:

    HashMap comp = new HashMap(); // Calculations Done
    Vector v = new Vector(); // Temporary List To Store Keys

    // Get The List Of Keys
    Vector<Integer> keys = new Vector<Integer>();
    Iterator<Integer> it = hm.keySet().iterator();
    while(it.hasNext()) keys.add(it.next());

    // For Every Key In Map...
    for(int i = 0; i < hm.size(); i++) {
        int key = keys.get(i);
        v.add(key);  // Add the Current Key To Temporary List

        // Check If Others Exist
        for(int j = i+1; j < hm.size(); j++) {
            int nkey = keys.get(j);
            if(hm.get(key).equals(hm.get(nkey))) {
                v.add(nkey);
            }
        }

        // Store The Value Of Current Key And The Keys In Temporary List In The Comp HashMap
        String val = hm.get(key);
        String cKey = "";
        for(int x = 0; x < v.size(); x++)
            cKey += v.get(x) + ",";

        // Remove The Comma From Last Key, Put The Keys As Value And Value As Key
        cKey = cKey.substring(0, cKey.length()-1);
        comp.put(cKey, val);

        // Clear The Temporary List
        v.clear();
    }

There is a little problem in this code: Duplicates occur and also the last duplicate seems to be correct.

The output using your example give. (You need to do a little formatting).

{3=a,b, 6=c, 5=e,c, 2,5=e,c, 4=f, 1,3=a,b}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文