jonprasetyo
jonprasetyo

Reputation: 3586

How to move values around in a HashMap

I have class, lets say Hockey. The Hockey class will set the hockey's score like the code below:

public class Hockey{
  private HashMap<String, Integer> hockeyScore;

  public Hockey(){
    hockeyScore = new HashMap<String, Integer>();
  }

  public void setHockeyScore(String clubName, int score){
    hockeyScore.put(clubName, score);  
  }
}

A hockey game will only have two teams and two scores, is it possible to swap the scores? For example when we insert into hashmap and it comes out with the keys and values...

team 'a' = 23

team 'b' = 10

then you swap the values in the hashmap which will look like...

team 'a' = 10

team 'b' = 23

Sorry guys, I was wondering if there is a like a method that swaps the scores around, without manually using the 'a' and 'b' reference. Like once you insert any keys and values into the hashmap, this method will swap the values around.

Upvotes: 3

Views: 5452

Answers (5)

jonprasetyo
jonprasetyo

Reputation: 3586

Thank you very much for your help!

I have figured out away to swap the values in the hashmap. First I collected the scores and put them into an arraylist and then i compared the arraylist to the hashmap and re set the hash map according the the arraylist.

public void swapScores(){ 
        for(Map.Entry<Team, Integer> getScores: winningTeam.entrySet()){
            if(scores.get(0).equals(getScores.getValue())){
                getScores.setValue(scores.get(1));
                System.out.println(getScores.getKey().getTeamName()+":"+getScores.getValue());
            }else if(scores.get(1).equals(getScores.getValue())){
                getScores.setValue(scores.get(0));
                System.out.println(getScores.getKey().getTeamName()+":"+getScores.getValue());
            }
        }

    }

If there is a shorter way to do this please let me know.

Thank you again everyone!

Upvotes: 0

Reimeus
Reimeus

Reputation: 159784

You could swap Map entries using this one-liner:

hockeyScore.put(a, hockeyScore.put(b, hockeyScore.get(a)));

Upvotes: 1

AlexWien
AlexWien

Reputation: 28727

Integer aScore = hockeyScore.get("a");
hockeyScore.put("a", map.get("b"));
hockeyScore.put("b", aScore);

and the threadsafe variant

synchronized (hockeyScore) {
   Integer aScore = hockeyScore.get("a");
   hockeyScore.put("a", map.get("b"));
   hockeyScore.put("b", aScore);
}

Upvotes: 0

SAJ14SAJ
SAJ14SAJ

Reputation: 1708

I am having trouble imagining why you would want to do this, but it is only possible in the case of a Map with 2 entries--otherwise, swap is meaningless.

My Java is a bit rusty, and I am not going to pull out Eclipse just to check syntax but something quite similar to this should do the job for you:

Map<string, int> swapped(string key1, string key2, Map<string, int> m) {
    Map dummy = new HashMap<string, int>();

    dummy.put(key2, m.get(key1));
    dummy.put(key1, m.get(key2));

    return dummy;
}

If you are more ambitious, you could iterate the entries to get the keys rather than having to have them available.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198103

Sure, the way you traditionally swap values:

Integer tmp = map.get(a);
map.put(a, map.get(b));
map.put(b, tmp);

Upvotes: 6

Related Questions