0

I'm trying to figure out how to test if a list of objects contains an object that contains a particular string as an attribute.

So here Is my list of objects

ArrayList<mObject> objectsList;

And here is my mObject class

public class mObject {

    String x = "True";

}

And here I try to make some kind of test

if(ObjectsList.contains(object.x == "True")){
    return true;
} else {
    return false;
}

I know the syntax is kind of ridiculous, but how can I make it technically correct?

the_prole
  • 8,275
  • 16
  • 78
  • 163
  • With Java 8 you could use a Stream, but in the end, you will probably have to iterate over the list and return true, as soon as one object contains the desired value. – Florian Schaetz Nov 12 '15 at 08:04

4 Answers4

0

for Java 8 you can use

objectsList.stream().anyMatch(myObject -> myObject.x.equals("True"));

please look at the code standards about naming variables (lowercase start and camel case) and classes (uppercase start)

PS: As your variable is a string with "True" value - are you sure not wanting to have a boolean?

Emerson Cod
  • 1,990
  • 3
  • 21
  • 39
0

Java 8:

List<mObject> list = ObjectsList.stream().filter(obj -> obj.x.equals( "True")).collect(Collectors.toList()).;
if (list.size() != 1) {
    return false;
} else {
    return true;
}

and, please, observe java naming conventions

Dmitrii Bocharov
  • 872
  • 6
  • 21
0

I override equals() method to be able to use .contians() because it comparing objects using equals(),as your List is List<mObject> (List of custom object) so i had to override the .equals() to be able to use .contains() probably

public class mObject {
    private String x = "True";
    public String getX(){
      return x;
    }
    public void setX(String x){
      this.x=x;
    }
    @Override
    public boolean equals(Object obj) {
       if (obj == null) {
           return false;
       }
       if (getClass() != obj.getClass()) {
           return false;
       }
       final mObject other = (ttt) obj;
       if (!Objects.equals(this.x, other.x)) {
           return false;
       }
       return true;
   }
}

To make your test :-

if(ObjectsList.contains(object) && object.getX().equals("True")){
   return true;
} else {
    return false;
}

Please see the difference between == and .equals() when comparing string from this and that link

Community
  • 1
  • 1
karim mohsen
  • 2,164
  • 1
  • 15
  • 19
  • I'm testing if ANY object in the object list contains a certain string attribute, not if a particular object exists in a list which contains a certain string attribute (the way your wrote it). The solution is iterative. – the_prole Nov 12 '15 at 19:55
0

For previuos versions of Java 8:

public class MObject {

    private String x = "True";

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

}

You can iterate over the list:

ArrayList<MObject> objectsList; //Init your object list.

for(MObject object : objectsList) {
    if(object.getX().equals("True")) {
        return true;
    } else {
        return false;
    }
 }
Miguel
  • 536
  • 6
  • 20