I needed a method to get the median of 3 values, I thought it a good opportunity to write a generic method since I don't really have that practiced. I wrote this and it seems pretty straight-forward, though I get a warning, but it seems to work fine, according to my tests.
I'm aware I could use an inherently sorted set, or Collections.sort()
, but this approach is for the sake of understanding.
I want to pinpoint a few things:
medianHelper
with Arrays.asList(a, b, c)
why is this? Trying to search this gives me unrelated results and it's otherwise elusive since I'm not sure what is happening. I get an UnsupportedOperationException
, but this is not present the way I have it below.The method follows:
private static <T extends Comparable> T median(T a, T b, T c) {
List<T> medianHelper = new ArrayList<>();
T max;
T min;
medianHelper.add(a);
medianHelper.add(b);
medianHelper.add(c);
if (a.compareTo(b) >= 0) {
max = a;
min = b;
} else {
max = b;
min = a;
}
if (max.compareTo(c) == -1) {
max = c;
}
if (min.compareTo(c) >= 0) {
min = c;
}
medianHelper.remove(max);
medianHelper.remove(min);
return medianHelper.get(0);
}
You haven't correctly introduced the type-parameter T
, as Comparable
is generic, too.
It should rather be:
private static <T extends Comparable<? super T>> T median(T a, T b, T c)
Furthermore, you can just sort the medianHelper
list, since its elements will be Comparable
. So your method can be significantly shortened to:
private static <T extends Comparable<? super T>> T median(T a, T b, T c) {
List<T> medianHelper = Arrays.asList(a, b, c);
Collections.sort(medianHelper);
return medianHelper.get(1);
}
Note that Arrays.asList()
returns an unmodifiable list, which means you're not allowed to add/remove elements after it's created. If you wish to do the comparisons yourself, you can use new ArrayList<>
instead of Arrays.asList()
and then manually add the elements to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With