Search code examples
javagenericscomparableinfinity

Java Generics and Infinity (Comparable)


With the type Integer you can do this:

int lowest = Integer.MIN_VALUE;

What can I do if I use generics?

K lowest = <...>;

I need this in order to implement something similar to a PriorityQueue. I have access to a node I want to remove from the queue, but it is not the min.

1. I need to make it the min by decreasing the key of that node,
2. And then remove the min.

I am stuck on the first step. The only thing I can do is set the key of the node to the current min. Not sure it is enough.


Solution

  • This doesn't make any sense...

    Given that you don't know what K is at that point, (i.e. You're implementing it generically... duh!) you can't specify a min/max bound for it.

    in a case where K could be a int, long, string OR object, you couldn't sensibly guess to use

    Integer.MIN_VALUE, "" OR NULL.

    I guess what you're looking for is a K.MIN_VALUE_OF_EVENTUAL_TYPE but that doesn't exist.