Home  >  Article  >  Java  >  How to filter in java

How to filter in java

WBOY
WBOYforward
2023-04-18 23:04:011477browse

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");

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"

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete