哈希映射内的哈希映射未使用删除删除键?

发布于 2025-01-16 22:57:33 字数 1382 浏览 2 评论 0原文

这是我的清单。

list = Stream.of(
        "06|20|1",
        "11|20|2",
        "11|20|2",
        "07|207|6",
        "11|207|2",
        "07|207|6",
).collect(Collectors.toList());

我有一个哈希图,例如:

HashMap<String, HashMap<String, String>> hashMap = new HashMap<>();
HashMap<String, String> newHash = new HashMap<>();

我的代码是

for (String line : list) {
    String key, value, priority;
    key = line.split("\\|", -1)[1];
    value = line.split("\\|", -1)[0];
    priority = line.split("\\|", -1)[2];

    if (hashMap.containsKey(key)) {
        HashMap<String, String> getPriority = hashMap.get(key);
        Map.Entry<String, String> entry = getPriority.entrySet().iterator().next();
        String oldKey = entry.getKey();
        String previousPrior = getPriority.get(oldKey);
        if (Integer.parseInt(priority) > Integer.parseInt(previousPrior)) {
            getPriority.remove(oldKey);
            getPriority.put(value,priority);
           hashMap.put(key, getPriority);
        }
    } else {
        newHash.put(value, priority);
        System.out.println(newhas);
        hashMap.put(key, newhas);
    }

}

我只想拥有最高优先级的键,例如:

{20={11=2},207={07=6}}

11 和 7 在 20 和 207 中具有最高值。

但我正在获取内部哈希图中的所有值。

This is my list.

list = Stream.of(
        "06|20|1",
        "11|20|2",
        "11|20|2",
        "07|207|6",
        "11|207|2",
        "07|207|6",
).collect(Collectors.toList());

I have a hashmap such as:

HashMap<String, HashMap<String, String>> hashMap = new HashMap<>();
HashMap<String, String> newHash = new HashMap<>();

And my code is

for (String line : list) {
    String key, value, priority;
    key = line.split("\\|", -1)[1];
    value = line.split("\\|", -1)[0];
    priority = line.split("\\|", -1)[2];

    if (hashMap.containsKey(key)) {
        HashMap<String, String> getPriority = hashMap.get(key);
        Map.Entry<String, String> entry = getPriority.entrySet().iterator().next();
        String oldKey = entry.getKey();
        String previousPrior = getPriority.get(oldKey);
        if (Integer.parseInt(priority) > Integer.parseInt(previousPrior)) {
            getPriority.remove(oldKey);
            getPriority.put(value,priority);
           hashMap.put(key, getPriority);
        }
    } else {
        newHash.put(value, priority);
        System.out.println(newhas);
        hashMap.put(key, newhas);
    }

}

I want to have the have the key with highest priority only such as:

{20={11=2},207={07=6}}

as 11 and 7 has the highest valaues in 20 and 207.

But i am getting all values in the inner hashmap.

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

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

发布评论

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

评论(2

你与清晨阳光 2025-01-23 22:57:33

使用流来代替怎么样?

Map<String, Map<String, String>> map = list.stream()
        .map(line -> line.split("\\|"))
        .sorted(Comparator.comparingInt(line -> Integer.parseInt(line[2])))
        .collect(Collectors.toMap(
                line -> line[1],
                line -> Map.of(line[0], line[2]),
                (low, high) -> high));

Ideone 演示

How about using streams instead?

Map<String, Map<String, String>> map = list.stream()
        .map(line -> line.split("\\|"))
        .sorted(Comparator.comparingInt(line -> Integer.parseInt(line[2])))
        .collect(Collectors.toMap(
                line -> line[1],
                line -> Map.of(line[0], line[2]),
                (low, high) -> high));

Ideone Demo

无言温柔 2025-01-23 22:57:33

对于在代码的 else 条件下插入 Map 变量中的每个新键,您需要创建新的 HashMap 将其与新钥匙。
您正在做的是对 Map 变量中的所有键使用相同的 newhas 变量。
因此将其更改

        else {
            newhas.put(value, priority);
            System.out.println(newhas);
            Map.put(key, newhas);
        }

       else {
            newHas = new HashMap<>();
            newhas.put(value, priority);
            System.out.println(newhas);
            Map.put(key, newhas);
        }

Ideone Demo

For each new key that you insert in your Map variable in the else condition of your code you need to create new HashMap to insert it along with the new key.
What you are doing is using the same newhas variable for all the keys in your Map variable.
So change this

        else {
            newhas.put(value, priority);
            System.out.println(newhas);
            Map.put(key, newhas);
        }

to

       else {
            newHas = new HashMap<>();
            newhas.put(value, priority);
            System.out.println(newhas);
            Map.put(key, newhas);
        }

Ideone Demo

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