In this article, we will discuss how to find all shortest Strings in an Arrays and List using Java 8 Stream and Collectors
1. Finding all shortest Strings in a List :
- Using Stream.min() method
- Using Stream.sorted() method
1.1 Using Stream.min() method
- A List contains multiple Strings where some String elements are of same length
- Get Stream from original List using List.stream() method and store length of String and actual String in a Map using Stream.collect(Collectors.groupingBy(String::length)) method as below,
- String length as Key
- List of Strings of same length as Value
- To get all shortest Strings in the List, use below methods which will return List which has minimum length
- Finally print List of Strings which has minimum length to the console
FindAllShortestStringInListUsingJavaStreamMinMethod.java
package in.bench.resources.shortest.string;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class FindAllShortestStringInListUsingJavaStreamMinMethod {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"James",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Binny",
"Spider",
"Lee",
"Anderson"
);
// 1.1 print to console
System.out.println("Original String List :- \n" + names + "\n");
// 2. get Map of length and its List<String>
Map<Integer, List<String>> map = names
.stream()
.collect(Collectors.groupingBy(String::length));
// 2.1 print Map to console
System.out.println("Length and its List of Strings :-");
map.entrySet().forEach(System.out::println);
// 3. get Shortest Strings from Map
List<String> shortestStrings = map
.entrySet()
.stream()
.min(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.get();
// 3.1 print Shortest Strings from List
System.out.println("\nShortest Strings in List :- \n" + shortestStrings);
}
}
Output:
Original String List :-
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Lee, Anderson]
Length and its List of Strings :-
3=[Bob, Lee]
4=[Bond]
5=[James, Alice, Binny]
6=[Spider]
7=[Whitman]
8=[Einstein, Anderson]
Shortest Strings in List :-
[Bob, Lee]
1.2 Using Stream.sorted() method
- A List contains multiple Strings where some String elements are of same length
- Get Stream from original List using List.stream() method and store length of String and actual String in a Map using Stream.collect(Collectors.groupingBy(String::length)) method as below,
- String length as Key
- List of Strings of same length as Value
- To get all shortest Strings in the List, use below methods which will return List which has minimum length
- Finally print List of Strings which has minimum length to the console
FindAllShortestStringInListUsingJavaStreamSortedMethod.java
package in.bench.resources.shortest.string;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class FindAllShortestStringInListUsingJavaStreamSortedMethod {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"James",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Binny",
"Spider",
"Lee",
"Anderson"
);
// 1.1 print to console
System.out.println("Original String List :- \n" + names + "\n");
// 2. get Map of length and its List<String>
Map<Integer, List<String>> map = names
.stream()
.collect(Collectors.groupingBy(String::length));
// 2.1 print Map to console
System.out.println("Length and its List of Strings :-");
map.entrySet().forEach(System.out::println);
// 3. get Shortest Strings from Map
List<String> shortestStrings = map
.entrySet()
.stream()
.sorted(Map.Entry.<Integer, List<String>>comparingByKey())
.map(Map.Entry::getValue)
.findFirst()
.get();
// 3.1 print Shortest Strings from List
System.out.println("\nShortest Strings in List :- \n" + shortestStrings);
}
}
Output:
Original String List :-
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Lee, Anderson]
Length and its List of Strings :-
3=[Bob, Lee]
4=[Bond]
5=[James, Alice, Binny]
6=[Spider]
7=[Whitman]
8=[Einstein, Anderson]
Shortest Strings in List :-
[Bob, Lee]
2. Finding all shortest Strings in an Arrays :
- Using Stream.min() method
- Using Stream.sorted() method
2.1 Using Stream.min() method
- This is very much same like 1.1 except that instead of getting stream from List, use stream from Arrays
- Use either of the below mentioned ways to get stream from Arrays
- Arrays.stream()
- Stream.of()
- See below syntax for getting all shortest Strings from Arrays using Stream.min() method
FindAllShortestStringInAnArraysUsingJavaStreamMinMethod.java
List<String> shortestStrings = Arrays
.stream(names)
.collect(Collectors.groupingBy(String::length))
.entrySet()
.stream()
.min(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.get();
2.2 Using Stream.sorted() method
- This is very much same like 1.2 except that instead of getting stream from List, use stream from Arrays
- Use either of the below mentioned ways to get stream from Arrays
- Arrays.stream()
- Stream.of()
- See below syntax for getting all shortest Strings from Arrays using Stream.sorted() method
FindAllShortestStringInAnArraysUsingJavaStreamSortedMethod.java
List<String> shortestStrings = Arrays
.stream(names)
.collect(Collectors.groupingBy(String::length))
.entrySet()
.stream()
.sorted(Map.Entry.<Integer, List<String>>comparingByKey())
.map(Map.Entry::getValue)
.findFirst()
.get();
Related Articles:
- Java – Find Largest number in an Arrays or List ?
- Java – Find Smallest number in an Arrays or List ?
- Java – How to get maximum element from ArrayList ?
- Java – How to get minimum element from ArrayList ?
- Java – Find 2nd Largest number in an Arrays or List ?
- Java – Find 2nd Smallest number in an Arrays or List ?
- Java – Find sum of Largest 2 numbers in an Arrays or List ?
- Java – Find sum of Smallest 2 numbers in an Arrays or List ?
- Java – Find 1st and Last elements in an Arrays ?
- Java – Find 1st and Last elements in a List or ArrayList ?
- Java – Find 1st and Last elements in a Set or HashSet ?
- Java – Find 1st and Last entries in a Map or HashMap ?
- Java – Find sum and average of a List or ArrayList ?
- Java – How to calculate sum and average of an Arrays ?
- Java – Find Longest String in an Arrays or List or Stream ?
- Java – Find Shortest String in an Arrays or List or Stream ?
- Java – Find 3rd Longest String in an Arrays or List or Stream ?
- Java – Find 3rd Shortest String in an Arrays or List or Stream ?
Happy Coding !!
Happy Learning !!