3

I want to create generic method for getting maximal value from the array. However I got stuck at the problem of conversion between T to type Math.max method uses.

here is my code: First I cannot initialize the maxValue like:

T maxVaue = 0;

secondly I cannot use data[i] in Math.max, in both cases I got error of In convertible cast.

class Matrix<T>{
    public T getMaxValue(T[] data){
        T maxValue;
        for (int i=0; i<data.length; i++){
            maxValue = Math.max(data[i], maxValue);
        }
        return maxValue;
    }
}
pezetem
  • 2,503
  • 2
  • 20
  • 38

3 Answers3

3

Math.max takes only int, float, double and long. So you can not use it with T.

What you can do is write your own max() method using T. For example, your method could take a Comparator object in param. Or your T objects can implement the Comparable interface.

Have a look at this post : How to implement a generic `max(Comparable a, Comparable b)` function in Java?

Community
  • 1
  • 1
Junior Dussouillez
  • 2,327
  • 3
  • 30
  • 39
2

You cannot do that as you are doing it because a T can be anything. You could e.g. limit T to a Number but then you'd have to choose an intermediate primitive type and you will still run into issues returning the value.

However, you could just limit T to Comparable and use that instead, e.g.:

class Matrix<T extends Comparable<T>>{
    public T getMaxValue(T[] data){
        T maxValue = null;
        for (int i=0; i<data.length; i++){
            if (maxValue == null || data[i].compareTo(maxValue) > 0)
                maxValue = data[i];
        }
        return maxValue;
    }
}

All of Java's primitive wrapper types implement Comparable, as well as many other types:

byte by = new Matrix<Byte>().getMaxValue(new Byte[]{1, 2, 3});
char ch = new Matrix<Character>().getMaxValue(new Character[]{'a', 'b', 'c'});
int in = new Matrix<Integer>().getMaxValue(new Integer[]{1, 2, 3});
short sh = new Matrix<Short>().getMaxValue(new Short[]{1, 2, 3});
long lo = new Matrix<Long>().getMaxValue(new Long[]{1L, 2L, 3L});
float fl = new Matrix<Float>().getMaxValue(new Float[]{0.1f, 0.2f, 0.3f});
double db = new Matrix<Double>().getMaxValue(new Double[]{0.1, 0.2, 0.3});
boolean bo = new Matrix<Boolean>().getMaxValue(new Boolean[]{false, true});
String st = new Matrix<String>().getMaxValue(new String[]{"turtles", "are", "weird"});
BigInteger bi = new Matrix<BigInteger>().getMaxValue(new BigInteger[]{...});
Jason C
  • 38,729
  • 14
  • 126
  • 182
2

T can hold only classes like Double, Integer, not primitive types like double, int. Maybe just use built-in Collections.max(Collection<T extends Comparable<T>>) and array of Doubles instead of doubles like in this code example

class Matrix<T extends Comparable<T>> {
    public T getMaxValue(T[] data) {
        return Collections.max(Arrays.asList(data));
    }

    public static void main(String[] args) throws IOException {
        Matrix<Double> m = new Matrix<>();
        System.out.println(m.getMaxValue(new Double[] { 1., 42., 3., 4., 5. }));
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269