Java 流過濾器

2018-03-12 17:43 更新

Java流 - Java流過濾器


過濾操作產(chǎn)生過濾流,即輸入流的子集,其元素對于指定的謂詞計算為true。

predicate是一個接受元素并返回布爾值的函數(shù)。

過濾的流具有與輸入流相同的類型。

如果predicate對所有元素求值為false,它將生成一個空流。

以下代碼僅在女性中使用員工流和過濾器。 它將女性映射到他們的名字,并將它們打印在標(biāo)準(zhǔn)輸出上。

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    Employee.persons()
            .stream()
            .filter(Employee::isFemale)
            .map(Employee::getName)
            .forEach(System.out::println);
  }
}

class Employee {
  public static enum Gender {
    MALE, FEMALE
  }

  private long id;
  private String name;
  private Gender gender;
  private LocalDate dob;
  private double income;

  public Employee(long id, String name, Gender gender, LocalDate dob,
      double income) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.dob = dob;
    this.income = income;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Gender getGender() {
    return gender;
  }

  public boolean isMale() {
    return this.gender == Gender.MALE;
  }

  public boolean isFemale() {
    return this.gender == Gender.FEMALE;
  }

  public void setGender(Gender gender) {
    this.gender = gender;
  }

  public LocalDate getDob() {
    return dob;
  }

  public void setDob(LocalDate dob) {
    this.dob = dob;
  }

  public double getIncome() {
    return income;
  }

  public void setIncome(double income) {
    this.income = income;
  }

  public static List<Employee> persons() {
    Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
        Month.JANUARY, 1), 2343.0);
    Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
        Month.JULY, 21), 7100.0);
    Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
        Month.MAY, 29), 5455.0);
    Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
        Month.OCTOBER, 16), 1800.0);
    Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
        Month.DECEMBER, 13), 1234.0);
    Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
        Month.JUNE, 9), 3211.0);

    List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

    return persons;
  }

  @Override
  public String toString() {
    String str = String.format("(%s, %s,  %s,  %s,  %.2f)\n", id, name, gender,
        dob, income);
    return str;
  }
}

上面的代碼生成以下結(jié)果。


例2

對于下面的代碼

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    Employee.persons()
    .stream()
    .filter(Employee::isMale)
    .filter(p ->  p.getIncome() > 5000.0)
    .map(Employee::getName)
    .forEach(System.out::println);
  }
}
...
...

我們可以將兩個濾波器組合為一個具有AND運(yùn)算。

public class Main {
  public static void main(String[] args) {
    Employee.persons()
    .stream()
    .filter(p ->  p.isMale() &&   p.getIncome() > 5000.0)
    .map(Employee::getName)
    .forEach(System.out::println);
  }
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號