This is the complete solution to find the nth Largest number in a Generic Array or an ArrayList. The values available in an Array or ArrayList are either integers, or Strings or Doubles . This solution is designed to to solve the problem using an interface, super class and two derived classes implementing the interface.
package pack1;
import java.util.ArrayList;
public interface NthLargestIntf {
/* Define the Abstract method 'nthLargest' with Generic data Array */
public <T> T nthLargest(T[] tmpArr, int pos);
/* Define the Abstract method 'nthLargest' with Generic ArrayList as input */
public <T> T nthLargest(ArrayList<T> aL, int pos);
}
package pack1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
import java.util.stream.Collectors;
class NthLargestInputs {
/* Declare the logger object for the class to display the log messages */
static Logger logger = Logger.getLogger(NthLargestInputs .class.getName());
}
class NthLargest_Using_Arrays_Generics extends NthLargestInputs implements NthLargestIntf {
/* Define the function with parameters as Generic Array, and position */
@Override
public <T> T nthLargest(T[] tmpArr, int pos) {
T resultVal = null;
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "nthLargest";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
Arrays.sort(tmpArr);
/* check for runtime exception */
try {
resultVal = tmpArr[tmpArr.length - pos];
System.out.println("The " + pos + "th largest entity in Array is=" + resultVal);
} catch (ArrayIndexOutOfBoundsException e) {
logger.warning("The Array index " + pos + " is out of bound " + e.getMessage());
throw e;
}
return resultVal;
}
@Override
public <T> T nthLargest(ArrayList<T> aL, int pos) {
return null;
}
}
class NthLargest_Using_ArrayList_Generic_Streams extends NthLargestInputs implements NthLargestIntf {
/* Define the function with parameters as Generic ArrayList, and position */
@Override
public <T> T nthLargest(ArrayList<T> aL, int pos) {
T resultVal = null;
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "nthLargest";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* check for runtime exception */
try {
resultVal = aL.stream().sorted().collect(Collectors.toList()).get(aL.size() - pos);
System.out.println("The " + pos + "th Largest value in given stream= " + resultVal);
} catch (ArrayIndexOutOfBoundsException e) {
logger.warning("The Array index " + pos + " is out of bound " + e.getMessage());
throw e;
}
return resultVal;
}
@Override
public <T> T nthLargest(T[] tmpArr, int pos) {
return null;
}
}
public class NthLargestJ8 {
public static void main(String[] args) {
/* Create a class object of NthLargest_Using_Arrays_Generics */
NthLargest_Using_Arrays_Generics NthLAUG = new NthLargest_Using_Arrays_Generics();
/* Pass the integer array as parameter to NthLargest_Using_Arrays_Generics, along with position */
Integer[] intArray = { 34, 21, 45, 75, 62, 98, 13, 49 };
NthLAUG.nthLargest(intArray, 1);
/* Pass the double array as parameter to NthLargest_Using_Arrays_Generics, along with position */
Double[] doubleArray = { 34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0 };
NthLAUG.nthLargest(doubleArray, 2);
/* Pass the string array as parameter to NthLargest_Using_Arrays_Generics, along with position */
String[] stringArray = { "MIKE", "RUSS", "SANJAY", "VALLI", "JOHN", "RAMAA", "DAVE", "NAGENDRA" };
NthLAUG.nthLargest(stringArray, 3);
/* Create a class object of NthLargest_Using_ArrayList_Generic_Streams */
NthLargest_Using_ArrayList_Generic_Streams NthLALUGS = new NthLargest_Using_ArrayList_Generic_Streams();
/* Pass the integer arraylist as parameter to NthLargest_Using_ArrayList_Generic_Streams, along with position */
ArrayList<Integer> inSt = new ArrayList<>(Arrays.asList(34, 21, 45, 75, 62, 98, 13, 49));
NthLALUGS.nthLargest(inSt, 4);
/* Pass the String arraylist as parameter to NthLargest_Using_ArrayList_Generic_Streams, along with position */
ArrayList<String> StringSt = new ArrayList<>(
Arrays.asList("MIKE", "RUSS", "SANJAY", "VALLI", "JOHN", "RAMAA", "DAVE", "NAGENDRA"));
NthLALUGS.nthLargest(StringSt, 5);
/* Pass the Double arraylist as parameter to NthLargest_Using_ArrayList_Generic_Streams, along with position */
ArrayList<Double> DoubleSt = new ArrayList<>(Arrays.asList(34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0));
NthLALUGS.nthLargest(DoubleSt, 6);
}
}
OUTPUT:
Jan 15, 2023 6:36:57 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8'
The 1th largest entity in Array is=98
Jan 15, 2023 6:36:57 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8'
The 2th largest entity in Array is=75.0
The 3th largest entity in Array is=RUSS
Jan 15, 2023 6:36:57 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8'
Jan 15, 2023 6:36:57 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8'
The 4th Largest value in given stream= 49
Jan 15, 2023 6:36:57 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8'
The 5th Largest value in given stream= NAGENDRA
Jan 15, 2023 6:36:57 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8'
The 6th Largest value in given stream= 34.0
The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/01/15/junit5-test-nth-largest-number-function/

Leave a comment