[Java] Sort the list using streams and lambda expressions

Sort the list using streams and lambda expressions

By using streams and lambda expressions, you can reduce the coding of list sorting.

Code example


package sample;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
	public static void main(String[] args) {
		//List setting (method without add method)
		List<String> sampleList = List.of("Ishikari","Sorachi","Shiribeshi","Iburi","Hidaka","Hiyama","Oshima","Rumoi","Kamikawa","Soya","Tokachi","Kushiro","Okhotsk","Nemuro");

		//Check the created list samples


		//Sort the created list samples using streams and lambda expressions
		// A → Z
		List<String> sortedSampleList1 = sampleList.stream().sorted((a,b) -> (a.compareTo(b))).collect(Collectors.toList());
		System.out.println("A → Z");

		// Z → A
		System.out.println("Z → A");
		List<String> sortedSampleList2 = sampleList.stream().sorted((a,b) -> (b.compareTo(a))).collect(Collectors.toList());

Output result

[Ishikari, Sorachi, Shiribeshi, Iburi, Hidaka, Hiyama, Oshima, Rumoi, Kamikawa, Soya, Tokachi, Kushiro, Okhotsk, Nemuro]

A → Z
[Hidaka, Hiyama, Iburi, Ishikari, Kamikawa, Kushiro, Nemuro, Okhotsk, Oshima, Rumoi, Shiribeshi, Sorachi, Soya, Tokachi]

Z → A
[Tokachi, Soya, Sorachi, Shiribeshi, Rumoi, Oshima, Okhotsk, Nemuro, Kushiro, Kamikawa, Ishikari, Iburi, Hiyama, Hidaka]


In programming (Java) of the Fundamental Information Technology Engineer Examination, code using stream lambda expressions is sometimes given as a subject.

Recommended Posts

[Java] Sort the list using streams and lambda expressions
The origin of Java lambda expressions
Nowadays Java lambda expressions and Stream API
[Java 7] Divide the Java list and execute the process
[Java] Understand the difference between List and Set
Effective Java 3rd Edition Chapter 7 Lambda and Streams
[java] sort in list
Understand Java 8 lambda expressions
About Java lambda expressions
Explain Java 8 lambda expressions
Use Java lambda expressions outside of the Stream API
Learn for the first time java # 3 expressions and operators
[Java] Introduction to lambda expressions
Java List Group, Sort, etc.
Bubble sort using ArrayList (JAVA)
Implement Thread in Java and try using anonymous class, lambda
[Java] Comparison method of character strings and comparison method using regular expressions
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Problems with Dijkstra's algorithm using PriorityQueue and adjacency list (java)
Java lambda expressions learned with Comparator
[Java] Delete the elements of List
Sort List in descending order in Java and generate a new List non-destructively
[Introduction to Java] About lambda expressions
Java for beginners, expressions and operators 1
Find out the list of fonts available in AWS Lambda + Java
Sort a List of Java objects
Java comparison using the compareTo () method
Java for beginners, expressions and operators 2
Getting started with Java lambda expressions
How to use Java lambda expressions
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
[Java] Generate a narrowed list from multiple lists using the Stream API
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
Display Japanese calendar and days of the week using java8 standard class
How to sort a List using Comparator
Now let's recap the Java lambda expression
I tried to summarize Java lambda expressions
Newcomer training using the Web-Basic programming using Java-
Try using the Stream API in Java
Examples of lambda, sort, and anonymous functions
Sort by multiple conditions using Java Stream
[Java] for Each and sorted in Lambda
Java and Derby integration using JDBC (using NetBeans)
How to sort the List of SelectItem
Arrylist and linked list difference in java
[Java8] Search the directory and get the file
Try using the Wii remote with Java
[Java] Contents of Collection interface and List interface
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Impressions and doubts about using java for the first time in Android Studio
When using a list in Java, java.awt.List comes out and an error occurs
Be careful with requests and responses when using the Serverless Framework in Java
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Regarding the transient modifier and serialization in Java
Interact with LINE Message API using Lambda (Java)
Create API using Retrofit2, Okhttp3 and Gson (Java)
Install java and android-sdk on Mac using homebrew
ChatWork4j for using the ChatWork API in Java
[Java] Summary of how to abbreviate lambda expressions
[Java] The confusing part of String and StringBuilder
Newcomer training using the Web-Basic programming / classes using Java-