Java – Find all longest 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 longest Strings in List or ArrayList :

  • A 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 longest Strings in the List, get the last entry in the TreeMap by iterating using any loop
  • Finally print last entry of the Map to the console

FindAllLongestStringInListUsingJava.java

package in.bench.resources.longest.string;

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

public class FindAllLongestStringInListUsingJava {

	public static void main(String[] args) {

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


		// 1. names with different length
		List<String> names = Arrays.asList(
				"Bond",
				"James",
				"Einstein",
				"Alice",
				"Whitman",
				"Bob",
				"Binny",
				"Spider",
				"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 last entry
		for(Map.Entry<Integer, List<String>> entry : treeMap.entrySet()) {

			// find last entry
			lastEntry = entry;
		}


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

Output:

Original String List :- 
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Anderson]

Length and its List of Strings :-
3=[Bob]
4=[Bond]
5=[James, Alice, Binny]
6=[Spider]
7=[Whitman]
8=[Einstein, Anderson]

Longest Strings and its length in List :- 
8=[Einstein, Anderson]

2. Finding all longest Strings in an Arrays :

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

FindAllLongestStringInAnArraysUsingJava.java

package in.bench.resources.longest.string;

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

public class FindAllLongestStringInAnArraysUsingJava {

	public static void main(String[] args) {

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


		// 1. names with different length
		String[] names = new String[] {
				"Bond",
				"James",
				"Einstein",
				"Alice",
				"Whitman",
				"Bob",
				"Binny",
				"Spider",
				"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 last entry
		for(Map.Entry<Integer, List<String>> entry : treeMap.entrySet()) {

			// find last entry
			lastEntry = entry;
		}


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

Output:

Original String[] Arrays :- 
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Anderson]

Length and its List of Strings :-
3=[Bob]
4=[Bond]
5=[James, Alice, Binny]
6=[Spider]
7=[Whitman]
8=[Einstein, Anderson]

Longest Strings and its length in List :- 
8=[Einstein, Anderson]

Related Articles:

Happy Coding !!
Happy Learning !!

Java – Find all shortest Strings in List or Arrays ?
Java – Find Third Shortest String in an Arrays or List ?