Java8中Streams过滤器示例分享

乔欣 Java经验 发布时间:2023-02-01 09:50:17 阅读数:13975 1
下文笔者讲述java8中Streams.filter(),collect(),findAny()和orElse()方法的功能简介说明

list过滤

jdk8之前的List过滤写法
 
 package com.java265.java8;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class BeforeJava8 {
 
    public static void main(String[] args) {
 
        List<String> lines = Arrays.asList("spring", "node", "java265");
        List<String> result = getFilterOutput(lines, "java265");
        for (String temp : result) {
            System.out.println(temp);    //output : spring, node
        }
 
    }
 
    private static List<String> getFilterOutput(List<String> lines, String filter) {
        List<String> result = new ArrayList<>();
        for (String line : lines) {
            if (!"java265".equals(line)) { // we dont like java265
                result.add(line);
            }
        }
        return result;
    }
}

使用stream.filter()过滤List
使用collect()将流转换为List

package com.java265.java8;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class TestClass {
 
    public static void main(String[] args) {
 
        List<String> lines = Arrays.asList("spring", "node", "java265");
 
        List<String> result = lines.stream()                // convert list to stream
                .filter(line -> !"java265".equals(line))     // we dont like java265
                .collect(Collectors.toList());              // collect the output and convert streams to a List
 
        result.forEach(System.out::println);                //output : spring, node
 
    }
}
 

filter(),findAny()和orElse()示例分享


//实体定义
package com.java265.java8;
 
public class Person {
 
    private String name;
    private int age;
 
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    //gettersm setters, toString
}

//jdk8之前的写法
package com.java265.java8;
 
import java.util.Arrays;
import java.util.List;
 
public class BeforeJava8 {
 
    public static void main(String[] args) {
 
        List<Person> persons = Arrays.asList(
                new Person("java265", 30),
                new Person("jack", 20),
                new Person("maomao",19)
        );
 
        Person result = getStudentByName(persons, "jack");
        System.out.println(result);
 
    }
 
    private static Person getStudentByName(List<Person> persons, String name) {
 
        Person result = null;
        for (Person temp : persons) {
            if (name.equals(temp.getName())) {
                result = temp;
            }
        }
        return result;
    }
}


//jdk8 stream的写法
package com.java265.java8;
 
import java.util.Arrays;
import java.util.List;
 
public class TestClass {
 
    public static void main(String[] args) {
 
        List<Person> persons = Arrays.asList(
                new Person("java265", 30),
                new Person("jack", 20),
                new Person("maomao",19)
        );
 
        Person result1 = persons.stream()                        // Convert to steam
                .filter(x -> "jack".equals(x.getName()))        // we want "jack" only
                .findAny()                                      // If 'findAny' then return found
                .orElse(null);                                  // If not found, return null
 
        System.out.println(result1);
        
        Person result2 = persons.stream()
                .filter(x -> "ahmook".equals(x.getName()))
                .findAny()
                .orElse(null);
 
        System.out.println(result2);
    }
}

Stream多条件过滤

package com.java265.java8;
 
import java.util.Arrays;
import java.util.List;
 
public class TestClass {
 
    public static void main(String[] args) {
 
        List<Person> persons = Arrays.asList(
                new Person("java265", 30),
                new Person("jack", 20),
                new Person("猫猫", 19)
        );
 
        Person result1 = persons.stream()
                .filter((p) -> "jack".equals(p.getName()) && 20 == p.getAge())
                .findAny()
                .orElse(null);
 
        System.out.println("result 1 :" + result1);
 
        //or like this
        Person result2 = persons.stream()
                .filter(p -> {
                    if ("jack".equals(p.getName()) && 20 == p.getAge()) {
                        return true;
                    }
                    return false;
                }).findAny()
                .orElse(null);
 
        System.out.println("result 2 :" + result2);
 
    }
 
 
}

Stream.filter()和Stream.map()示例

package com.java265.java8;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class TestClass {
 
    public static void main(String[] args) {
 
        List<Person> persons = Arrays.asList(
                new Person("java265", 30),
                new Person("jack", 20),
                new Person("猫猫",19)
        );
 
        String name = persons.stream()
                .filter(x -> "jack".equals(x.getName()))
                .map(Person::getName)     //convert stream to String
                .findAny()
                .orElse("");
 
        System.out.println("name : " + name);
 
        List<String> collect = persons.stream()
                .map(Person::getName)
                .collect(Collectors.toList());
 
        collect.forEach(System.out::println);
    }    
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202302/16752162615600.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者