0

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

4
  • Why would you add 100 and 15, aren't they for different games? It seems Map<gameId, Achievement> only stores 1 achievement per game? Is HashMap.values() what you need? Commented Jan 31, 2016 at 18:48
  • 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 Commented Jan 31, 2016 at 18:51
  • I see..kind of.. Well, I'll cook up an answer for you but I'll suggest a slightly different structure. Commented Jan 31, 2016 at 18:56
  • Thanks. I appreciate it. I have just been stuck on this little tidbit for a while Commented Jan 31, 2016 at 18:57

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This works like a charm and was exactly what I was looking for. Thanks for the help. Also thanks to everyone else for the assistance.
1

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() );
    }
}

1 Comment

This is probably not what you meant, is it? It seems you might actually have a List of Achievements, in which case you don't need a Map.
0

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);

Comments

0
 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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.