So here you are.

You've got two List objects in your Java application and you'd like to determine if any object from one List is also in the other List.

Well I've been there. And I'll show you how I handled it.

But first, you need to make sure you can perform an equality check.


Before you can determine if one object from a List is in another List, you need to make sure that you can compare if two objects are equal to one another.

Surprisingly, you can determine equality with a method called equals(). It's part of every object in Java applications.

If you're working with your own custom objects (in other words, you're not using Strings, Integers, or other Java objects), then you'll need to override that equals() method to determine equality.

Don't know how to do that? Just head over to my guide on overriding equals() and you'll learn all about it.

Then, come back here.

Some Sample Code

Let's say you're running a website powered by Spring Boot. Like this one.

Now let's further say you've got a feature that lets visitors enter a bunch of tags and you show them all the blog posts that match any one of those tags.

So your application will capture the user-entered tags in a List of Strings. And the tags for each blog post are also stored as a List of Strings.

Clearly, what needs to happen here is you need to find all blog posts with any tags that match the tag in the user-defined List.

Now let's mock that whole scenario up with some code.

List<String> userDefinedTags = List.of("spring", "angular", "java");
List<String> blogPostTags = List.of("angular", "directive", "component");

Two List objects.

The first object, userDefinedTags, includes a list of tags the user is searching for.

The second object, blogPostTags, includes a list of tags for a hypothetical blog post.

Clearly, the two lists have one element in common: "angular."

Now let's write some code that will make that determination.

Making That Determination

The method you're looking for is called disjoint(). It's part of the Collections class.

The disjoint() method takes two parameters: both of them Collection types.

(Recall from Java 101 that List extends Collection so you're good to go here.)

The disjoint() method returns a boolean. Specifically, it will return true if none of the elements in one Collection exist in the other Collection.

So here's the code to determine if any element in the first list is in the second list:

List<String> userDefinedTags = List.of("spring", "angular", "java");
List<String> blogPostTags = List.of("angular", "directive", "component");

boolean match = !Collections.disjoint(userDefinedTags, blogPostTags);

Pay very careful attention to that exclamation point in front of Collections.disjoint().

Why is that there? Because disjoint() returns true if no elements match.

But you want to know if any elements match. That's what brought you here from your favorite search engine.

So you need to "not" the boolean. You do that with an exclamation point (Java 101 again).

If you run that code above, you'll see that it spits out the word "true" in a nice shade of red on the console.

And it should do that because there's a match from one list to the other: "angular".

Wrapping It Up

Over to you.

Mess around with that code above. Try two lists with multiple matching elements. Or no matching elements.

Then, update the code to suit your own reqs.

Have fun!

Photo by Sarah Chai from Pexels