EC by Example: Min and Max

Learn how to find the minimum and maximum values in a collection using Eclipse Collections.

Of Max and Min

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

If you have a collection that contains values, you can simply use and with no parameters.

@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

Alternatively, there are forms of and that take a as a parameter.

@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

There are methods named and which take a as a parameter. The is turned into a for you.

@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

There are methods on that are prefixed with summarize that return an appropriate instance of . The three types (, and ) added in Java 8 have the methods and , which allow you to calculate the and in a single iteration. The methods take a primitive as a parameter. There are four methods (with a suffix of , , and ).

@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

Any primitive collection can give you the and values. The methods and will throw an if the collection is empty. You can specify a default value to protect against the case of a collection being empty using and .

@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

If you have a primitive , you can call directly on it. The method will return either an , , or instance. For , , and iterables, this will result in a widening of the type. As you can see below, for a returns an instance.

@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

The and methods will throw an if the collection is empty. You can protect against this by using the methods named and . There are also optional forms for and named and .

@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. / — find the minimum or maximum value in a collection of values.
  2. / with — find the minimum or maximum value in a collection using the specified .
  3. / — find the minimum or maximum value in a collection using the specified .
  4. — returns an by applying the specified to each element of the collection. There are also forms of with a suffix of , or .
  5. / ()
  6. / ()
  7. / with — find the minimum or maximum value in a collection using the specified returning an value.
  8. — returns an appropriate instance for the primitive type.
  9. . — creates a wrapping the specified .

Check out this presentation to learn more about the origins, design and evolution of the Eclipse Collections API.

Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

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

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