Anton Stoychev
Anton Stoychev

Reputation: 11

How to sum value in same key HashMap Java?

I need to sum the value in same key not just to replace? Thanks.

    LinkedHashMap<String, LinkedHashMap<String, Integer>> users = new LinkedHashMap<>();
    for (int i = 0; i < n; i++) {
        String[] input = scanner.readLine().split("\\s+");
        String ip = input[0];
        String name = input[1];
        int duration = Integer.parseInt(input[2]);
        if (!users.containsKey(name)) {
            users.put(name, new LinkedHashMap<>());
            users.get(name).put(ip,duration);
        } else {
            users.get(name).put(ip,duration);
        }
    }

Upvotes: 1

Views: 2458

Answers (5)

Jesse Nelson
Jesse Nelson

Reputation: 796

LinkedHashMap<String, LinkedHashMap<String, Integer>> users = new LinkedHashMap<>();
    for (int i = 0; i < n; i++) {
        String[] input = scanner.readLine().split("\\s+");
        String ip = input[0];
        String name = input[1];
        int duration = Integer.parseInt(input[2]);
        LinkedHashMap<String, Integer> ipDurations= users.get(name);
        
        if (ipDurations== null) {
            ipDurations= new LinkedHashMap<>();
            ipDurations.put(ip, duration);
            users.put(name, ipDurations);
        } else {
            Integer cummulativeDuration = ipDurations.get(ip);
            if (cummulativeDuration == null) {
               cuumulativeDuration = 0;
               ipDurations.put(ip, cummulativeDuration);
            }
            cummulativeDuration += duration;
          }
     }

Upvotes: 2

fps
fps

Reputation: 34460

You can do it by using Map.computeIfAbsent in the outer map, along with Map.merge in the inner maps:

Map<String, Map<String, Integer>> users = new LinkedHashMap<>();

for (int i = 0; i < n; i++) {
    String[] input = scanner.readLine().split("\\s+");
    String ip = input[0];
    String name = input[1];
    int duration = Integer.parseInt(input[2]);

    users.computeIfAbsent(name, k -> new LinkedHashMap<>())
        .merge(ip, duration, Integer::sum);
}

Upvotes: 3

Oleg Zinoviev
Oleg Zinoviev

Reputation: 559

Unfortunately I cannot add full snippet due I type from phone, but I beleive all you need is stream and collect with merge if you need initial map later, or just merge instead put if you dont

Upvotes: 1

Mykyta Lebid
Mykyta Lebid

Reputation: 77

I believe it'd be your answer Try this:

for (int i = 0; i < n; i++) {
        String[] input = scanner.readLine().split("\\s+");
        String ip = input[0];
        String name = input[1];
        int duration = Integer.parseInt(input[2]);
        if (!users.containsKey(name)) {
            users.put(name, new LinkedHashMap<>());
            users.get(name).put(ip, duration);
        } else {
            LinkedHashMap<String, Integer> user = users.get(name);
            Integer userDuration = user.get(ip);
            user.put(ip, userDuration + duration);
        }
    }

Upvotes: 2

Pace
Pace

Reputation: 43817

int oldDuration = users.get(name).get(ip);
int totalDuration = oldDuration + duration;
users.get(name).put(ip, totalDuration);

Upvotes: 0

Related Questions