JAVA8 新特性 stream flatMap用法

将多个List合并为一个List

List list1= new ArrayList(){{add("1");add("2");}};
List list2= new ArrayList(){{add("3");add("4");}};
List list3= new ArrayList(){{add("5");add("6");}};
List> list = new ArrayList(){{add(list1);add(list2);add(list3);}};
System.out.println(list.stream().flatMap(List::stream).collect(Collectors.toList()));

将多个数组合并为一个List

String[] arr1=new String[]{"1","2"};
String[] arr2=new String[]{"3","4"};
String[] arr3=new String[]{"5","6"};
List listArr = new ArrayList(){{add(arr1);add(arr2);add(arr3);}};
System.out.println(listArr.stream().flatMap(Arrays::stream).collect(Collectors.toList()));

 

你可能感兴趣的:(JAVA日常技术点记录)