Skip to content
Advertisement

How to search in a List of Java object

I have a List of object and the list is very big. The object is

JavaScript

Now I have to search for a specific value of an object in the list. Say if value3=='three' I have to return those objects (My search is not always based on value3)

The list is

JavaScript

What is the efficient way of doing it?

Thanks.

Advertisement

Answer

You can give a try to Apache Commons Collections.

There is a class CollectionUtils that allows you to select or filter items by custom Predicate.

Your code would be like this:

JavaScript

Update:

In java8, using Lambdas and StreamAPI this should be:

JavaScript

much nicer!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement