How do you check if a list contains an element that matches some predicate?

I have objects containing an ArrayList that has 4 parameters (x, y, iD and myType). I want to check if there are objects in this ArrayList that have specific coordinates, regardless of their iD and myType parameters. I wanted to use Arrays.asList(yourArray).contains(yourValue)

, but this is when the object has only one parameter.

Here's the whole code:

public class MyObject {

  public float x;
  public float y;
  public int iD;
  public String myType;


  public MyObject (float x, float y, int iD, String myType) 
  {
    this.myType = myType;
    this.iD = iD;
    this.x = x;
    this.y = y;
  }
  @Override
    public String toString() {
    return ("[iD="+iD+" x="+x+" y="+y +" type="+myType+"]");
  }
}


ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();

void setup() 
{
  size(100, 60);
  myArrayList.add(new MyObject(3.5, 4.5, 6, "a"));
  myArrayList.add(new MyObject(5.4, 2.6, 4, "b"));
}

      

For example, if I want to check if there is an object that has coordinates (3.5, 4.5), how should I proceed? Is there an easy way to do this?

thanks for the help

+3


source to share


2 answers


You can override the equality function to define equal:

public class MyObject {

    public float x;
    public float y;
    public int iD;
    public String myType;


    public MyObject (float x, float y, int iD, String myType)
    {
        this.myType = myType;
        this.iD = iD;
        this.x = x;
        this.y = y;
    }
    @Override
    public String toString() {
        return ("[iD="+iD+" x="+x+" y="+y +" type="+myType+"]");
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof MyObject) {
            MyObject myObject = (MyObject) o;
            return myObject.iD == this.iD && myObject.myType.equals(this.myType);
        }

        return false;
    }
}

      



Attention:

I have to admit this is a dangerous way to do it. override equals can cause weird program problems if you used equals to do some other comparisons. but in a special case, perhaps you can do it.

+3


source


javadoc List#contains(Object)

claims

Returns true if this list contains the specified element.

This is not what you are trying to do here. You are not specifying the element, you want to specify the properties of the element. Don't use this method.

The long form solution is to iterate over the elements in List

and check them individually, returning true

as soon as you find it, or false

when you run out of elements.

public boolean findAny(ArrayList<MyObject> myArrayList, float targetX) {
    for (MyObject element : myArrayList) {
        if (element.x == targetX) { // whatever condition(s) you want to check
            return true;
        }
    }
    return false;
}

      

Since Java 8, there is a better way to do this using Stream#anyMatch(Predicate)

which



Returns whether any elements of this stream match the provided predicate.

If the given Predicate

is just a test for the properties you are looking for

return myArrayList.stream().anyMatch((e) -> e.x == targetX); 

      


For equality checks for floating point values, see the following:

+2


source







All Articles