@Test public void testMaxValues() { Comparator<String> longest = Comparator.comparing(String::length); List<String> strings = ImmutableList.of("a", "abc", "ab", "efg", ""); assertThat(maxValues(strings, longest), contains("abc", "efg")); }
public static <R> Collection<R> maxValues(Collection<R> values, Comparator<R> comparator) { List<R> maxValues = new ArrayList<>(); for (R value : values) { if (maxValues.isEmpty()) { maxValues.add(value); continue; } int cmp = comparator.compare(value, maxValues.get(0)); if (cmp == 0) { maxValues.add(value); } else if (cmp > 0) { maxValues.clear(); maxValues.add(value); } } return maxValues; }
CollectionUtil { public static <R> Collection<R> maxValues(Collection<R> values, Comparator<R> comparator) { List<R> maxValues = new ArrayList<>(); for (R value : values) { if (maxValues.isEmpty()) { maxValues.add(value); continue; } int cmp = comparator.compare(value, maxValues.get(0)); if (cmp == 0) { maxValues.add(value); } else if (cmp > 0) { maxValues.clear(); maxValues.add(value); } } return maxValues; } }
CollectionUtil { public static <R> Collection<R> maxValues(Collection<R> values, Comparator<R> comparator) { List<R> maxValues = new ArrayList<>(); for (R value : values) { if (maxValues.isEmpty()) { maxValues.add(value); continue; } int cmp = comparator.compare(value, maxValues.get(0)); if (cmp == 0) { maxValues.add(value); } else if (cmp > 0) { maxValues.clear(); maxValues.add(value); } } return maxValues; } private  CollectionUtil(); }
CollectionUtil { public static <R> Collection<R> maxValues(Collection<R> values, Comparator<R> comparator) { List<R> maxValues = new ArrayList<>(); for (R value : values) { if (maxValues.isEmpty()) { maxValues.add(value); continue; } int cmp = comparator.compare(value, maxValues.get(0)); if (cmp == 0) { maxValues.add(value); } else if (cmp > 0) { maxValues.clear(); maxValues.add(value); } } return maxValues; } private  CollectionUtil(); static Map<K, V> toImmutableMap(Map<K, V> map); static Map<K2, V2> toImmutableMap(
      Map<K1, V1> map,
      Function<Entry<K1, V1>, K2> keyFunction,
      Function<Entry<K1, V1>, V2> valueFunction); static Map<K2, V> toMap(
      Set<K1> set, Function<K1, K2> keyFunction, Function<K1, V> valueFunction); static Map<K, V> toImmutableMap(
      Collection<E> set, Function<E, K> keyFunction, Function<E, V> valueFunction); static SortedMap<K2, V2> toImmutableSortedMap(
          Map<K1, V1> map,
          Function<Entry<K1, V1>, K2> keyFunction,
          Function<Entry<K1, V1>, V2> valueFunction); static Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
          Function<? super T, ? extends K> keyFunction,
          Function<? super T, ? extends V> valueFunction); static NavigableMap<K, V> toImmutableSortedMap(
      Collection<E> set, Function<E, K> keyFunction, Function<E, V> valueFunction); static Collector<T, ?, Integer> toOrderedHashCode(); static Collector<T, ?, Integer> toUnorderedHashCode(); static Collection<R> maxValues(Collection<R> values, Comparator<R> comparator); }
CollectionUtil { public static <R> Collection<R> maxValues(Collection<R> values, Comparator<R> comparator) { List<R> maxValues = new ArrayList<>(); for (R value : values) { if (maxValues.isEmpty()) { maxValues.add(value); continue; } int cmp = comparator.compare(value, maxValues.get(0)); if (cmp == 0) { maxValues.add(value); } else if (cmp > 0) { maxValues.clear(); maxValues.add(value); } } return maxValues; } private  CollectionUtil(); static Map<K, V> toImmutableMap(Map<K, V> map); static Map<K2, V2> toImmutableMap(
      Map<K1, V1> map,
      Function<Entry<K1, V1>, K2> keyFunction,
      Function<Entry<K1, V1>, V2> valueFunction); static Map<K2, V> toMap(
      Set<K1> set, Function<K1, K2> keyFunction, Function<K1, V> valueFunction); static Map<K, V> toImmutableMap(
      Collection<E> set, Function<E, K> keyFunction, Function<E, V> valueFunction); static SortedMap<K2, V2> toImmutableSortedMap(
          Map<K1, V1> map,
          Function<Entry<K1, V1>, K2> keyFunction,
          Function<Entry<K1, V1>, V2> valueFunction); static Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
          Function<? super T, ? extends K> keyFunction,
          Function<? super T, ? extends V> valueFunction); static NavigableMap<K, V> toImmutableSortedMap(
      Collection<E> set, Function<E, K> keyFunction, Function<E, V> valueFunction); static Collector<T, ?, Integer> toOrderedHashCode(); static Collector<T, ?, Integer> toUnorderedHashCode(); static Collection<R> maxValues(Collection<R> values, Comparator<R> comparator);  }