Java 8 – Find all shortest Strings in List or Arrays or Stream ?

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

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

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:

Happy Coding !!
Happy Learning !!

Java - How to Sort String[] Arrays by its length ?
Java 8 – Find all longest Strings in List or Arrays or Stream ?