How to save String object into an ArrayList and also remove the HashMap inside of this ArrayList<HashMap<String, String>>?

huangapple go评论101阅读模式
英文:

How to save String object into an ArrayList and also remove the HashMap inside of this ArrayList<HashMap<String, String>>?

问题

我有一个ArrayList<HashMap<String, String>>,我想要移除 `status_id=X`,只保留 `status_name=X`,然后保存到ArrayList中。我应该如何做呢?

ArrayList<HashMap<String, String>> statusList = [{status_id=1, status_name=iceCream}, {status_id=2, status_name=cake (chocolate)}]

预期的结果应该打印出:

[status_name=iceCream, status_name=cake(chocolate)]
英文:

I have an ArrayList<HashMap<String, String>>, and I want to remove status_id=X and only keep status_name=X and save into ArrayList. How can I do it?

ArrayList&lt;HashMap&lt;String, String&gt;&gt; statusList = [{status_id=1, status_name=iceCream}, {status_id=2, status_name=cake (chocolate)}]

The expected result should print:

[status_name=iceCream, status_name=cake(chocolate)]

答案1

得分: 2

请尝试以下代码:

public static void main(String[] args) {
    ArrayList<Map<String, String>> statusList = new ArrayList();
    Map<String, String> map1 = new HashMap<>();
    map1.put("status_id","1");
    map1.put("status_name","ice_cream");

    Map<String, String> map2 = new HashMap<>();
    map2.put("status_id","2");
    map2.put("status_name","cake (chocolate)");

    statusList.add(map1);
    statusList.add(map2);

    System.out.print(statusList);

    List<String> list1 = statusList.stream().map(x -> x.get("status_name")).collect(Collectors.toList());
    System.out.print(list1);
}
英文:

Please try this code-

          public static void main(String[] args) {
            ArrayList&lt;Map&lt;String, String&gt;&gt; statusList = new ArrayList();
            Map&lt;String, String&gt; map1 = new HashMap&lt;&gt;();
            map1.put(&quot;status_id&quot;,&quot;1&quot;);
            map1.put(&quot;status_name&quot;,&quot;ice_cream&quot;);
    
            Map&lt;String, String&gt; map2 = new HashMap&lt;&gt;();
            map2.put(&quot;status_id&quot;,&quot;2&quot;);
            map2.put(&quot;status_name&quot;,&quot;cake (chocolate)&quot;);
    
            statusList.add(map1);
            statusList.add(map2);
    
            System.out.print(statusList);
    
            List&lt;String&gt; list1 =  statusList.stream().map(x -&gt; x.get(&quot;status_name&quot;)).collect(Collectors.toList());
            System.out.print(list1);
          }

答案2

得分: 1

根据情况,您似乎需要一个Map.Entry列表,其中键映射到status_name。如果您使用的是 Java 8 及更高版本,您可以展开哈希映射并在其上应用过滤器。

List<Map.Entry<String, String>> statusNames = statusList.stream()
        .flatMap(hashMap -> hashMap.entrySet().stream())
        .filter(entry -> entry.getKey().equals("status_name"))
        .collect(Collectors.toList());

如果您使用的是 Java 7 版本之前的版本,您需要遍历元素哈希映射并将所需的Map.Entry添加到一个单独的列表中。

List<Map.Entry<String, String>> statusNames = new ArrayList<>();

for (Map<String, String> tempMap : statusList) {
    for (Map.Entry<String, String> entrySet : tempMap.entrySet()) {
        if(entrySet.getKey().equals("status_name"))
            statusNames.add(entrySet);
    }
}
英文:

As it seems you need a list of Map.Entry where the key is mapped to status_name. If you are on Java 8+ version, you can flatten the hashmap and apply a filter on it.

List&lt;Map.Entry&lt;String, String&gt;&gt; statusNames = statusList.stream()
        .flatMap(hashMap -&gt; hashMap.entrySet().stream())
        .filter(entry -&gt; entry.getKey().equals(&quot;status_name&quot;))
        .collect(Collectors.toList());

If you are on the pre-java 7 version. you need to iterate over element HashMap and add the required Map.Entry to a separate list.

List&lt;Map.Entry&lt;String, String&gt;&gt; statusNames = new ArrayList();

for (Map&lt;String, String&gt; tempMap : statusList) {
    for (Map.Entry&lt;String, String&gt; entrySet : tempMap.entrySet()) {
        if(entrySet.getKey().equals(&quot;status_name&quot;))
            statusNames.add(entrySet);
    }
}

huangapple
  • 本文由 发表于 2020年8月30日 15:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定