Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in java if Set contains object with some string value?

I have Set of objects. Each object has String value.

I need to select all objects that have this value equal to "direction".

Is it possible without iterating over the set?

like image 549
user710818 Avatar asked Oct 16 '12 07:10

user710818


People also ask

How do you check if a set contains a String?

Set contains() method in Java with Examples Set. contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element.

How do you check if an object contains a String in Java?

The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise.

How do you know if a set contains an object?

Use the has() method to check if a Set contains an object, e.g. set.has(obj) . The object has to be passed by reference to the has method to get a reliable result. The has method tests for the presence of a value in a Set and returns true if the value is contained in the Set .

How do you check if an object contains a value in Java?

The contains(value) method of Properties class is used to check if this Properties object contains any mapping of this value for any key present in it. It takes this value to be compared as a parameter and returns a boolean value as a result. This method is more expensive than the containsKey() method.


2 Answers

In general, no. You need to iterate over the set and check each object to see if the property is equal to the value you are searching for. This is an O(n) operation.

There is one situation in which you could do it without iterating. If your object's equals method is defined in terms of equality of that String property, and if the hashCode method is also implemented correctly, then you can use the hashSet.contains to find an object with the correct value in O(1) time without requiring iterating over the set.

As I mentioned, this is a very specific use case and not a general solution. It might be useful if the string was some sort of unique identifier, but it won't work for your specific use case.

You might also want to consider other collections that would be better suited to your use case. You could for example if you are using Guava then you could consider using a Multimap.

Related

  • HashMap with multiple values under the same key
like image 141
Mark Byers Avatar answered Sep 24 '22 00:09

Mark Byers


Yes this is possible by overwriting the equals() method.

@Override  public boolean  equals (Object object) {  } 

You just want to check everything works in the equals method.

Code:

package com.webapp.test;  import java.util.ArrayList; import java.util.List;  public class EmployeeModel {          public EmployeeModel(String name, String designation, long age) {         this.name = name;         this.designation = designation;         this.age = age;     }      private String name;     private String designation;     private long age;      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public String getDesignation() {         return designation;     }      public void setDesignation(String designation) {         this.designation = designation;     }      public long getAge() {         return age;     }      public void setAge(long age) {         this.age = age;     }      @Override     public boolean equals (Object object) {         boolean result = false;         if (object == null || object.getClass() != getClass()) {             result = false;         } else {             EmployeeModel employee = (EmployeeModel) object;             if (this.name == employee.getName() && this.designation == employee.getDesignation() && this.age.equals(employee.getAge())) {                 result = true;             }         }         return result;     } }  public static void main(String args[]) {     EmployeeModel first = new EmployeeModel("Sameer", "Developer", 25);     EmployeeModel second = new EmployeeModel("Jon", "Manager", 30);     EmployeeModel third = new EmployeeModel("Priyanka", "Tester", 24);      List<EmployeeModel> employeeList = new ArrayList<EmployeeModel>();     employeeList.add(first);     employeeList.add(second);     employeeList.add(third);      EmployeeModel checkUserOne = new EmployeeModel("Sameer", "Developer", 25);     System.out.println("Check checkUserOne is in list or not ");     System.out.println("Is checkUserOne Present = ? " + employeeList.contains(checkUserOne));      EmployeeModel checkUserTwo = new EmployeeModel("Tim", "Tester", 24);     System.out.println("Check checkUserTwo is in list or not");     System.out.println("Is checkUserTwo Present = ? " + employeeList.contains(checkUserTwo));  } 

Output:

Check checkUserOne is in list or not  Is checkUserOne Present = ? true Check checkUserTwo is in list or not  Is checkUserTwo Present = ? false 
like image 36
Sameer Kazi Avatar answered Sep 27 '22 00:09

Sameer Kazi