简体   繁体   English

如何根据值修改HashMaps

[英]How to modify HashMaps according to values

Let's say I have a HashMap<String, Integer> containing usernames and their position as an int in a queue or line.假设我有一个HashMap<String, Integer>包含用户名及其在队列或行中作为 int 的位置。 How would I go about removing a user from the queue and subsequently update all the values for the users behind?我将如何从队列中删除用户并随后更新后面用户的所有值?

I've found a method of doing this that I believe should work.我找到了一种我认为应该可行的方法。 @Rias, thanks for the direction to concurrent hashMaps which provided a way to complete the same task I was facing. @Rias,感谢对并发 hashMaps 的指导,它提供了一种完成我面临的相同任务的方法。

public void removeFromQueue(String user) {
    if (queueMap.containsKey(user)) {
        int userPlace = queueMap.get(user);
        for (String currentser : queueMap.keySet()) {
            if (queueMap.get(currentUser) > userPlace) {
                queueMap.put(user, queueMap.get(currentUser) - 1);
            }
        }
    }
}

The self-answer can be improved.自我回答可以改进。 Most obviously you are doing two map look ups for each entry when none are required (as we are iterating).最明显的是,当不需要时(因为我们正在迭代),您正在为每个条目进行两次地图查找。 Also there is now the handy Map.replaceAll method.现在还有方便的Map.replaceAll方法。 So perhaps:所以也许:

public void removeFromQueue(String user) {
    Integer userPlace = queueMap.get(user);
    if (userPlace != null) {
        queueMap.replaceAll((k, v) -> v>userPlace ? v-1 : v);
    }
}

You can do it as follows:你可以这样做:

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    static Map<String, Integer> queueMap = new LinkedHashMap<String, Integer>();

    public static void main(String[] args) {
        queueMap.put("Arvind", 5);
        queueMap.put("Avinash", 6);
        queueMap.put("Kumar", 7);
        queueMap.put("Lightcaster", 8);
        queueMap.put("Stackoverflow", 9);
        System.out.println("Before: " + queueMap);
        removeFromQueue("Kumar");
        System.out.println("After: " + queueMap);
    }

    static void removeFromQueue(String user) {
        final Integer userPlace = queueMap.get(user);
        if (userPlace != null) {
            queueMap.replaceAll((k, v) -> v > userPlace ? v - 1 : v);
            queueMap.remove(user);
        }
    }
}

Output:输出:

Before: {Arvind=5, Avinash=6, Kumar=7, Lightcaster=8, Stackoverflow=9}
After: {Arvind=5, Avinash=6, Lightcaster=7, Stackoverflow=8}

I hope I didn't miss anything, why you can't use a simple ArrayList<String> for all the user names?我希望我没有遗漏任何东西,为什么不能对所有用户名使用简单的ArrayList<String>

The list will keep the users in order by their index, which is the equivalent of the value in your map.该列表将按用户的索引排序,这相当于您地图中的值。 Moreover, if you remove a user which his index is i , then all the users with index > i will be reorganized as you want (after the removal the new index of the user with index i + 1 will be i )此外,如果您删除其索引为i的用户,则所有具有 index > i的用户将根据需要进行重组(删除后,索引为i + 1的用户的新索引将为i

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

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