In this article, we will discuss how to find third longest String in an Arrays and List
1. Finding Third Longest String in an Arrays:
We will follow below 2 approaches to get 3rd Longest String in an Arrays
- Using SortedSet and TreeSet
- Using Arrays sorting
1.1 Using SortedSet & TreeSet approach
- Create TreeSet object with SortedSet reference and pass Comparator as constructor-argument for String length comparison
- Now, elements inside TreeSet object will be sorted according to String length and last element will be the longest element
- Using last() method of SortedSet reference, we can get last element and that will be the longest element
- Remove last 2 elements using remove() method of SortedSet reference passing sortedSet.last() method as argument
- Now after removing last 2 elements, current last element in the TreeSet object will be the 3rd longest String
- Finally print 3rd longest String to the console
FindThirdLongestStringInAnArraysUsingTreeSet.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
public class FindThirdLongestStringInAnArraysUsingTreeSet {
public static void main(String[] args) {
// 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. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 create an Object of TreeSet which stores elements
SortedSet<String> sortedSet = new TreeSet<String>(new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str1.length() - str2.length();// acc. to String length in ASC order
}
});
// 2.2 convert Arrays to List and addAll to above Set
sortedSet.addAll(Arrays.asList(names));
// 2.3 remove last 2 element
sortedSet.remove(sortedSet.last()); // longest
sortedSet.remove(sortedSet.last()); // second longest
// 2.4 now, this will be Third longest String
String thirdLongestString = sortedSet.last();
// 2.5 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.6 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.7 print to console
System.out.println("\nThird longest String in an Arrays is - "
+ thirdLongestString);
// 2.8 print execution time in nanoseconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String[] Arrays :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in an Arrays is - Spider
Execution time - 999800 ns
1.2 Using Arrays sorting approach
- First step is to sort Arrays of String using Arrays.sort() method in ascending-order
- After sorting, get 3rd longest String by passing index of third-last element to String[] Arrays
FindThirdLongestStringInAnArraysUsingSortMethod.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
public class FindThirdLongestStringInAnArraysUsingSortMethod {
public static void main(String[] args) {
// 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. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 sort Arrays element in ascending order of String length
Arrays.sort(
names, // original Arrays
new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str1.length() - str2.length();// acc. to String length in ASC order
}
});
// 2.2 3rd last element will be third longest String in an Arrays
String thirdShortestString = names[names.length - 3];
// 2.3 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.4 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.5 print to console
System.out.println("\nThird longest String in an Arrays is - "
+ thirdShortestString);
// 2.6 print execution time in nanoseconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String[] Arrays :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in an Arrays is - Spider
Execution time - 1000600 ns
2. Finding Third Longest String in List or ArrayList:
- First step is to sort List of String using Collections.sort() method in ascending-order
- After sorting, get 3rd longest String by passing index of third-last element to String List
FindThirdLongestStringInListUsingCollectionsSortMethod.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class FindThirdLongestStringInListUsingCollectionsSortMethod {
public static void main(String[] args) {
// 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. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 sort List element in ascending order
Collections.sort(
names, // original List
new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str1.length() - str2.length();// acc. to String length in ASC order
}
});
// 2.2 3rd last element will be third largest String in an Arrays
String thirdShortestString = names.get(names.size() - 3);
// 2.4 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.5 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.6 print to console
System.out.println("\nThird longest String in List is - "
+ thirdShortestString);
// 2.7 print execution time in nanoseconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String List :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in List is - Spider
Execution time - 927300 ns
3. Points to remember w.r.t execution time:
- Execution time differs in different platforms
- With small set of numbers, we may not find large difference in execution time
- But with large set of numbers, difference will be significant to consider
Related Articles:
- Java 8 – Find Largest number in an Arrays or List or Stream ?
- Java 8 – Find Smallest number in an Arrays or List or Stream ?
- Java 8 – Find 2nd Largest number in an Arrays or List or Stream ?
- Java 8 – Find 2nd Smallest number in an Arrays or List or Stream ?
- Java 8 – Find sum of Largest 2 numbers in an Arrays or List or Stream ?
- Java 8 – Find sum of Smallest 2 numbers in an Arrays or List or Stream ?
- Java 8 – Find 1st and Last elements in an Arrays ?
- Java 8 – Find 1st and Last elements in a List or ArrayList ?
- Java 8 – Find 1st and Last elements in a Set or HashSet ?
- Java 8 – Find 1st and Last entries in a Map or HashMap ?
- Java 8 – Find sum and average of a List or ArrayList ?
- Java 8 – How to calculate sum and average of an Arrays ?
Happy Coding !!
Happy Learning !!