Most efficient way to see if an ArrayList contains an object in Java

后端 未结 12 1143
孤城傲影
孤城傲影 2020-11-30 18:25

I have an ArrayList of objects in Java. The objects have four fields, two of which I\'d use to consider the object equal to another. I\'m looking for the most efficient wa

12条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 19:15

    If you are a user of my ForEach DSL, it can be done with a Detect query.

    Foo foo = ...
    Detect query = Detect.from(list);
    for (Detect each: query) 
        each.yield = each.element.a == foo.a && each.element.b == foo.b;
    return query.result();
    

提交回复
热议问题