[Java] How to operate List using Stream API

What is Stream API?

The Stream API is a feature introduced in Java8. It is for handling aggregates such as arrays and collections. It is a convenient API that can aggregate values ​​and process using data. By using Stream, the extended for statement can be simplified.

How to use Stream

Count the number of each element (count)

public class Sample {
	public static void main(String[] args) {

		List<String> JS = new ArrayList<>(Arrays.asList("Vue", "React", "Angular"));

		long count = JS.stream().count();
		System.out.println(count); // 3
	}
}

Judge and extract each element by condition (filter)

The following is the process to remove "Angular" and display the list.

public class Sample {
	public static void main(String[] args) {

		List<String> JS = new ArrayList<>(Arrays.asList("Vue", "React", "Angular"));

        JS.stream()
        .filter(s -> !s.equals("Angular"))
        .forEach(System.out::println); // Vue React
	}
}

Sorted elements

The second sorted method is in descending order because the reverseOrder method of the Comparator interface is specified as an argument.

public class Sample {
	public static void main(String[] args) {

	    List<String> JS = new ArrayList<>(Arrays.asList("Vue", "React", "Angular"));

		JS.stream()
			.sorted() //Ascending order because no arguments are passed
			.forEach(System.out::println); // Angular React Vue

		JS.stream()
			.sorted(Comparator.reverseOrder()) //In descending order
			.forEach(System.out::println); // Vue React Angular
	}
}

Do all element conditions match (allMatch)

The allMatch method returns true if they all match.

public class Sample {
	public static void main(String[] args) {

		List<Integer> num1 = new ArrayList<>(Arrays.asList(10,38,3));

        boolean a = num1.stream()
        		.allMatch(b -> b > 10); //Are all elements greater than 10
        System.out.println(a); //false

        boolean c = num1.stream()
        		.allMatch(d -> d > 1); //Are all elements greater than 1
        System.out.println(c); //true
	}
}

Does any of the element's conditions match (anyMatch)?

The anyMatch method returns true if at least one matches.

public class Sample {
	public static void main(String[] args) {

		List<Integer> num1 = new ArrayList<>(Arrays.asList(10,38,3));

        boolean a = num1.stream()
        		.anyMatch(b -> b > 40); //Are there any elements greater than 40?
        System.out.println(a); //false

        boolean c = num1.stream()
        		.anyMatch(d -> d > 30); //Are there any elements greater than 30
        System.out.println(c); //true
	}
}

Is there any element that matches the condition (noneMatch)?

The noneMatch method returns true if there are no matches.

public class Sample {
	public static void main(String[] args) {

		List<Integer> num1 = new ArrayList<>(Arrays.asList(10,38,3));

        boolean a = num1.stream()
        		.noneMatch(b -> b > 7); //Is there any element larger than 7?
        System.out.println(a); //false

        boolean c = num1.stream()
        		.noneMatch(d -> d > 40); //Is there any element larger than 40?
        System.out.println(c); //true
	}
}

reference

Interface Stream Java8 StreamAPI

Recommended Posts

[Java] How to operate List using Stream API
[For beginners] How to operate Stream API after Java 8
[Java] Introduction to Stream API
[java8] To understand the Stream API
[Introduction to Java] About Stream API
I tried using Java8 Stream API
[Java] How to use List [ArrayList]
Java 8 ~ Stream API ~ to start now
[Must-see for apprentice java engineer] How to use Stream API
How to play MIDI files using the Java Sound API
Java Stream API
How to sort a List using Comparator
Data processing using stream API from Java 8
Try using the Stream API in Java
[Java] How to add data to List (add, addAll)
[Java] How to calculate age using LocalDate
[Java] Stream API / map
How to use Java API with lambda expression
Java8 Stream API practice
[Java] Generate a narrowed list from multiple lists using the Stream API
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
How to play MIDI files using the Java Sound API (specify the MIDI device to use)
[Java] How to easily get the longest character string of ArrayList using stream
List processing to understand with pictures --java8 stream / javaslang-
Sample code to convert List to List <String> in Java Stream
I tried to operate SQS using AWS Java SDK
java: How to write a generic type list [Note]
Summary of Java communication API (1) How to use Socket
How to convert A to a and a to A using AND and OR in Java
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
Java Stream API cheat sheet
How to minimize Java images
How to write java comments
How to use java class
Java Stream API in 5 minutes
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
[Java] Stream API --Stream termination processing
How to set Java constants
[Java] Stream API --Stream intermediate processing
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
[Java] Stream API intermediate operation
How to authorize using graphql-ruby
How to handle exceptions coolly with Java 8 Stream or Optional
How to call and use API in Java (Spring Boot)
List processing to understand with pictures --java8 stream / javaslang --bonus
For Java beginners: List, Map, Iterator / Array ... How to convert?
Convert 2D array to csv format with Java 8 Stream API
How to study Java Silver SE 8