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?
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.
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.
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 .
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With