Java 8 Streams filter examples

博文出处:https://www.mkyong.com/java8/java-8-streams-filter-examples/


In this tutorial, we will show you a few of examples to demonstrate the use of Streams filter() with collect(),findAny() and orElse()

1. Streams filter() and collect()

1.1 Normal Java example to filter a List.

List<String> lines = Arrays.asList("spring", "node", "mkyong");
List<String> result = getFilterOutput(lines, "mkyong");
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 (!"mkyong".equals(line)) {
                result.add(line);
            }
        }
        return result;
    }

1.2 The equivalent example in Java 8, using stream.filter() to filter a List, and collect() to convert a stream.

import java.util.stream.Collectors;

//...
List<String> lines = Arrays.asList("spring", "node", "mkyong");

List<String> result = lines.stream() 			//convert list to stream
	.filter(line -> !"mkyong". equals (line))	//filters the line, equals to "mkyong"
	.collect(Collectors.toList());			//collect the output and convert streams to a List

result.forEach(System.out::println);			//output : spring node

2. Streams filter(), findAny() and orElse()

2.1 Normal Java example to get a Person by his name.

List<Person> persons = Arrays.asList(new Person("mkyong"),
	new Person("michael"), new Person("lawrence"));

Person result = getStudentByName(persons, "michael");

//...
    private Person getStudentByName(List<Person> persons, String name) {

        Person result = null;
        for (Person temp : persons) {
            if (name.equals(temp.getName())) {
                result = temp;
            }
        }
        return result;
    }

2.2 The equivalent example in Java 8, using stream.filter () to filter a List, and .findAny().orElse (null) to return an object conditional.

List<Person> persons = Arrays.asList(new Person("mkyong"),
		new Person("michael"), new Person("lawrence"));

Person result = persons.stream()				   // Convert to steam
	.filter(x -> "michael".equals(x.getName()))	// we want "michael" only
	.findAny()									// If 'findAny' then return found
	.orElse(null);								// If not found, return null

2.3 For multiple condition.

List<Person> persons = Arrays.asList(new Person("mkyong", 20),
	new Person("michael", 21), new Person("lawrence", 23));

Person result = persons.stream()
	.filter((x) -> "michael".equals(x.getName()) && 21==x.getAge())
	.findAny()
	.orElse(null);

//or like this
Person result = persons.stream()
	.filter(x -> {
		if("michael".equals(x.getName()) && 21==x.getAge()){
			return true;
		}
		return false;
	}).findAny()
	.orElse(null);

2.3 Extra, filter() and map() example.

List<Person> persons = Arrays.asList(new Person("mkyong", 20),
	new Person("michael", 21), new Person("lawrence", 23));

String name = persons.stream()
	.filter(x -> "michael".equals(x.getName()))
	.map(Person::getName)						//convert stream to String
	.findAny()
	.orElse("");

//name = michael
Note
Highly recommend this Streams tutorial –  Processing Data with Java SE 8 Streams

References

  1. Java 8 cyclic inference in my case
  2. Java 8 Explained: Using Filters, Maps, Streams and Foreach to apply Lambdas to Java Collections!
  3. Java 8 forEach examples
  4. Java 8 Streams: multiple filters vs. complex condition
  5. Processing Data with Java SE 8 Streams

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值