Eric Rangil
Eric Rangil

Reputation: 135

adding specific value from a hashmap with a user defined object as value

I have a hashmap which is in the form <gameID, Achievement> in which Achievement is a user defined object which holds an AchievementID, AchievementName, and AchievementPoints:

[1003901=(901, Shoot 'em up, 100), 1001902=(902, Tango down, 15)]

I would like to add up the values based on AchievementPoints for the entire game, so in this case, I would add up 100 and 15. I tried to iterate through using the for each loop, however, I can't seem to figure out how to access the AchievementPoints member specifically and just use that value. Is there any way to just iterate through the hashmap and just add values based on a specific member of the object value?

Edit 1:
sorry, to clarify, my gameID is actually a combination where the first four numbers are the actual gameID and the next 3 are the achievementID. This was done to keep the id unique. That is why I would add both of those

Upvotes: 0

Views: 83

Answers (4)

NilsH
NilsH

Reputation: 13821

Assuming Java 8, if you simply want to add up all the values in the map, you could do something like this:

achievementMap.values().stream().mapToInt(a -> a.getAchievmentPoints()).sum();

This creates a stream that iterates over all the values in the map, performs a transformation of the Achievement instance to an integer value, selecting the achievementPoints property of the object and then sums it all up.

Upvotes: 2

thedarkpassenger
thedarkpassenger

Reputation: 7358

 int total = 0;
HashMap<String,Achievement> map = new HashMap();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()){
  Map.Entry entry = (Map.Entry) iterator.next();
  Achievement achievement = (Achievement) entry.getValue();

  //Once you have achievement Object, apply your custom logic here.
  total = total + achievement.achievementPoints;

}

Upvotes: 0

Kenney
Kenney

Reputation: 9093

I'm guessing this is more or less what you're intending: you have multiple games, each game having multiple achievements: a Map<Integer,List<Achievement>.

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main
{
    static class Achievement
    {
        private int id;

        private String name;

        private int score;

        public Achievement( int id, String name, int score )
        {
            this.id = id;
            this.name = name;
            this.score = score;
        }
    }

    public static void main( String[] args )
    {
        Map<Integer, List<Achievement>> map = new HashMap<>();
        map.put( 1003, Arrays.asList( new Achievement( 901, "Shoot 'em up", 100 ) ) );
        map.put( 1001, Arrays.asList( new Achievement( 902, "Tango down", 15 ) ) );
        System.out.println( map.get( 1003 ).stream().mapToInt( ( a ) -> a.score ).sum() );
    }
}

Upvotes: 1

Esteban Rincon
Esteban Rincon

Reputation: 2110

If what you want to do is sum all the points gathered through the game, assuming you use encapsulation in the Achievement class and there's a getter method for Achievement points then, what if you iterate through the hashMap like this:

int sum = 0;
 Iterator it = gameMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        sum += pair.getValue().getAchievementPoints();
    }
System.out.println("Sum = " + sum);

Upvotes: 0

Related Questions