Java – Find all shortest Strings in List or Arrays ?

In this article, we will discuss how to find all longest Strings in an Arrays and List using Java

1. Finding all shortest Strings in List or ArrayList :

  • List contains multiple Strings where some String elements are of same length
  • Iterate through List and put Key/Value pairs in the newly created TreeMap as below,
    • String length as Key
    • Add actual String to List and then this List as Value
  • Note:- TreeMap stores Map entries in the Ascending-order of Keys
  • Print TreeMap to the console to display all Strings with its length
  • To get all shortest Strings in the List, get the first entry in the TreeMap by iterating using any loop
  • Finally print first entry of the Map to the console

FindAllShortestStringInListUsingJava.java

package in.bench.resources.shortest.string;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class FindAllShortestStringInListUsingJava {

	public static void main(String[] args) {

		// local variables
		Map.Entry<Integer, List<String>> firstEntry = null;


		// 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. create TreeMap for length and List<String>
		Map<Integer, List<String>> treeMap = new TreeMap<>();


		// 2.1 iterate through original List
		for(String name : names) {

			// check if this length already present in the above created Map
			if(treeMap.containsKey(name.length())) {

				// add String of same length in already created List
				treeMap.get(name.length()).add(name);
			}
			else {

				// create List and add first item
				List<String> list = new ArrayList<String>();
				list.add(name);

				// put newly created List into Map
				treeMap.put(name.length(), list);
			}
		}


		// 2.2 print Map to console
		System.out.println("Length and its List of Strings :-");
		for(Map.Entry<Integer, List<String>> entry : treeMap.entrySet()) {

			System.out.println(entry);
		}


		// 3. iterate through Map and get first entry
		for(Map.Entry<Integer, List<String>> entry : treeMap.entrySet()) {

			// find last entry
			firstEntry = entry;
			break;
		}


		// 3.1 print shortest Strings from List
		System.out.println("\nShortest Strings and its length in List :- \n" + firstEntry);
	}
}

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 and its length in List :- 
3=[Bob, Lee]

2. Finding all shortest Strings in an Arrays :

  • This illustration is very much same like above demonstration with only change is that Arrays is used instead of List

FindAllShortestStringInAnArraysUsingJava.java

package in.bench.resources.shortest.string;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class FindAllShortestStringInAnArraysUsingJava {

	public static void main(String[] args) {

		// local variables
		Map.Entry<Integer, List<String>> firstEntry = null;


		// 1. names with different length
		String[] names = new String[] {
				"Bond",
				"James",
				"Einstein",
				"Alice",
				"Whitman",
				"Bob",
				"Binny",
				"Spider",
				"Lee",
				"Anderson"
		};


		// 1.1 print to console
		System.out.println("Original String[] Arrays :- \n" 
				+ Arrays.toString(names) + "\n");


		// 2. create TreeMap for length and List<String>
		Map<Integer, List<String>> treeMap = new TreeMap<>();


		// 2.1 iterate through original List
		for(String name : names) {

			// check if this length already present in the above created Map
			if(treeMap.containsKey(name.length())) {

				// add String of same length in already created List
				treeMap.get(name.length()).add(name);
			}
			else {

				// create List and add first item
				List<String> list = new ArrayList<String>();
				list.add(name);

				// put newly created List into Map
				treeMap.put(name.length(), list);
			}
		}


		// 2.2 print Map to console
		System.out.println("Length and its List of Strings :-");
		for(Map.Entry<Integer, List<String>> entry : treeMap.entrySet()) {

			System.out.println(entry);
		}


		// 3. iterate through Map and get first entry
		for(Map.Entry<Integer, List<String>> entry : treeMap.entrySet()) {

			// find last entry
			firstEntry = entry;
			break;
		}


		// 3.1 print shortest Strings from List
		System.out.println("\nShortest Strings and its length in List :- \n" + firstEntry);
	}
}

Output:

Original String[] Arrays :- 
[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 and its length in List :- 
3=[Bob, Lee]

Related Articles:

Happy Coding !!
Happy Learning !!

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