Java 8 Streams filter examples

来源:互联网 发布:java 对象初始化 null 编辑:程序博客网 时间:2024/04/28 12:10

博文出处: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 Listresult.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 thisPerson 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
原创粉丝点击