java8--stream

public class ListStreamDemo{
public static void main(String[] args) {
Integer[] arr = {1,1,3,7};
List<Integer> ints = Arrays.asList(arr);
int max = ints.stream().mapToInt(Integer::intValue).max().getAsInt(); //获取集合的最大值
System.out.println(max);
int sum = ints.stream().mapToInt(Integer::intValue).sum(); //获取集合的和
System.out.println(sum);

List<Integer> sorted = ints.stream().sorted().collect(Collectors.toList()); //正序
System.out.println(sorted);
List<Integer> reserverOrder = ints.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); //倒序
List<Integer> reserverOrder1 = ints.stream().sorted(Comparator.comparing(Integer::intValue).reversed()).collect(Collectors.toList()); //倒叙2
System.out.println(reserverOrder);
System.out.println(reserverOrder1);

    System.out.println("================求两个List集合的交集==========================");
     List<Integer> list1 = {1,3,4,4,5,6};
     List<Integer> list2 = {1,6};
    List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);

List<String> listDistinct = list1.stream().distinct().collect(toList());
System.out.println("---去重 listDistinct---");
   listDistinct.parallelStream().forEachOrdered(System.out :: println);
  
    
 


}
}
posted @ 2021-05-06 10:19  umin  阅读(57)  评论(0编辑  收藏  举报