Java Stream forEach、filter和distinct的使用

Stream是Java 8的新特性,基于lambda表达式,是对集合对象功能的增强,它专注于对集合对象进行各种高效、方便聚合操作或者大批量的数据操作,提高了编程效率和代码可读性。本文主要介绍Java Stream中 forEach遍历循环、filter过滤筛选数据和distinct去除重复数据的使用,以及相关的示例代码。

1、forEach的使用

Stream()方法返回串行流,返回的Stream是根据当前数据的顺序进行遍历,parallelStream()方法返回并行流,并行能提高运行效率,但不能确保执行的顺序,如下,

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

public class Main {
   public static class Person {
        int age;
        Person (int age) {
            this.age = age;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
  }
public static void main(String[] args) {

    List<Person> list1 = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    //串行遍历
    list1.stream().forEach(o->{
       System.out.println("stream() :"+o.getAge());
    });
    
    List<Person> list2 = Arrays.asList(new Person(66),new Person(77), new Person(88), new Person(89), new Person(99));
    //并行遍历
    list2.parallelStream().forEach(o->{
       System.out.println("parallelStream() :"+o.getAge());
    });
    System.exit(0); //success
  }
}

2、filter的使用

使用filter()可以根据指定条件,筛选出我们需要获取的数据,如下,

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

public class Main {
   public static class Person {
        int age;
        Person (int age) {
            this.age = age;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
  }
public static void main(String[] args) {

    List<Integer> numbers = Arrays.asList(12, 74, 5, 8, 16);
    numbers.stream().filter(n -> n > 10).forEach(System.out::println);
    List<Person> list1 = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    list1.stream().filter(p -> p.getAge() < 40).forEach(f -> System.out.println("getAge(): "+f.getAge()));    
    System.exit(0); //success
  }
}

相关文档:Java Stream使用多个过滤器(filter)或复杂条件方法用法及简单写法代码

3、使用distinct去除重复数据

distinct()内部是调用equals方法,对比对象的值需要重写equals方法,否则只是比较引用值。

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

public class Main {
   public static class Person {
        int age;
        Person (int age) {
            this.age = age;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person p = (Person) o;
        return p.getAge() == this.age;
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(age);
    }
  }
public static void main(String[] args) {

    List<Integer> numbers = Arrays.asList(16, 8, 12, 74, 5, 8, 16);
    numbers.stream().distinct().forEach(System.out::println);
    List<Person> list1 = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(22),  new Person(55),  new Person(55));
    list1.stream().distinct().forEach(f -> System.out.println("getAge(): "+f.getAge()));    
    System.exit(0); //success
  }
}

推荐阅读
cjavapy编程之路首页