1、概念
多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值”。
2、筛选与切片
3、filter
package com.yinggu.demo11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import com.yinggu.data.EmployeeData;
import com.yinggu.domain.Employee;
* @author 黑猴子的家
* https://www.jianshu.com/u/37fd8e2dff4c
* 此类用于演示中间操作
* 1.中间操作相当于流水线,调用完一个中间操作APi,可以继续调用下一个中间操作API
* 2.中间操作需要搭配着终止操作一起执行,否则将不做任何处理,将这种现象称为“惰性操作”
* 特点: stream一旦做完终止操作,将 关闭,不能再继续进行中间操作
* 相关API:
* filter 过滤
* distinct 去重,注意必须重写hashcode和equals
* limit 限制最大个数
* skip 跳过
*
* map 映射
* flatMap 映射
* sorted 自然排序
* sorted(Comparator)定制排序
public class TestStreamMiddle {
List<Employee> list = EmployeeData.getData();
// 筛选
@Test
public void test1() {
// 1.获取Stream对象
Stream<Employee> streamlist = list.stream();
// 2.中间操作
// 2-1.中间操作——filter
Stream<Employee> filterstream = streamlist.filter(e -> e.getAge() > 30);
// 3.终止操作
filterstream.forEach(System.out::println);
}
// 筛选
/**
* 先打印全部,再打印过滤的
*/
@Test
public void test2() {
Stream<Employee> streamlist = list.stream();
streamlist.forEach(System.out::println);
System.out.println("--------------------------------------");
list.stream().filter(e -> e.getAge() > 30).forEach(System.out::println);
}
}
4、distinct
package com.yinggu.demo11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import com.yinggu.data.EmployeeData;
import com.yinggu.domain.Employee;
public class TestStreamMiddle {
List<Employee> list = EmployeeData.getData();
// distinct
/**
* 重写 Employee 类的hashCode和equals 方法
*/
@Test
public void test3() {
list.add(new Employee(100, "鸠摩智", 20, 10000));
list.add(new Employee(100, "鸠摩智", 20, 10000));
list.add(new Employee(100, "鸠摩智", 20, 10000));
list.add(new Employee(100, "鸠摩智", 20, 10000));
list.add(new Employee(100, "鸠摩智", 20, 10000));
Stream<Employee> streamlist = list.stream();
Stream<Employee> distinctstream = streamlist.distinct();
distinctstream.forEach(System.out::println);
}
}
5、limit
package com.yinggu.demo11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import com.yinggu.data.EmployeeData;
import com.yinggu.domain.Employee;
public class TestStreamMiddle {
List<Employee> list = EmployeeData.getData();
// limit
@Test
public void test4() {
// 1.获取Stream对象
Stream<Employee> streamlist = list.stream();
// 2.中间操作
// 2-1 .中间操作——limit
Stream<Employee> limitStream1 = streamlist.limit(3);
// Stream<Employee> limitStream2 = streamlist.limit(20);
// 3.终止操作
limitStream1.forEach(System.out::println);
}
}
6、skip 跳跃
package com.yinggu.demo11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import com.yinggu.data.EmployeeData;
import com.yinggu.domain.Employee;
public class TestStreamMiddle {
List<Employee> list = EmployeeData.getData();
// skip
@Test
public void test5() {
// 1.获取Stream对象
Stream<Employee> streamlist = list.stream();
// 2.中间操作
// 2-1.中间操作——skip
Stream<Employee> skipStream = streamlist.skip(2);
// 3.终止操作
skipStream.forEach(System.out::println);
}
}
7、连写
package com.yinggu.demo11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import com.yinggu.data.EmployeeData;
import com.yinggu.domain.Employee;
public class TestStreamMiddle {
List<Employee> list = EmployeeData.getData();
// 连写
@Test
public void test6() {
list.stream().filter(e -> e.getSalary() > 1000)
.limit(3).distinct()
.skip(1)
.forEach(System.out::println);
}
}