30

I need to find the smaller of two Comparable values:

Comparable<C> a = ...;
Comparable<C> b = ...;
Comparable<C> min = a.compareTo(b) <= 0 ? a : b;

This is similar to Math.min(a, b), but for Comparable.

I know that the ternary operator is already quite short, but I can't inline the expressions for a and b and I think that min(a, b) and max(a, b) is easier to understand.

I know that there are several functions that operate on Stream and Collection values, like:

Stream.of(a, b).min(Comparator.naturalOrder())

This would help to inline the expressions, but I still find it difficult to read and a bit too much overhead for such a small task.

For the moment I'm using my own utility function, but I'm interested to know if there's an existing function for this purpose. How can one find the minimum of two Comparable values in a readable and library-independent manner without too much performance overhead?

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • 2
    [`ObjectUtils`](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ObjectUtils.html) has this. – Max Vollmer Jul 10 '18 at 13:07
  • Guava has one, but recommends another (more verbose) alternative for Java 8: https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Ordering.html#min-E-E- – JB Nizet Jul 10 '18 at 13:08
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. – GhostCat Jul 10 '18 at 13:13
  • I changed the question from "Is there" to "How to", which will lead to the same answers but hopefully prevent such flags... – Tobias Liefke Jul 10 '18 at 13:43

5 Answers5

28
  1. From java.util.Collections: Collections.max() and Collections.min()

    Comparable<C> a = ...;
    Comparable<C> b = ...;
    Comparable<C> min = Collections.min(Arrays.asList(a,b));
    

  1. From org.apache.commons.lang3.ObjectUtils : ObjectUtils.max() and ObjectUtils.min()

    Comparable<C> a = ...;
    Comparable<C> b = ...;
    Comparable<C> min = ObjectUtils.min(a, b);
    

Apache Commons has less overhead and is able to handle null values, but it is a third party library.

Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
azro
  • 53,056
  • 7
  • 34
  • 70
8

I have created my own helper class, which extends Comparable by min, max, isLessThan, isLessOrEqualTo, isGreaterThan and isGreaterOrEqualTo:

public interface Ordered<T> extends Comparable<T> {

  static <T extends Comparable<? super T>> T min(T a, T b) {
    return a.compareTo(b) <= 0 ? a : b;
  }

  static <T extends Comparable<? super T>> T max(T a, T b) {
    return a.compareTo(b) >= 0 ? a : b;
  }

  default boolean isLessThan(T other) {
    return compareTo(other) < 0;
  }

  default boolean isLessOrEqualTo(T other) {
    return compareTo(other) <= 0;
  }

  default boolean isGreaterThan(T other) {
    return compareTo(other) > 0;
  }

  default boolean isGreaterOrEqualTo(T other) {
    return compareTo(other) >= 0;
  }

}

The min and max methods I use for any Comparable:

String first = "a";
String second = "b";
System.out.println(Ordered.min(first, second)); // Prints "a"

For my own implementations of Comparable I extend Ordered and use that one for readable comparisons. Very helpful for enums:

public enum Board implements Ordered<Board> {
  NONE,
  BREAKFAST,
  HALF_BOARD,
  FULL_BOARD,
  ALL_INCLUSIVE
}

Usage:

Board requestedBoard = ...;
Board availableBoard = ...;
if (requestedBoard.isLessOrEqualTo(availableBoard)) {
  ...
}
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
6

3rd party solutions

Collections has max(collection) and min(collection) methods, which kind of do what you want.

Bringing whole new library just to inline one simple op might be an overkill, unless you have Apache Commons or Guava in the mix.

Hand crafted

public <T extends Comparable<T>> T max(T a, T b) { 
    return a.compareTo(b) >= 0 ? a : b; 
}

public <T extends Comparable<T>> T min(T a, T b) { 
    return a.compareTo(b) < 0 ? a : b; 
}
M. Justin
  • 14,487
  • 7
  • 91
  • 130
diginoise
  • 7,352
  • 2
  • 31
  • 39
5

The Google Guava library has the Comparators.min and Comparators.max methods as of version 30.0:

Comparable<C> min = Comparators.min(a, b);
M. Justin
  • 14,487
  • 7
  • 91
  • 130
1

Using Stream

Comparable<C> min = Stream.of(a, b).min(Comparable::compareTo).get();

Note that you will usually not call get() and assign, but use ifPresent(min -> ...) instead. But in this case you can trust the value to be present since the stream is not empty.

herman
  • 11,740
  • 5
  • 47
  • 58