0

How can I use contains method to search by id only, I want to check if there is id equal 4 or not without using loop, How?

Test Class

public class Test {

    private int id;
    private String name;
    private int age;

    public Test(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

}

Set Data

List <Test> list = new ArrayList <> ();
list.add(new Test(1, "Name 1", 25));
list.add(new Test(2, "Name 2", 37));
list.add(new Test(3, "Name 3", 63));
list.add(new Test(4, "Name 4", 19));
list.add(new Test(5, "Name 5", 56));
2
  • You can store your classes in a map, where id is the key. But for a list you cannot avoid iterating over it. Commented Sep 14, 2021 at 6:00
  • Can you try using JSON without creating POJO class. Commented Sep 14, 2021 at 6:04

1 Answer 1

6

If you check the contains method from java library. It internally calls indexOf method (which internally calls indexOfRange method), it return the index by making the equals method call on object.

int indexOfRange(Object o, int start, int end) {
        Object[] es = elementData;
        if (o == null) {
            for (int i = start; i < end; i++) {
                if (es[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = start; i < end; i++) {
                if (o.equals(es[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

In your code equals will be called on Test class object.

As the solution either override the equals method to return true, if id matched. But I would say that won't be a good solution because equals method should follow some contracts.

So alternatively correct should be use of Streams API here (although Stream also will use iteration internally).

List <Test> list = new ArrayList <> ();
list.add(new Test(1, "Name 1", 25));
list.add(new Test(2, "Name 2", 37));
list.add(new Test(3, "Name 3", 63));
list.add(new Test(4, "Name 4", 19));
list.add(new Test(5, "Name 5", 56));
        
boolean isMatch = arr.stream().anyMatch(i-> i.id == toMatch);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, overriding equals sounds more intuitive (but as Gaurav says, it should follow some contract and logic) as that would mean there is a basis for comparison between any two objects. Also keep in mind that it is a best practice approach that if you override equals then it makes sense to override hashCode as well.
.stream added in API 24 in android and minSdkVersion in my android app is API 23, So there is no another way?
you can write a user defined function and can return . Using normal for loop / itetor. But you need to iterate the entire list until match is found. Ref :baeldung.com/find-list-element-java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.