java 8 Lambda 表达式和 streamAPI小练习

package com.mall.demo.controller;

import java.util.*;
import java.util.stream.Collectors;

public class Java8Tester {
public static void main(String[] args) {
ArrayList names1 = new ArrayList();
names1.add(“Google”);
names1.add(“Runoob”);
names1.add(“Taobao”);
names1.add(“Baidau”);
names1.add(“Baidu”);
names1.add(“Baicdu”);
names1.add(“Sina”);
names1.add("");
names1.add("");
names1.add("");

    Java8Tester java8Tester = new Java8Tester();
    System.out.println("未排序"+names1);
    java8Tester.useJava8(names1);
    System.out.println("使用java8 排序"+names1);
    long count = names1.stream().filter(s -> s.isEmpty()).count();
    System.out.println("空字符串数量为"+count);
    long count1 = names1.stream().filter(s -> s.length()==4).count();
    System.out.println("字符串长度为4有"+count1+"个");
    List collect = names1.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
    System.out.println("筛选后的列表"+collect);
    //合并字符串
    String collect1 = names1.stream().filter(s -> !s.isEmpty()).collect(Collectors.joining("-"));
    System.out.println("- __ -"+collect1);

    List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
    Object collect2 = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
    System.out.println("列表"+collect2);
    IntSummaryStatistics intSummaryStatistics = numbers.stream().mapToInt(i -> i).summaryStatistics();
    System.out.println("summaryStatistics"+intSummaryStatistics);

    Random random = new Random();
    random.ints().limit(10).forEach(System.out::println);

}
private void useJava8(List names){
    Collections.sort(names,(o1, o2) -> o1.compareTo(o2));
}

}


你可能感兴趣的:(学习笔记)