CollectionUtil { public static <T extends Object & Comparable<? super T>> Pair<T, T> minAndMax(Collection<? extends T> coll) { Iterator<? extends T> i = coll.iterator(); T minCandidate = i.next(); T maxCandidate = minCandidate; while (i.hasNext()) { T next = i.next(); if (next.compareTo(minCandidate) < 0) { minCandidate = next; } else if (next.compareTo(maxCandidate) > 0) { maxCandidate = next; } } return Pair.of(minCandidate, maxCandidate); }  static boolean isEmpty(Collection<?> collection); static boolean isNotEmpty(Collection<?> collection); static T getFirst(Collection<T> collection); static T getLast(Collection<T> collection); static boolean elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2); static T min(Collection<? extends T> coll); static T min(Collection<? extends T> coll, Comparator<? super T> comp); static T max(Collection<? extends T> coll); static T max(Collection<? extends T> coll, Comparator<? super T> comp); static Pair<T, T> minAndMax(Collection<? extends T> coll); static Pair<T, T> minAndMax(Collection<? extends T> coll, Comparator<? super T> comp); static List<T> topN(Iterable<T> coll, int n); static List<T> topN(Iterable<T> coll, int n, Comparator<? super T> comp); static List<T> bottomN(Iterable<T> coll, int n); static List<T> bottomN(Iterable<T> coll, int n, Comparator<? super T> comp);  }
@Test public void minAndMax() { List<Integer> list = ListUtil.newArrayList(4, 1, 9, 100, 20, 101, 40); assertThat(CollectionUtil.min(list)).isEqualTo(1); assertThat(CollectionUtil.min(list, Ordering.natural())).isEqualTo(1); assertThat(CollectionUtil.max(list)).isEqualTo(101); assertThat(CollectionUtil.max(list, Ordering.natural())).isEqualTo(101); assertThat(CollectionUtil.minAndMax(list).getLeft()).isEqualTo(1); assertThat(CollectionUtil.minAndMax(list).getRight()).isEqualTo(101); assertThat(CollectionUtil.minAndMax(list, Ordering.natural()).getLeft()).isEqualTo(1); assertThat(CollectionUtil.minAndMax(list, Ordering.natural()).getRight()).isEqualTo(101); }