Stream之第二步中间操作

List employees = Arrays.asList(
        new Employee("张小", 19, 1999.99),
        new Employee("李而", 18, 2999.99),
        new Employee("张三", 20, 3999.99),
        new Employee("张四", 50, 4999.99),
        new Employee("王五", 38, 5555.55),
        new Employee("赵六", 30, 6666.66),
        new Employee("田七", 21, 7777.77),
        new Employee("不小", 21, 7777.77),
        new Employee("田七", 21, 7777.77),
        new Employee("田七", 21, 7777.77)
);

//中间操作
/**
 * 筛选与切片
 * filter--接收Lambda,从流中排除某些元素
 * limit--载断流,例其元素不超过给定数量
 * skip(n)--跳过元素,返回一个扔掉前n个元素的流。若流中元素不足n个,则返回一个空流。与limit(n)互补
 * distinct--筛选,通过流所生成元素的hashCode()和equals()去除重复元素
 */
//内部迭代:迭代操作由Stream API完成
@Test
public void test2(){
    /*Stream employeeStream = employees.stream()
            .filter((e) -> e.getAge() > 35);
    employeeStream.forEach(System.out::println);*/

    //中间操作:不会执行任何操作
    Stream employeeStream = employees.stream()
            .filter((e) ->{
                System.out.println("Stream API 的中间操作");
                return e.getAge()>35;
            });
    //终止操作:一次性执行全部内容,即“惰性求值"
    employeeStream.forEach(System.out::println);
}

//外部迭代:
@Test
public void test3(){
    Iterator iterator = employees.iterator();
    while (iterator.hasNext()){
        System.out.println(iterator.next());
    }
}

@Test
public void test5(){
    employees.stream()
            .filter((e)->{
                System.out.println("短路!");//&& ||
                return e.getSalary()>5000;
            })
            .limit(2)
            .forEach(System.out::println);
}

@Test
public void test6(){
    employees.stream()
            .filter((e)->e.getSalary()>5000)
            .skip(2)
            .distinct() //若起作用,必须重写hashCode()和equals()方法
            .forEach(System.out::println);
}

/**
 * 映射
 * map---接收Lambda,将元素转换成其他形式或提取信息。接收一个函数作为函数,该函数会被应用到每个元素上,并将其形成一个新的元素
 * flatMap--接收一个函数作为参数,将流中每个值换成另一个流,然后把所有流连接成一个流
 */
@Test
public void test7(){
    List list=Arrays.asList("aaa","bbb","ccc","ddd","eee");
    list.stream()
            .map((str)->str.toUpperCase())
            .forEach(System.out::println);
    System.out.println("-----------------------");
     /*employees.stream()
             .map(Employee::getName)
             .forEach(System.out::println);
     System.out.println("-----------------------");
     Stream> stream = list.stream()
            .map(TestStreamAPI1::filterCharacter);
     stream.forEach((sm)->{
         sm.forEach(System.out::println);
     });*/
    System.out.println("-----------------------");
    Stream sm = list.stream()
            .flatMap(TestStreamAPI2::filterCharacter);
    sm.forEach(System.out::println);
}
@Test
public void test8(){
    List list=Arrays.asList("aaa","bbb","ccc","ddd","eee");
    List list2=new ArrayList<>();
    list2.add(11);
    list2.add(22);
    //list2.add(list); //[11, 22, [aaa, bbb, ccc, ddd, eee]]
    list2.addAll(list); //[11, 22, aaa, bbb, ccc, ddd, eee]
    System.out.println(list2);
}
public static Stream filterCharacter(String str){//add(Object obj) addAll(Collection coll)
    List list=new ArrayList<>();
    for (Character ch:str.toCharArray()) {
        list.add(ch);
    }
    return  list.stream();
}

/**
 * 排序
 * sorted()  --自然排序(Comparable)
 * sorted(Comparator com)  --定制排序(Comparator)
 */
@Test
public void test9(){
    List list= Arrays.asList("ccc","aaa","bbb","eee","ddd");
    list.stream()
            .sorted()
            .forEach(System.out::println);
    System.out.println("-----------------------");
    employees.stream()
            .sorted((e1,e2)->{
                if (e1.getAge().equals(e2.getAge())){
                    return e1.getName().compareTo(e2.getName());
                }else {
                    return -e1.getAge().compareTo(e2.getAge());
                }
            }).forEach(System.out::println);
}

你可能感兴趣的:(Stream之第二步中间操作)