Here are a few other ideas.
Using streams:
public void add(T t) { if (!members.stream().anyMatch(m -> m.getClass() == t.getClass())) { members.add(t); } }
Using AbstractSet and HashMap :
class ClassSet<E> extends AbstractSet<E> { private final Map<Class<?>, E> map = new HashMap<>(); @Override public boolean add(E e) { // this can be // return map.putIfAbsent(e.getClass(), e) != null; // in Java 8 Class<?> clazz = e.getClass(); if (map.containsKey(clazz)) { return false; } else { map.put(clazz, e); return true; } } @Override public boolean remove(Object o) { return map.remove(o.getClass()) != null; } @Override public boolean contains(Object o) { return map.containsKey(o.getClass()); } @Override public int size() { return map.size(); } @Override public Iterator<E> iterator() { return map.values().iterator(); } }
A HashMap can also be used without wrapping it in Set . The Set interface is defined around equals and hashCode , so any implementation that differs from this is technically non-contractual. Alternatively, you can use LinkedHashMap if the values ββare repeated frequently.
source share