-2

I have a HashMap which looks like :

( student1 => Map( name => Tim,         
                   Scores => Map( math => 10,
                                  physics => 20,
                                  Computers => 30),
                    place => Miami,
                    ranking => Array(2,8,1,13),
                  ),
  student2 => Map ( 
                   ...............
                   ...............
                ),
   ............................
   ............................
);

Since the keys are not any particular type(objects, Strings, Integers etc), how can I "dive" through this complex HashMap ?

EDIT: "dive" means to iterate through each and every keys and values.

M-D
  • 10,057
  • 9
  • 29
  • 35
  • what is meant by dive through? – Subin Sebastian Jun 13 '12 at 04:59
  • 1
    As I've already told you in your previous post, submit your requirement and you can get better help with your design, you're like asking the community how can we do your work step by step. – Luiggi Mendoza Jun 13 '12 at 05:04
  • Is this a materially different question to the one you posted an hour ago: [How do I dump the contents of a HashMap](http://stackoverflow.com/questions/11007860/how-do-i-dump-the-contents-of-a-hash-map)? – Greg Kopff Jun 13 '12 at 05:13
  • @GregKopff there is [Creating complex HashMap in Java](http://stackoverflow.com/q/11008083/1065197) question too. It's really bad when a person prefer to ask for fish instead of learning how to go fishing by himself. – Luiggi Mendoza Jun 13 '12 at 05:16
  • If your keys are not of a particular type, your model might need a redesign. On the other hand, all keys are subclasses of `Object`. The `Map` interface specifies `Object` as type for the `get` and `contains` operations, so no problem there. But the real question is: Why are you using such an unwieldy data structure to start with? – Wormbo Jun 13 '12 at 16:34

3 Answers3

3

How about

Map<Long, Student> studentIdToStudentMap = new HashMap<Long, Student>();

and

class Student{
  private String name;
  private List<Score> scores;
  private Location location;
  private List<Rank> rankings;
  //const, equals(), hashcode(), accessors
}

and

class Score{
   private String subject;
   private float marksObtained;
   private float totalMark;
   private Long examId;
   //const, equals(), hashcode(), accessors
}

Like wise

jmj
  • 232,312
  • 42
  • 391
  • 431
  • Yes, thats good. But "students" is just an example of what I actually requires and that cannot be transformed into a class. So I need the Data structure. – M-D Jun 13 '12 at 05:05
  • You can take this as an example to create class for your model – jmj Jun 13 '12 at 05:09
1

Totally untested and not guaranteed to be fit for any purpose (your question is somewhat vague), but it should give you the idea on how to work with your problem:

public void doStuffWithMap(Map yourMap)
{
  for(Map.Entry entry : yourMap.entrySet())
  {
    if (entry.getValue() instanceof Map)
      doStuffWithMap((Map)entry.getValue());
    else
      System.out.println(entry.getKey() + ": " + entry.getValue());
  }
}
yankee
  • 36,174
  • 13
  • 96
  • 156
1

use something like

private static printKeyValues(Map map) {
  for (Object key : map.keySet()) {
    if(map.get(key) instanceOf Map) {
      printKeyValues((Map)map.get(key))
    } else {
      System.out.println("key:" + key+" value:"+ map.get(key));
    }
  }
}
Greg Kopff
  • 15,100
  • 11
  • 53
  • 72
Subin Sebastian
  • 10,642
  • 3
  • 36
  • 41