EC by Example: Min and Max

Of Max and Min

How do you find the min and max of a collection?

@Test
public void theMinAndTheMax()
{
MutableList<String> alphabet =
Lists.mutable.with("a", "b", "c", "d", "e");
String max = alphabet.max();
String min = alphabet.min();
Assert.assertEquals("e", max);
Assert.assertEquals("a", min);
}

Using Comparators

@Test
public void theComparators()
{
MutableList<String> alphabet =
Lists.mutable.with("a", "b", "c", "d", "e");
String max = alphabet.max(Comparators.reverseNaturalOrder());
String min = alphabet.min(Comparators.reverseNaturalOrder());
Assert.assertEquals("a", max);
Assert.assertEquals("e", min);
}

Using minBy and maxBy

@Test
public void byTheBy()
{
MutableList<String> strings =
Lists.mutable.with("1", "22", "333", "4444");
String max = strings.maxBy(String::length);
String min = strings.minBy(String::length);
Assert.assertEquals("4444", max);
Assert.assertEquals("1", min);
}

Getting min and max using SummaryStatistics

@Test
public void theStatistics()
{
MutableList<Integer> alphabet =
Lists.mutable.with(2, -3, 4, -5);
IntSummaryStatistics stats =
alphabet.summarizeInt(i -> i * i);
Assert.assertEquals(4, stats.getMin());
Assert.assertEquals(25, stats.getMax());
}

Primitive min and max

@Test
public void primitiveMaxAndMin()
{
String string =
("The quick brown fox " +
"jumps over the lazy dog.").toLowerCase();
CharList chars =
Strings.asChars(string).select(Character::isLetter);
Assert.assertEquals('a', chars.min());
Assert.assertEquals('z', chars.max());
CharList emptyChars = Strings.asChars(""); Assert.assertEquals('a', emptyChars.minIfEmpty('a'));
Assert.assertEquals('z', emptyChars.maxIfEmpty('z'));
}

Primitive summaryStatistics

@Test
public void primitiveSummaryStatistics()
{
String string =
("The quick brown fox " +
"jumps over the lazy dog.").toLowerCase();
CharList chars =
Strings.asChars(string).select(Character::isLetter);
IntSummaryStatistics stats = chars.summaryStatistics(); Assert.assertEquals('a', stats.getMin());
Assert.assertEquals('z', stats.getMax());
}

Optional min and max

@Test
public void theOptionals()
{
MutableList<String> alphabet = Lists.mutable.empty();
Optional<String> max =
alphabet.maxOptional(Comparators.naturalOrder());
Optional<String> min =
alphabet.minOptional(Comparators.naturalOrder());
Assert.assertFalse(max.isPresent());
Assert.assertFalse(min.isPresent());
}

APIs covered in the examples

  1. min / max — find the minimum or maximum value in a collection of Comparable values.
  2. min / max with Comparator — find the minimum or maximum value in a collection using the specified Comparator.
  3. minBy / maxBy — find the minimum or maximum value in a collection using the specified Function.
  4. summarizeInt — returns an IntSummaryStatistics by applying the specified IntFunction to each element of the collection. There are also forms of summarize with a suffix of Float, Long or Double.
  5. min / max (CharList)
  6. minIfEmpty / maxIfEmpty (CharList)
  7. minOptional / maxOptional with Comparator — find the minimum or maximum value in a collection using the specified Comparator returning an Optional value.
  8. summaryStatistics — returns an appropriate SummaryStatistics instance for the primitive Iterable type.
  9. Strings.asChars — creates a CharAdapter wrapping the specified String.

--

--

--

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

MyPetPro Platform Structure

How to add Reviews Snippet using Google Tag Manager

How to Refactor a Codebase?

Flutter! You definitely should give it a try.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Donald Raab

Donald Raab

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

More from Medium

nodef found org.apache.kafka.common.serialization.stringserializer

[Solved] jvm 1 | Unrecognized option: — add-exports=java.base/jdk.internal.ref=ALL-UNNAMED

import Json dependency in JAVA

Pitfall When Using Java Okta SDK JWT Verifier