Wouldn't Collections.disjoint(A, B) work? From the documentation:
Returns
trueif the two specified collections have no elements in common.
Thus, the method returns false if the collections contains any common elements.
Something like 'contains any' for Java set? - Stack Overflow
java - How contains method of Set collection works - Stack Overflow
How to check in java if Set contains object with some string value? - Stack Overflow
How to check if a Java set contains multiple items at once?
Wouldn't Collections.disjoint(A, B) work? From the documentation:
Returns
trueif the two specified collections have no elements in common.
Thus, the method returns false if the collections contains any common elements.
Stream::anyMatch
Since Java 8 you could use Stream::anyMatch.
setA.stream().anyMatch(setB::contains)
From the Object javadoc
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
If the hashCode() method is not overridden then the Object's hashCode() method is used which is default implementation.
In your case you had overridden the equals method but you use the default implementation of the hashCode() as you didn't override the hashCode() method in your UserDefinedName class.
The UserDefinedName class overrides the equals method, and the hashCode contract demands that equal objects have equal hash codes. To fulfill this contract, you must override hashCode whenever you override equals
add following code and it will work.
public int hashCode() {
return 37 * first.hashCode() + last.hashCode();
}
Because an hashSet uses the hashcode value of the object to store it. So it requires that you have to override the method hashCode in your own class. Since you didn't override it, it uses the hashCode method inherited from the object class.
The hashcode from the object class is calculated with the memory address of your object, i.e :
UserDefinedName p = new UserDefinedName("Carballo", "Videl");
System.out.println(p);
System.out.println(0x1e5e2c3);
System.out.println(p.hashCode());
Output :
UserDefinedName@1e5e2c3
31843011
31843011
So if you tried this, you will see that it outputs true :
Set<UserDefinedName> s = new HashSet<UserDefinedName>();
UserDefinedName p = new UserDefinedName("Carballo", "Videl");
s.add(p);
System.out.println(s.contains(p));
Now if you want to compare your User class correclty, you will have to override your hashCode method (you can generated it with eclipse) to generate the hashCode your object field by field.
If you have this method to your class, this will print true for the code you provide.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((last == null) ? 0 : last.hashCode());
return result;
}
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
Yes this is possible by overriding 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
I have a set that contains different strings.
I also have a list of strings.
If any of the strings in the list, are in the set, I want to return true.
Currently I am using .contains along with a bunch of if else statements. But I want to clean this up using syntactic sugar. What would you recommend?