Hash Map

Ordering a hash map

Page 1 of 1

3 Replies - 787 Views - Last Post: 14 December 2009 - 12:22 PM Rate Topic: -----

#1 nickbuckley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 09

Hash Map

Posted 14 December 2009 - 04:42 AM

Im trying to order my hash map by points but having no luck. There are 3 hashmaps played,goal difference and game stats. Gamestats contains the matches won drawn lost and points. Im trying to order by points. Any help as currently i can only sort by team name.

HashMap played = new HashMap();
HashMap goalDifference = new HashMap();
HashMap gameStats = new HashMap();
for(int i = 0; i < matches.size(); i++)
{
Match match = matches.get(i);
played = getPlayed(match, played, "home");
played = getPlayed(match, played, "away");
goalDifference = getGoalDifference(match, goalDifference, "home");
goalDifference = getGoalDifference(match, goalDifference, "away");
gameStats = getGamesStats(match, gameStats, "home");
gameStats = getGamesStats(match, gameStats, "away");

}

Object[] teams = played.keySet().toArray();
Object[] sorted = gameStats.keySet().toArray();

Arrays.sort(teams);
System.out.println(Arrays.toString(sorted));

Is This A Good Question/Topic? 0
  • +

Replies To: Hash Map

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Hash Map

Posted 14 December 2009 - 06:57 AM

Please :code:

In terms of your problem, I would fill in an array with the values of the Map (iterate through it using an Iterator and Set) then use Arrays.sort(). Maps do not maintain order by either the keys or the values, so my solution is probably the easiest way to go.
Was This Post Helpful? 0
  • +
  • -

#3 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Hash Map

Posted 14 December 2009 - 07:13 AM

Several things that are not clear here:

What do you use as keys for the played map? Because you do sort them, but (a) I don't think it's sorting on what you want, (b ) you don't use the sorted array afterwards.

Why do you have 3 different maps?

Why use hashmaps at all? As macosxnerd mentioned, hashmaps are not sorted: you could use either a list, and sort it passing a proper Comparator, or a treemap that sorts the keys in ascending order (but obviously you have to make sure the sorting will be the one you want, i.e. make sure the key is the object you want to sort,, and that it provides the right Comparable implementation).

Give a shout if that's not clear.

This post has been edited by EdwinNameless: 14 December 2009 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: Hash Map

Posted 14 December 2009 - 12:22 PM

Sounds like GameStat(s) should be a class on its own. You could then sort it in any way you want by using the appropriate Comparator
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1