英文:
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<HashMap<String, String>> 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<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);
}
答案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<Map.Entry<String, String>> statusNames = statusList.stream()
.flatMap(hashMap -> hashMap.entrySet().stream())
.filter(entry -> entry.getKey().equals("status_name"))
.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<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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论