加入Java8集合API
我有两个加入Java8集合API,java,join,collections,java-8,Java,Join,Collections,Java 8,我有两个列表对象 [{Month=August-2013, Sales=282200}, {Month=July-2013, Sales=310400}, {Month=June-2013, Sales=309600}, {Month=May-2013, Sales=318200}, {Month=September-2013, Sales=257800}] 及 我想在Month键上加入(合并)这两个列表。如何使用新集合API在这些列表上执行多个联接操作(即完全外部联接、右外部联接等)
列表
对象
[{Month=August-2013, Sales=282200}, {Month=July-2013, Sales=310400},
{Month=June-2013, Sales=309600}, {Month=May-2013, Sales=318200},
{Month=September-2013, Sales=257800}]
及
我想在
Month
键上加入(合并)这两个列表。如何使用新集合API在这些列表上执行多个联接操作(即完全外部联接、右外部联接
等),执行完全外部联接的一种方法是首先构造一个映射
,将月份值链接到映射本身,并获取列表中的值:
//first concatenate the two lists
Map<String, Map<String, Object>> result = Stream.concat(list1.stream(),
list2.stream())
//then collect in a map where the key is the value of the month
.collect(toMap(m -> (String) m.get("Month"),
//the value is the map itself
m -> m,
//merging maps (i.e. adding the "Sales" and "NoOfTranx" infos)
(m1, m2) -> {m1.putAll(m2); return m1; }));
//finally put that in a list
List<Map<String, Object>> merge = new ArrayList<>(result.values());
代码:
publicstaticvoidmain(字符串[]args){
List list1=新的ArrayList();
列表1.添加(地图(“月份”、“2013年8月”、“销售额”,282200));
列表1.添加(地图(“月份”、“2013年7月”、“销售额”,310400));
System.out.println(“list1=“+list1”);
List list2=新的ArrayList();
清单2.添加(地图(“月份”、“2013年8月”、“NoOfTranx”,6700));
清单2.添加(地图(“月份”、“2013年7月”、“中午”,14400));
System.out.println(“list2=“+list2”);
映射结果=Stream.concat(list1.Stream(),
list2.stream())
.collect(toMap(m->(String)m.get(“月”),
m->m,
(m1,m2)->{m1.putAll(m2);返回m1;});
列表合并=新建ArrayList(result.values());
System.out.println(“merge=“+merge”);
}
私有静态映射(对象…kvs){
Map Map=newhashmap();
对于(int i=0;i
哪里定义了toMap?导入静态java.util.stream.Collectors.toMap代码>这里只有一列“月”我要加入。如果ex.{Month=August-13,Product=p1,Sales=282200}有多个列,我想加入Month和Product这两个列怎么办?使用相同的逻辑,您需要创建一个MontProduct
类,该类包含String Month
和String Product
,并具有适当的equals/hashcode方法。
//first concatenate the two lists
Map<String, Map<String, Object>> result = Stream.concat(list1.stream(),
list2.stream())
//then collect in a map where the key is the value of the month
.collect(toMap(m -> (String) m.get("Month"),
//the value is the map itself
m -> m,
//merging maps (i.e. adding the "Sales" and "NoOfTranx" infos)
(m1, m2) -> {m1.putAll(m2); return m1; }));
//finally put that in a list
List<Map<String, Object>> merge = new ArrayList<>(result.values());
list1 = [{Month=August-13, Sales=282200}, {Month=July-13, Sales=310400}]
list2 = [{Month=August-13, NoOfTranx=6700}, {Month=July-13, NoOfTranx=14400}]
merge = [{Month=August-13, Sales=282200, NoOfTranx=6700}, {Month=July-13, Sales=310400, NoOfTranx=14400}]
public static void main(String[] args) {
List<Map<String, Object>> list1 = new ArrayList<>();
list1.add(map("Month", "August-13", "Sales", 282200));
list1.add(map("Month", "July-13", "Sales", 310400));
System.out.println("list1 = " + list1);
List<Map<String, Object>> list2 = new ArrayList<>();
list2.add(map("Month", "August-13", "NoOfTranx", 6700));
list2.add(map("Month", "July-13", "NoOfTranx", 14400));
System.out.println("list2 = " + list2);
Map<String, Map<String, Object>> result = Stream.concat(list1.stream(),
list2.stream())
.collect(toMap(m -> (String) m.get("Month"),
m -> m,
(m1, m2) -> {m1.putAll(m2); return m1; }));
List<Map<String, Object>> merge = new ArrayList<>(result.values());
System.out.println("merge = " + merge);
}
private static Map<String, Object> map(Object... kvs) {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < kvs.length; i += 2) {
map.put((String) kvs[i], kvs[i+1]);
}
return map;
}