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
Highly recommend this Streams tutorial – Processing Data with Java SE 8 Streams
References
- Java 8 cyclic inference in my case
- Java 8 Explained: Using Filters, Maps, Streams and Foreach to apply Lambdas to Java Collections!
- Java 8 forEach examples
- Java 8 Streams: multiple filters vs. complex condition
- Processing Data with Java SE 8 Streams
0 0
- Java 8 Streams filter examples
- Java 8 Streams filter
- Java 8 Streams filter 示例
- c2 Streams - Filter Streams
- Filter Streams
- Filter Streams
- Streams--Java 8 中的 Streams API 详解
- Java 8: Streams API
- Java 8 Streams map()
- Streams filter 例子
- java 8 之Streams 解说
- Java 8 Streams map() 示例
- Java 8 Streams map() 例子
- Java 8 Streams map 示例
- Java 8 Streams API 详解
- Java 8 forEach examples
- Understanding Java 8 Streams API---reference
- Java 8 集合的 Streams 使用示例
- 什么是面向对象,和面向过程的区别是什么
- ListView滑动删除 ,仿腾讯QQ
- Python 遍历字典
- CALayer
- h5+video+progress自定义播放
- Java 8 Streams filter examples
- Visual C++ 2008 runtime files are out of data
- CTS-testAccountManagement
- 链表与环的三个问题:判断是否有环,求环的长度,求环的起点
- linux scp
- iOS UILabel复制粘贴效果
- 汇编实现堆栈原理
- 假期的FLAG
- 3.操作系统引导——操作系统启动