Introduction
Sorting a list of strings alphabetically is a common task in programming, especially in data processing and display operations. Java 8 provides an efficient way to sort lists using the Stream
API and lambda expressions. This guide will show you how to create a Java program that sorts a list of strings alphabetically.
Problem Statement
Create a Java program that:
- Takes a list of strings.
- Sorts the list alphabetically using Java 8's
Stream
API. - Returns and displays the sorted list.
Example 1:
- Input:
["Banana", "Apple", "Mango", "Grapes"]
- Output:
["Apple", "Banana", "Grapes", "Mango"]
Example 2:
- Input:
["Zebra", "Monkey", "Elephant", "Ant"]
- Output:
["Ant", "Elephant", "Monkey", "Zebra"]
Solution Steps
- Create the List: Initialize a list of strings.
- Sort the List Using Java 8 Streams: Convert the list to a stream, apply the
sorted()
method, and collect the result back into a list. - Display the Result: Print the sorted list.
Java Program
Java 8 Program to Sort a List of Strings Alphabetically
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Java 8 Program to Sort a List of Strings Alphabetically
* Author: https://www.javaguides.net/
*/
public class SortStrings {
public static void main(String[] args) {
// Example 1: Sorting a list of strings
List<String> fruits = Arrays.asList("Banana", "Apple", "Mango", "Grapes");
// Sort the list alphabetically using Java 8 streams
List<String> sortedFruits = fruits.stream()
.sorted() // Sorts in natural order (alphabetically)
.collect(Collectors.toList());
// Display the sorted list
System.out.println("Sorted fruits: " + sortedFruits);
// Example 2: Sorting another list of strings
List<String> animals = Arrays.asList("Zebra", "Monkey", "Elephant", "Ant");
// Sort the list alphabetically using Java 8 streams
List<String> sortedAnimals = animals.stream()
.sorted() // Sorts in natural order (alphabetically)
.collect(Collectors.toList());
// Display the sorted list
System.out.println("Sorted animals: " + sortedAnimals);
}
}
Explanation
Input: The program initializes lists of strings containing the names of fruits and animals.
Sorting Using Stream:
- The
stream()
method is called on the list to convert it into a stream. - The
sorted()
method sorts the elements in natural order, which for strings means alphabetical order. - The
collect(Collectors.toList())
method collects the sorted elements back into a list.
- The
Output: The program prints the alphabetically sorted list of strings.
Output Example
Example 1 (Fruits List):
Sorted fruits: [Apple, Banana, Grapes, Mango]
Example 2 (Animals List):
Sorted animals: [Ant, Elephant, Monkey, Zebra]
Custom Comparator for Case-Insensitive Sorting
If you want to perform case-insensitive sorting (e.g., treating "apple" and "Apple" as equal), you can modify the sorting step by using a custom comparator:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SortStringsCaseInsensitive {
public static void main(String[] args) {
// Example: Case-insensitive sorting
List<String> fruits = Arrays.asList("Banana", "apple", "Mango", "grapes");
// Sort the list alphabetically in a case-insensitive manner
List<String> sortedFruits = fruits.stream()
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toList());
// Display the sorted list
System.out.println("Case-insensitive sorted fruits: " + sortedFruits);
}
}
Output Example (Case-Insensitive Sorting):
Case-insensitive sorted fruits: [apple, Banana, grapes, Mango]
Conclusion
This Java 8 program demonstrates how to sort a list of strings alphabetically using streams. The sorted()
method provides a simple and effective way to achieve this, and by using Collectors.toList()
, the sorted stream can be collected back into a list. The program can be easily extended or modified, such as by using a custom comparator for case-insensitive sorting. This approach is efficient and leverages the power of Java 8's functional programming features.
Comments
Post a Comment
Leave Comment