Generic Method to find the median of 3 values

痴心易碎 提交于 2019-12-08 17:05:27

问题


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:

  1. I noticed this doesn't work if I tried to declare 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.
  2. Why am I getting a warning? What is wrong/missing?

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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/29938390/generic-method-to-find-the-median-of-3-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!