流(Stream)与集合类似,但集合中保存的是数据,而Stream中保存对集合或数组数据的操作。
代码演示:
public class TestStream1 {
public static void main(String[] args) {
//新建流
ArrayList<String> list = new ArrayList<>();
list.add("张三");
list.add("张三锋");
list.add("张无极");
list.add("赵梦露");
list.add("张黎");
list.add("田美丽");
//1 通过Collection对象的stream()方法(单线程)或parallelStream()方法(多线程)。
list.stream().filter(s->s.startsWith("张")).forEach(System.out::println);
//
//2 通过Arrays类的stream()方法。
int[] nums = {10,2,8,1,17,24};
Arrays.stream(nums).sorted().forEach(System.out::println);
//
//3 通过Stream接口的of()、iterate()、generate()方法。
Stream.of(5,3,6,13,1,8).sorted().forEach(System.out::println);
//
//4 通过IntStream、LongStream、DoubleStream接口中的of、range、rangeClosed方法。
//生成一个0~99的数组的流(含头不含尾)
IntStream.range(0,100).forEach(System.out::println);
//生成一个0~100的数组的流(含头含尾)
IntStream.rangeClosed(0,100).forEach(System.out::println);
}
}
代码演示:
public class TestStream2 {
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("张三",22,18000));
employees.add(new Employee("李四",23,20000));
employees.add(new Employee("王五",25,22000));
employees.add(new Employee("酒玖",20,25000));
employees.add(new Employee("张利",22,26000));
employees.add(new Employee("壮武吉",27,30000));
//1 filter、limit、skip、distinct、sorted
//filter 过滤
employees.stream()
.filter(e->e.getSalary()>=25000)
.forEach(System.out::println);
System.out.println("------------");
//limit 限制
employees.stream()
.limit(2)
.forEach(System.out::println);
//skip 跳过指定的元素个数
System.out.println("-----");
employees.stream()
.skip(2)
.limit(2)
.forEach(System.out::println);
System.out.println("-----");
//distinct: 去掉重复元素 hashcode和equals
employees.stream()
.distinct()
.forEach(System.out::println);
//sorted:排序
System.out.println("----------");
employees.stream()
.sorted((e1,e2)->Double.compare(e1.getSalary(),e2.getSalary()))
.forEach(System.out::println);
// 2 map映射
System.out.println("--------map-------");
//需求:遍历所有的姓名
employees.stream()
.map(Employee::getName)
.forEach(System.out::println);
}
static class Employee{
private String name;
private int age;
private int salary;
public Employee() {
}
public Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
}
代码演示:
public class TestStream3 {
public static void main(String[] args) {
//1 forEach、min、max、count
List<Integer> list=new ArrayList<>();
list.add(20);
list.add(18);
list.add(15);
list.add(22);
list.add(30);
//forEach 遍历元素
list.stream()
.forEach(System.out::println);
System.out.println("-------min-------");
//min 最小元素
//Optional: 封装元素的容器,目的避免空指针异常。
Optional<Integer> optional1 = list.stream()
.min((o1, o2) -> o1 - o2);
System.out.println(optional1.get());
System.out.println("-------max-------");
//min 最大元素
//Optional: 封装元素的容器,目的避免空指针异常。
Optional<Integer> optional2 = list.stream()
.max((o1, o2) -> o1 - o2);
System.out.println(optional2.get());
// count 元素个数
System.out.println("-----count----");
long count = list.stream().count();
System.out.println(count);
//reduce 规约,统计
//统计所有元素总和
System.out.println("----reduce------------");
Optional<Integer> sum = list.stream()
.reduce((x, y) -> x + y);
System.out.println(sum.get());
//collect 收集
//把所有的人的姓名转成List集合
List<Student> students=new ArrayList<>();
students.add(new Student("张三1",20,"男",100));
students.add(new Student("张三2",20,"男",100));
students.add(new Student("张三3",20,"男",100));
students.add(new Student("张三4",20,"男",100));
students.add(new Student("张三5",20,"男",100));
List<String> names = students.stream()
.map(Student::getName)
.collect(Collectors.toList());
System.out.println(names);
}
static class Student{
private String name;
private int age;
private String gender;
private double score;
public Student() {
}
public Student(String name, int age, String gender, double score) {
this.name = name;
this.age = age;
this.gender = gender;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
}