【问题标题】:add elements in arraylist inside hashmap在 hashmap 内的 arraylist 中添加元素
【发布时间】:2015-01-21 14:44:44
【问题描述】:

我正在尝试动态构建 String 和 arraylist 类型的动态哈希图。我有一些来自服务器的 json 数据,而不是声明许多 arraylist,我想将它们保存在 hashmap 中,其中 String 作为键,arraylist 作为值。

这就是我现在正在做的事情

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);

所以这是一种丑陋的做法......我想通过直接放入hashmap而不声明这么多arraylist来做到这一点。请忽略 json 字符串提取,因为它只是虚拟的。

【问题讨论】:

  • 用空列表预先填充您的 HashMap,对您不起作用?
  • 你只用了两个ArrayList,不是很多吗?而且我认为这不是整个代码,对吧? B/c 必须有迭代让所有学生进入 allStu 等。
  • 是的,这不是完整的,只是其中的一部分……这只是为了解释想法……

标签: java json arraylist hashmap


【解决方案1】:

你只需要在开始时初始化arraylist,然后根据key添加值。如果你知道我猜你知道的钥匙,你可以这样做

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

正如@steffen 提到的,但有细微的变化

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

这与其他目的没有什么不同,但可能仍然可以提供帮助。

【讨论】:

    【解决方案2】:
    hash.get("Student").put(prop)
    

    可能是一个解决方案,因为您知道地图中的关键。

    使用这种方式,您可以省略“allStu”和“allEmp”列表,因为您可以直接从地图中获取它们。

    【讨论】:

    • 但没有初始化arraylist并放置密钥..我想这是不可能的
    • 你是对的,但是在你的构造函数中你可以正确初始化地图,例如public MyConstructor() { List&lt;classproperty&gt; stud = new ArrayList&lt;classproperty&gt;(); List&lt;classproperty&gt; emp = new ArrayList&lt;classproperty&gt;(); hash = new HashMap&lt;String, List&lt;classproperty&gt;&gt;(); hash.put("Student", stud); hash.put("Emp", emp); }
    【解决方案3】:

    我建议使用已经支持此功能的 Guava 库中的 MultiMap。如果你不打算导入这个库,那么你可以手动滚动自己作为 Map&lt;K, List&lt;V&gt;&gt; 的包装器:

    //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();
        }
    }
    

    然后只需使用您的多图:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2017-01-16
      相关资源
      最近更新 更多