【发布时间】:2021-10-17 05:51:19
【问题描述】:
我正在学习集合,我想使用流循环 2 个 HashMap 并将键和两个值添加到 List 对象。
例如:
HashMap<String, String> map1= new LinkedHashMap<String, String>();
HashMap<String, String> map2= new LinkedHashMap<String, String>();
map1.put("A", "Apple");
map1.put("B", "Ball");
map1.put("C", "Cucumber");
map1.put("D", "Dairy");
map2.put("A", "Airplane");
map2.put("B", "Baby");
map2.put("C", "Cat");
map2.put("D", "Dog");
class DummyClass {
String Key;
String Value1;
String Value2;
// getters and setters..
}
List<DummyClass> test = Add key, and both Values
输出预期:
DummayClass Obj1 = {A,Apple,Airplane};
DummayClass Obj2 = {B,Ball,Baby}; likewise
我可以通过迭代单个地图并比较密钥并添加它来实现这一点。但我想使用Streams 来达到同样的效果。
【问题讨论】:
-
您能与我们分享您尝试并坚持的地方吗?
-
无论是正式的家庭作业还是自学我认为都适用:How do I ask and answer homework questions?
标签: java collections java-stream