Sitemap

Peekaboo with Collections in Java

2 min readJul 4, 2025

--

Checking for containment in direct and indirect ways.

Photo by Super Tseng on Unsplash

Asking a collection in Java if it contains a specific thing is kind of like playing peekaboo. I was experimenting with different ways of seeing if a Collection in Eclipse Collections contains a specific element. I used a combination of contains, containsBy, anySatisfy, Stream.anyMatch. I look for a specific String in uppercase and lowercase, which is stored in lowercase in two different kinds of Bag. One Bag just uses hashCode and equals for identifying via contains. The other Bag uses a HashingStrategy.

@Test
public void testingIfAnElementIsContained()
{
MutableBag<String> bag =
Bags.mutable.with("ant", "bat", "cat");

// using contains on a Bag is o(1) but requires an exact match
assertFalse(bag.contains("CAT"));
assertTrue(bag.contains("cat"));
assertFalse(bag.contains("dog"));

// using containsBy is o(n) and can be lenient on the item contained
assertTrue(bag.containsBy(String::toUpperCase, "CAT"));
assertFalse(bag.containsBy(String::toUpperCase, "cat"));
assertFalse(bag.containsBy(String::toUpperCase, "DOG"));

// using anySatisfy with equalsIgnoreCase is o(n) and is lenient
assertTrue(bag.anySatisfy("CAT"::equalsIgnoreCase));
assertTrue(bag.anySatisfy("cat"::equalsIgnoreCase));
assertFalse(bag.anySatisfy("DOG"::equalsIgnoreCase));

// using anyMatch with equalsIgnoreCase is o(n) and is lenient
assertTrue(bag.stream()
.anyMatch("CAT"::equalsIgnoreCase));
assertTrue(bag.stream()
.anyMatch("cat"::equalsIgnoreCase));
assertFalse(bag.stream()
.anyMatch("DOG"::equalsIgnoreCase));

// Using map, anyMatch, equals is o(n) and lenient on the item matched
assertTrue(bag.stream()
.map(String::toUpperCase)
.anyMatch("CAT"::equals));
assertFalse(bag.stream()
.map(String::toUpperCase)
.anyMatch("cat"::equals));
assertFalse(bag.stream()
.map(String::toUpperCase)
.anyMatch("dog"::equals));

MutableBag<String> hsBag =
HashingStrategyBags.mutable.fromFunction(String::toUpperCase);
hsBag.with("ant").with("bat").with("cat");

// using contains on a Bag with HashingStrategy is o(1) and is lenient
assertTrue(hsBag.contains("CAT"));
assertTrue(hsBag.contains("cat"));
assertFalse(hsBag.contains("dog"));
}

The methods contains, containsBy, anySatisfy, anyMatch are all categorized as “Testing” methods in my book Eclipse Collections Categorically, as these methods help you test for the presence or absence of an element in a collection.

I felt like writing a short coding blog, so that’s all! Thank you for reading!

I am the creator of and committer for the Eclipse Collections OSS project, which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions. I am also the author of the book, Eclipse Collections Categorically: Level up your programming game.

--

--

Donald Raab
Donald Raab

Written by Donald Raab

Java Champion. Creator of the Eclipse Collections OSS Java library (https://github.com/eclipse-collections). Inspired by Smalltalk. Opinions are my own.

No responses yet