1. 程式人生 > >【Java】JDK8 學習 第一章

【Java】JDK8 學習 第一章

package com.read.JDK8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Demo1_2_2_test {
    public static void main(String[] args) {
        List<People> inventory = Arrays.asList(
                new
People("wang", 10), new People("wang", 10), new People("zhao", 30), new People("qian", 21), new People("qian", 15) ); // 選出name == wang List<People> wangPeople = filterPeople(inventory, Demo1_2_2_test::isWang); // 類直接呼叫方法
System.out.println(wangPeople); // 選出name == qian 並且 age >15 List<People> qianPeo = filterPeople(inventory, (People p) -> "qian".equals(p.getName()) && p.getAge() > 15); System.out.println(qianPeo); // 流處理 List<People> tPeople = inventory.stream().filter(people -> people.getAge() > 15
).collect(Collectors.toList()); System.out.println(tPeople); } public static boolean isWang(People people) { return "wang".equals(people.getName()); } public static List<People> filterPeople(List<People> inventory, Predicate<People> peoplePredicate) { List<People> result = new ArrayList<>(); for (People p : inventory) { if (peoplePredicate.test(p)) { //對傳入的方法進行測試 判斷是否正確 result.add(p); } } return result; } public static class People { private String name; private int age; public People(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", age=" + age + '}'; } 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; } } } package com.read.JDK8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class Demo1_2_2_test { public static void main(String[] args) { List<People> inventory = Arrays.asList( new People("wang", 10), new People("wang", 10), new People("zhao", 30), new People("qian", 21), new People("qian", 15) ); // 選出name == wang List<People> wangPeople = filterPeople(inventory, Demo1_2_2_test::isWang); // 類直接呼叫方法 System.out.println(wangPeople); // 選出name == qian 並且 age >15 List<People> qianPeo = filterPeople(inventory, (People p) -> "qian".equals(p.getName()) && p.getAge() > 15); System.out.println(qianPeo); // 流處理 List<People> tPeople = inventory.stream().filter(people -> people.getAge() > 15).collect(Collectors.toList()); System.out.println(tPeople); } public static boolean isWang(People people) { return "wang".equals(people.getName()); } public static List<People> filterPeople(List<People> inventory, Predicate<People> peoplePredicate) { List<People> result = new ArrayList<>(); for (People p : inventory) { if (peoplePredicate.test(p)) { //對傳入的方法進行測試 判斷是否正確 result.add(p); } } return result; } public static class People { private String name; private int age; public People(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", age=" + age + '}'; } 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; } } }