In this article, we will discuss how to find longest String in an Arrays and List using Java
1. Finding Longest String in List or ArrayList :
We will find Longest String in a List or ArrayList in different ways
- Using standard for-loop in Java
- Using enhanced for-each loop in Java 5
- Using Java 8 Stream and Collectors
- Using Collections.sort() method
- Using Arrays.sort() method
1.1 Using standard for-loop in Java
- First, assume 1st element of the list as longest String
- Iterate through remaining elements in the List starting with index 1 till end
- Compare each iterating elements with assumed Longest element
- If iterating element is the longest when comparing with assumed longest element then set/assign this element as Longest element
- Likewise continue till the end of the loop and set/assign accordingly
- Finally print Longest String and its length from the List
FindLongestStringInListUsingJava.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package in.bench.resources.longest.string; import java.util.Arrays; import java.util.List; public class FindLongestStringInListUsingJava { public static void main(String[] args) { // local variables String longestStr = null ; int lengthofLongestStr = 0 ; int indexPosition = 0 ; // 1. names with different length List<String> names = Arrays.asList( "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" ); // 1.1 print to console= System.out.println( "Original String List :- \n" + names); // 2. assume first element as Longest lengthofLongestStr = names.get( 0 ).length(); // 2.1 Iterate and find Longest name for ( int index = 1 ; index < names.size(); index++) { if (names.get(index).length() > lengthofLongestStr) { lengthofLongestStr = names.get(index).length(); indexPosition = index; } } // 2.2 get Longest String after above iteration longestStr = names.get(indexPosition); System.out.println( "\nLongest String is " + longestStr + " at index-position " + indexPosition); // 2.3 find length of Longest name System.out.println( "\nLength of Longest String is = " + lengthofLongestStr); } } |
Output:
1 2 3 4 5 6 | Original String List :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is Einstein at index-position 1 Length of Longest String is = 8 |
1.2 Using enhanced for-each loop in Java 5
- This illustration is very similar like previous 1.1 except that it uses enhanced for each loop introduced in Java 1.5 version
- But code looks very elegant when comparing with previous example
FindLongestStringInListUsingJava5ForEachLoop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package in.bench.resources.longest.string; import java.util.Arrays; import java.util.List; public class FindLongestStringInListUsingJava5ForEachLoop { public static void main(String[] args) { // local variables String longestStr = null ; // 1. names with different length List<String> names = Arrays.asList( "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" ); // 1.1 print to console= System.out.println( "Original String List :- \n" + names); // 2. iterate using Java 5 and find Longest String for (String name : names) { if ( null == longestStr || name.length() > longestStr.length()) { longestStr = name; } } // 2.1 print Longest String after above iteration System.out.println( "\nLongest String is = " + longestStr); // 2.2 find length of Longest name System.out.println( "\nLength of Longest String is = " + longestStr.length()); } } |
Output:
1 2 3 4 5 6 | Original String List :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is = Einstein Length of Longest String is = 8 |
1.3 Using Java 8 Stream and Collectors
- With Java 1.8 version we can find Longest String using Stream methods like
- Stream.max() method
- Stream.reduce() method
- Stream.collect() method
- Stream.sorted() method
- IntStream.summaryStatistics() method
- Collections.max() method
- Read Java 8 – Find Longest String in an Arrays or List or Stream ?
1.4 Using Collections.sort() method
- Collections.sort() method accepts 2 input-arguments where
- 1st argument is the actual String list from which longest String has to be find
- 2nd argument is the Comparator for sorting which here is according to String length
- Get the last element using index-position which gives longest String after sorting
- Finally print Longest String and its length from the List
FindLongestStringInListUsingCollectionSortingByLength.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package in.bench.resources.longest.string; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class FindLongestStringInListUsingCollectionSortingByLength { public static void main(String[] args) { // local variables String longestStr = null ; // 1. names with different length List<String> names = Arrays.asList( "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" ); // 1.1 print to console= System.out.println( "Original String List :- \n" + names); // 2. sort List according to String length Collections.sort(names, new Comparator<String>() { @Override public int compare(String str1, String str2) { return str1.length() - str2.length(); } }); // 2.1 get Longest String after above sorting longestStr = names.get(names.size() - 1 ); System.out.println( "\nLongest String is = " + longestStr); // 2.2 find length of Longest name System.out.println( "\nLength of Longest String is = " + longestStr.length()); } } |
Output:
1 2 3 4 5 6 | Original String List :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is = Einstein Length of Longest String is = 8 |
1.5 Using Arrays.sort() method
- Arrays.sort() method accepts 2 input-arguments where
- 1st argument is the arrays from which longest String has to be find (convert actual String list to Arrays using list.toArray(new String[0]); method)
- 2nd argument is the Comparator for sorting which here is according to String length
- Get the last element using index-position which gives longest String after sorting
- Finally print Longest String and its length from the List
FindLongestStringInListUsingArraysSortingByLength.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package in.bench.resources.longest.string; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class FindLongestStringInListUsingArraysSortingByLength { public static void main(String[] args) { // local variables String longestStr = null ; // 1. names with different length List<String> names = Arrays.asList( "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" ); // 1.1 print to console= System.out.println( "Original String List :- \n" + names); // 2. convert List to Arrays String[] sortedArrays = names.toArray( new String[ 0 ]); // 2.1 sort converted Arrays according to String length Arrays.sort(sortedArrays, new Comparator<String>() { @Override public int compare(String str1, String str2) { return str1.length() - str2.length(); } }); // 2.2 get Longest String after above sorting longestStr = sortedArrays[sortedArrays.length - 1 ]; System.out.println( "\nLongest String is = " + longestStr); // 2.3 find length of Longest name System.out.println( "\nLength of Longest String is = " + longestStr.length()); } } |
Output:
1 2 3 4 5 6 | Original String List :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is = Einstein Length of Longest String is = 8 |
2. Finding Longest String in an Arrays :
We will find Longest String in an Arrays in different ways
- Using standard for-loop in Java
- Using enhanced for-each loop in Java 5
- Using Java 8 Stream and Collectors
- Using Arrays.sort() method
- Using Collections.sort() method
2.1 Using standard for-loop in Java
- First, assume 1st element of an Arrays as longest String
- Iterate through remaining elements in an Arrays starting with index 1 till end
- Compare each iterating elements with assumed Longest element
- If iterating element is the longest when comparing with assumed longest element then set/assign this element as Longest element
- Likewise continue till the end of the loop and set/assign accordingly
- Finally print Longest String and its length from Arrays
FindLongestStringInAnArraysUsingJava.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package in.bench.resources.longest.string; import java.util.Arrays; public class FindLongestStringInAnArraysUsingJava { public static void main(String[] args) { // local variables String longestStr = null ; int lengthofLongestStr = 0 ; int indexPosition = 0 ; // 1. names with different length String[] names = new String[] { "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" }; // 1.1 print to console= System.out.println( "Original String[] Arrays :- \n" + Arrays.toString(names)); // 2. assume first element as Longest lengthofLongestStr = names[ 0 ].length(); // 2.1 Iterate and find Longest name for ( int index = 1 ; index < names.length; index++) { if (names[index].length() > lengthofLongestStr) { lengthofLongestStr = names[index].length(); indexPosition = index; } } // 2.2 get Longest String after above iteration longestStr = names[indexPosition]; System.out.println( "\nLongest String is " + longestStr + " at index-position " + indexPosition); // 2.3 find length of Longest name System.out.println( "\nLength of Longest String is = " + lengthofLongestStr); } } |
Output:
1 2 3 4 5 6 | Original String[] Arrays :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is Einstein at index-position 1 Length of Longest String is = 8 |
2.2 Using enhanced for-each loop in Java 5
- This illustration is very similar like previous 2.1 except that it uses enhanced for each loop introduced in Java 1.5 version
- But code looks very elegant when comparing with previous example
FindLongestStringInAnArraysUsingJava5ForEach.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package in.bench.resources.longest.string; import java.util.Arrays; public class FindLongestStringInAnArraysUsingJava5ForEach { public static void main(String[] args) { // local variables String longestStr = null ; // 1. names with different length String[] names = new String[] { "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" }; // 1.1 print to console= System.out.println( "Original String[] Arrays :- \n" + Arrays.toString(names)); // 2. iterate using Java 5 and find Longest String for (String name : names) { if ( null == longestStr || name.length() > longestStr.length()) { longestStr = name; } } // 2.1 print Longest String after above iteration System.out.println( "\nLongest String is = " + longestStr); // 2.2 find length of Longest name System.out.println( "\nLength of Longest String is = " + longestStr.length()); } } |
Output:
1 2 3 4 5 6 | Original String[] Arrays :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is = Einstein Length of Longest String is = 8 |
2.3 Using Java 8 Stream and Collectors
- With Java 1.8 version we can find Longest String using Stream methods like
- Arrays.stream.max() method
- Arrays.stream.reduce() method
- Arrays.stream.collect() method
- Arrays.stream.sorted() method
- IntStream.summaryStatistics() method
- Collections.max() method
- Read Java 8 – Find Longest String in an Arrays or List or Stream ?
2.4 Using Arrays.sort() method
- Arrays.sort() method accepts 2 input-arguments where
- 1st argument is the arrays from which longest String has to be find
- 2nd argument is the Comparator for sorting which here is according to String length
- Get the last element using index-position which gives longest String after sorting
- Finally print Longest String and its length from Arrays
FindLongestStringInAnArraysUsingArraysSortingByLength.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package in.bench.resources.longest.string; import java.util.Arrays; import java.util.Comparator; public class FindLongestStringInAnArraysUsingArraysSortingByLength { public static void main(String[] args) { // local variables String longestStr = null ; // 1. names with different length String[] names = new String[] { "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" }; // 1.1 print to console= System.out.println( "Original String[] Arrays :- \n" + Arrays.toString(names)); // 2. sort converted Arrays according to String length Arrays.sort(names, new Comparator<String>() { @Override public int compare(String str1, String str2) { return str1.length() - str2.length(); } }); // 2.2 get Longest String after above sorting longestStr = names[names.length - 1 ]; System.out.println( "\nLongest String is = " + longestStr); // 2.3 find length of Longest name System.out.println( "\nLength of Longest String is = " + longestStr.length()); } } |
Output:
1 2 3 4 5 6 | Original String[] Arrays :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is = Einstein Length of Longest String is = 8 |
2.5 Using Collections.sort() method
- Collections.sort() method accepts 2 input-arguments where
- 1st argument is the list from which longest String has to be find (convert Arrays into List using Arrays.asList(); method)
- 2nd argument is the Comparator for sorting which here is according to String length
- Get the last element using index-position which gives longest String after sorting
- Finally print Longest String and its length from Arrays
FindLongestStringInAnArraysUsingCollectionSortingByLength.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package in.bench.resources.longest.string; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class FindLongestStringInAnArraysUsingCollectionSortingByLength { public static void main(String[] args) { // local variables String longestStr = null ; // 1. names with different length String[] names = new String[] { "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" }; // 1.1 print to console= System.out.println( "Original String[] Arrays :- \n" + Arrays.toString(names)); // 2. convert to List List<String> sortedNames = Arrays.asList(names); // 2.1 sort List according to String length Collections.sort(sortedNames, new Comparator<String>() { @Override public int compare(String str1, String str2) { return str1.length() - str2.length(); } }); // 2.2 get Longest String after above sorting longestStr = sortedNames.get(sortedNames.size() - 1 ); System.out.println( "\nLongest String is = " + longestStr); // 2.3 find length of Longest name System.out.println( "\nLength of Longest String is = " + longestStr.length()); } } |
Output:
1 2 3 4 5 6 | Original String[] Arrays :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Longest String is = Einstein Length of Longest String is = 8 |
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 !!