简体   繁体   中英

add elements in arraylist inside hashmap

I am trying to build a dynamic hashmap of type String and arraylist dynamically. I have some json data coming from server and instead of declaring many arraylist I want to save them in hashmap with String as key and arraylist as value.

Here is what I am doing now

ArrayList<classproperty> allStu;
ArrayList<classproperty> allEmp;
HashMap<String, ArrayList<classproperty>> hash;
if (type.equals("Student")) {
    prop = new classproperty("Student", info.getJSONObject(i).getJSONObject("student").getJSONArray("class").getJSONObject(s).getJSONObject("type").getString("name"));
    allStu.add(prop);       
}
if (type.equals("Emp")) {
    prop = new esSignalProperty("Emp", info.getJSONObject(m).getJSONObject("emp").getJSONObject(s).getJSONObject("dept").getString("name"));
    allemp.add(prop);        
}

hash.put("Student", allStu);
hash.put("Emp", allemp);

So it is ugly way to do it...and I would like to do it by directly putting into hashmap without declaring so many arraylist. please ignore json string extraction as it is just dummy.

You just need to initialize the arraylist in the beginning and then just add value based on key. if you know the keys that I guess you know you can do like this

public HashMap<String, ArrayList<classproperty>> hash
hash.put("Student", new ArrayList<classproperty>());
hash.put("Emp", new ArrayList<classproperty>());

after just as @steffen mention but with minor change

  hash.get("Student").add(prop);
  hash.get("Emp").add(prop);

It is nothing very different from what other purposed but may be still can help.

hash.get("Student").put(prop)

could be a solution, as you know the key inside the map.

Using this way you can leave out the 'allStu' and 'allEmp' Lists, as you can get them directly from the map.

I would suggest using MultiMap from Guava library that already supports this. If you don't plan to import this library, then you can roll your own manually as a wrapper of a Map<K, List<V>> :

//basic skeleton of the multimap
//as a wrapper of a map
//you can define more methods as you want/need
public class MyMultiMap<K,V> {
    Map<K, List<V>> map;
    public MyMultiMap() {
        map = new HashMap<K, List<V>>();
    }

    //in case client needs to use another kind of Map for implementation
    //e.g. ConcurrentHashMap
    public MyMultiMap(Map<K, List<V>> map) {
        this.map = map;
    }

    public void put(K key, V value) {
        List<V> values = map.get(key);
        if (values == null) {
            //ensure that there will always be a List
            //for any key/value to be inserted
            values = new ArrayList<V>();
            map.put(key, values);
        }
        values.add(value);
    }

    public List<V> get(K key) {
        return map.get(key);
    }

    @Override
    public String toString() {
        //naive toString implementation
        return map.toString();
    }
}

Then just use your multimap:

MyMultiMap myMultiMap = new MyMultiMap<String, ClassProperty>();
myMultiMap.put("student", new ClassProperty(...));
myMultiMap.put("student", new ClassProperty(...));
System.out.println(myMultiMap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM