Home > Java > javaTutorial > How to filter in java

How to filter in java

WBOY
Release: 2023-04-18 23:04:01
forward
1502 people have browsed it

Description

1. If the Lambda parameter generates a true value, the filter (Lambda that can generate a boolean result) will generate an element;

2. When false is generated , this element will no longer be used.

Example

Create a List collection:

List<String> stringCollection = new ArrayList<>();
stringCollection.add("ddd2");
stringCollection.add("aaa2");
stringCollection.add("bbb1");
stringCollection.add("aaa1");
stringCollection.add("bbb3");
stringCollection.add("ccc");
stringCollection.add("bbb2");
stringCollection.add("ddd1");
Copy after login

Filter's input parameter is Predicate, and Predicate is the intermediate operation of the assertion, which can filter out the necessary Collection elements. Its participation is also a Stream stream, and the filtered elements can be printed through the foreach terminal operation.

stringCollection
    .stream()
    .filter((s) -> s.startsWith("a"))
    .forEach(System.out::println);
 
// "aaa2", "aaa1"
Copy after login

The above is the detailed content of How to filter in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template