Creating a complex HashMap in Java

What is the easiest way to create a HashMap as follows:

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

I tried this:

 HashMap record = new HashMap(); record.put("student1", new HashMap()); record.get("student1").put("name","Tim"); record.get("student1").put("Scores", new HashMap()); 

But I get an error. I do this because record.get("student1") is a HashMap object, so I assume put should work on it, etc.

If this does not work, what is the best way to do this?

+4
source share
7 answers

You get this exception because get() returns an Object type. you need to do this using Map .

 ((Map)record.get("student1")).put("name","Tim"); 
+4
source

You can do this using the Object type before Map or HashMap .

 HashMap record = new HashMap(); record.put("student1", new HashMap()); ((HashMap)record.get("student1")).put("name","Tim"); ((HashMap)record.get("student1")).put("Scores", new HashMap()); 

However, as I said, are you sure you want this project?

+1
source

Java is a statically and nominally typed language. So the coding style that you follow is not preferred. Instead, you should create classes and objects.

Thus, Guava provides several utility classes, such as Lists , Iterables , Maps , etc., which allow you to create the appropriate collection objects from varargs.

+1
source

Although I agree with other commentators that I prefer to create suitable classes for your domain objects, we can simplify the syntax of building a map using some helper methods:

 Map<String, Object> map = map( entry("student1", map( entry("name", "Tim"), entry("scores", map( entry("math", 10), entry("physics", 20), entry("Computers", 30) )), entry("place", "Miami"), entry("ranking", array(2, 8, 1, 13)) )), entry("student2", map( // ... )) ); // map.toString(): // {student2={}, student1={scores={math=10, physics=20, Computers=30}, name=Tim, place=Miami, ranking=[2, 8, 1, 13]}} 

You just need to define a helper class, as shown below, and use static import:

 import static com.example.MapHelper.*; 

Helper class:

 package com.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MapHelper { public static class Entry { private String key; private Object value; public Entry(String key, Object value) { this.key = key; this.value = value; } public String getKey() { return key; } public Object getValue() { return value; } } public static Map<String, Object> map(Entry... entries) { Map<String, Object> map = new HashMap<String, Object>(); for (Entry e : entries) { map.put(e.getKey(), e.getValue()); } return map; } public static Entry entry(String k, Object v) { return new Entry(k, v); } public static List<Object> array(Object... items) { List<Object> list = new ArrayList<Object>(items.length); for (int i = 0; i < items.length; i++) { list.add(i, items[i]); } return list; } } 
+1
source

Well, the “Java” way to do this is to break these objects into classes. In the example you provided, it looks like you can create a Student class that contains attributes such as name, location, location, etc.

This is much cleaner than getting everything on a map like this one.

0
source

Why are you creating a complex set of HashMaps, you can instead create a Java object and then use it for a HashMap.

So, in your case, you can create a ScoreInfo class that will have a rating, place and ranking of the HashMap, and then use this as the value of the HashMap .

0
source

Replace

 HashMap record = new HashMap(); 

from

 Map<String,Map<String, Object>> record = new HashMap<String,Map<String, Object>>(); 

But, it does not make sense to set different types of objects as values. If the next line is wrong,

 record.get("student1").put("Scores", new HashMap()); 

You can also simplify the definition.

 Map<String,Map> record = new HashMap<String,Map>(); 

Assumption: you are using JDK 1.5+

-1
source

All Articles