I have to write a method which selects the maximum value from a list, and it has to be with Generics. Obviously the List can be Number and String as well. (The return value has to be Opt Object. This is the tasks.)
This is what I have so far, but its not working, I would appreciate your advice:
public static <T> Opt<T> max(List<? extends Object> list) {
T max = (T) list;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) > max) {
max = (T) list.get(i);
}
}
return (Opt<T>) max;
}
And this is the main looks like: (From this one I have to make my method work.)
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
Utility.addTo(stringList, "aghi");
Utility.addTo(stringList, "fed");
Utility.addTo(stringList, "ghh");
Utility.addTo(stringList, "abc");
Utility.addTo(stringList, "123");
System.out.println("The maximum value: " + Utility.max(stringList).get());
List<Integer> intList = new ArrayList<>();
Utility.addTo(intList, 123);
Utility.addTo(intList, 456);
Utility.addTo(intList, -199);
Utility.addTo(intList, -90);
Utility.addTo(intList, 0);
Utility.addTo(intList, -10);
Utility.addTo(intList, 200);
System.out.println("The maximum value: " + Utility.max(intList).get());
List<Double> doubleList = new ArrayList<>();
Utility.addTo(doubleList, 123.0);
Utility.addTo(doubleList, 456.001);
Utility.addTo(doubleList, -199.0);
Utility.addTo(doubleList, -90.90);
Utility.addTo(doubleList, 0.0);
Utility.addTo(doubleList, -10.20);
Utility.addTo(doubleList, 200.1);
System.out.println("The maximum value: " + Utility.max(doubleList).get());
}
And the Output shoud be:
The maximum value: ghh
The maximum value: 456
The maximum value: 456.001
Your code is not working (I mean cannot be compiled) because of this line:
In Java, there are no overloaded operators as in C++, so you need to find another way...
By
Opt
you probably meantjava.util.Optional
class and you can use it like this:This is of course not working, it returns last element from list.
When creator of a class expects users might be interested in sorting (comparing), they would implemente
java.lang.Comparable
, this is the case forString
,Long
andDouble
. So instead of T, you can sayT extends Comparable
like this:Look at Comparable#compareTo JavaDoc.
Try to understand what this line means exactly (and why you cannot use it with list of
java.lang.Object
s):and why we do not need it in addTo: