Preposition Preference

Donald Raab
Nov 15, 2017 · 4 min read
Photo taken at Grounds for Sculpture in Hamilton, NJ

Have you ever thought about how much we use prepositions in our Java APIs?

Two prepositions enter. One preposition leaves.

MutableList<String> list = Lists.mutable.of("1", "2", "3");                           vs.MutableList<String> list = Lists.mutable.with("1", "2", "3");
|set|
set := Set with: ‘1’ with: ‘2’ with: ‘3’.
MutableSet<String> set = Sets.mutable.with("1", "2", "3");
MutableSet<String> set = Sets.mutable.of("1", "2", "3");
Set<String> set = 
Sets.mutable.with("1", "2", "3")
.with("4")
.without("2");
Assert.assertEquals(Sets.mutable.with("1", "3", "4"), set);
Set<String> set =
Sets.mutable.with("1", "2", "3")
.withAll(Lists.mutable.with("4"))
.withoutAll(Lists.mutable.with("1", "3"));
Assert.assertEquals(Sets.mutable.with("2", "4"), set);
ImmutableSet<String> set =
Sets.immutable.with("1", "2", "3")
.newWithAll(Lists.mutable.with("4"))
.newWithoutAll(Lists.mutable.with("1", "3"));
Assert.assertEquals(Sets.mutable.with("2", "4"), set);

Attack Of the Clones

// Bag API - occurrencesOf
MutableBag<String> bag = Bags.mutable.with("1", "2", "3");
Assert.assertEquals(1, bag.occurrencesOf("2"));
// List API - indexOf
MutableList<String> list = Lists.mutable.with("1", "2", "3");
Assert.assertEquals(1, list.indexOf("2"));
// RichIterable API - sumOfInt, sumOfLong, sumOfFloat, sumOfDouble
MutableList<String> list = Lists.mutable.with("1", "2", "3");
long sum = list.sumOfInt(Integer::parseInt);
Assert.assertEquals(6L, sum);
// RichIterable API - selectInstancesOf
MutableList<String> list = Lists.mutable.with("1", "2", "3");
MutableList<String> filtered = list.selectInstancesOf(String.class);
Assert.assertEquals(list, filtered);

Revenge of the With

With methods in the RichIterable interface
boolean any =
this.people.anySatisfyWith(Person::hasPet, PetType.CAT);
Assert.assertTrue(any);

boolean all =
this.people.allSatisfyWith(Person::hasPet, PetType.CAT);
Assert.assertFalse(all);

boolean none =
this.people.noneSatisfyWith(Person::hasPet, PetType.CAT);
Assert.assertFalse(none);

Person found =
this.people.detectWith(Person::hasPet, PetType.CAT);
Assert.assertNotNull(found);

int count =
this.people.countWith(Person::hasPet, PetType.CAT);
Assert.assertEquals(2, count);

MutableList<Person> selected =
this.people.selectWith(Person::hasPet, PetType.CAT);
MutableList<Person> rejected =
this.people.rejectWith(Person::hasPet, PetType.CAT);
PartitionMutableList<Person> partition =
this.people.partitionWith(Person::hasPet, PetType.CAT);
Assert.assertEquals(selected, partition.getSelected());
Assert.assertEquals(rejected, partition.getRejected());

Donald Raab

Written by

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade