Still traversing the search collection? Java 8 can be solved in one line of code. It's really simple

background

Yes, if you want to search the List collection, you can use your own contains/ indexOf method to find elements before Java 8, but only complete elements, not fuzzy search or user-defined search. At this time, you can only traverse.

But now it's 2021. Are you still searching for List collection elements in the traditional way of traversing the collection?

Then you're too out. Using stream search elements in Java 8 can be done in one line of code, and it's really elegant! This article will not introduce Stream foundation, Stream series I have written a topic before, I do not understand the official account Java technology stack, and then read in the official account Java tutorial menu.

Stream search

In Java 8, you can convert a List set into a stream. Stream provides a series of powerful search functions, such as filter, find *, * Match and other methods. One line of code can handle the search.

For example, there are initial data:

The user information is: name, age and gender.

filter

Use the filter method to realize user-defined search, such as searching for all people whose names contain c in the list < user > collection:

 

Output results:

findFirst (find first)

Find the first element in the Stream. For example, search the first person in the list < user > collection with a history of more than 30 years:

/**
 * Set search first
 * @author: Stack length
 * @from: Official account Java technology stack
 */
@Test
public void findFirst() {
    System.out.println("Search for the first person over 30 years old");
    User user = list.stream().filter(u -> u.getAge() > 30).findFirst().get();
    System.out.println(user);
}

Output results:

The example is that you need to filter before findFirst, but if you don't want conditions, filter is not necessary.

Stream can not focus on the official account Java technology stack, and then read the Java series tutorial in the official account Java tutorial menu.

findAny (find any)

Find any element in the Stream, for example, search any person over 30 years old in the list < user > collection:

/**
 * Set search any one
 * @author: Stack length
 * @from: Official account Java technology stack
 */
@Test
public void findAny() {
    System.out.println("Search for anyone over 30 years old");
    User user = list.stream().filter(u -> u.getAge() > 30).findAny().get();
    System.out.println(user.getName());
}

Output results:

Why is the result the same as findFirst? What's the difference between and findFirst?

findAny is to find any element. If there is less data in the serial stream, the first element will generally be returned, but the result returned in the parallel stream is uncertain. It may be any element in the stream.

The purpose of findAny is to improve the performance of parallel stream operations, but if you need a fixed result, it is recommended to use findFirst.

All the complete sample source code of this article has been uploaded:

https://github.com/javastacks/javastack

anyMatch

Find out whether there are any matches in the elements in the Stream, such as whether there are XX people in the list < user > Set:

/**
 * The collection matches any element
 * @author: Stack length
 * @from: Official account Java technology stack
 */
@Test
public void anyMatch() {
    System.out.println("Does it exist Jack: " + list.stream().anyMatch(u -> u.getName().contains("Jack")));
    System.out.println("Does it exist Jet: " + list.stream().anyMatch(u -> u.getName().contains("Jet")));
}

Output results:

*The result returned by Match is of type boolean.

noneMatch (null match)

Find out whether there is no match in the elements in the Stream, such as whether XX people do not exist in the search list < user > collection:

/**
 * The collection does not match any element
 * @author: Stack length
 * @from: Official account Java technology stack
 */
@Test
public void noneMatch() {
    System.out.println("Does it not exist Jack: " + list.stream().noneMatch(u -> u.getName().contains("Jack")));
    System.out.println("Does it not exist Jet: " + list.stream().noneMatch(u -> u.getName().contains("Jack")));
}

Output results:

This method is the opposite of anyMatch.

allMatch

Find out whether all the elements in the Stream match. For example, search whether all the people in the list < user > collection are older than XX:

/**
 * Set matches all elements
 * @author: Stack length
 * @from: Official account Java technology stack
 */
@Test
public void allMatch() {
    System.out.println("All are older than 3:" + list.stream().allMatch(u -> u.getAge() > 2));
    System.out.println("All are older than 30:" + list.stream().allMatch(u -> u.getAge() > 30));
}

Output results:

summary

All the above search operations can be done in one line of code. Is it very simple and elegant?

Collections other than List can be converted to List, then converted to Stream, and then searched. For Stream, search is pediatrics. Have you learned to waste it?

Send it to your colleagues quickly to make your code more elegant!

All the complete sample source code of this article has been uploaded:

https://github.com/javastacks/javastack

Welcome to Star. The following Java examples will be provided here!

Source:
https://mp.weixin.qq.com/s/FI1Ue6_ut5YVLg-rmrYAqA

Author: Java technology stack

If you think this article is helpful to you, please forward it for attention and support

Friends who need Java interview materials can pay attention to the official account "w programming diary" for free access.
 

Keywords: Java Programmer

Added by WebGeek182 on Wed, 22 Dec 2021 11:41:37 +0200