Reputation: 31
Say I have a custom datatype:
public class Notification {
private String name;
private String location;
private String message;
// Constructors, getter, setters, etc.
}
And I want to add objects (of Notification
type) from a list to a Set to get rid of duplicates. But just simply adding them to a Set doesn't work because Java doesn't know how to check to see if there are duplicates.
How do I tell Java that when I add a Notification
object to a set, I want it to only check whether the name
attribute is unique or not (disregarding other fields)?
Upvotes: 2
Views: 796
Reputation: 31
I found that using Map (keyed by name attribute) as mentioned by Louis Wasserman better than overriding hashChode and equals. If you're trying to do what I'm doing, you can implement something like the following:
Map<String, Notification> m = new HashMap();
for (Notification n : notifications) {
m.put(n.getHostname(), n);
}
Iterator it = m.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
When you iterate through the Map, you'll see that it figures out whether the object is unique or not based on the key! Much easier than using a Set.
Upvotes: 1
Reputation: 14978
According to the Set documentation, it will contain a
A collection that contains no duplicate elements. More formally, sets contain no pair of elements
e1
ande2
such thate1.equals(e2)
, and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
So from this we can see that if you override the equals
method for Notification
you can specify there how two elements (two notifications) are compared.
Also I recommend you to override the hashCode
method in case you use an implementation of Set such as HashSet
For example you could go with something like this:
public class Notification {
private String name;
private String location;
private String message;
// Constructors, getter, setters, etc.
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Notification)) return false;
Notification notif = (Notification) o;
return Objects.equals(name, notif.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
Upvotes: 0