2013-07-03 02:52:38 +00:00
|
|
|
/* Copyright (c) 2008-2013, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-07-21 22:36:51 +00:00
|
|
|
package java.util;
|
|
|
|
|
2009-08-04 23:36:25 +00:00
|
|
|
public class HashSet<T> extends AbstractSet<T> implements Set<T> {
|
2007-07-21 22:36:51 +00:00
|
|
|
private static final Object Value = new Object();
|
|
|
|
|
|
|
|
private final HashMap<T, Object> map;
|
|
|
|
|
2008-02-28 22:18:46 +00:00
|
|
|
public HashSet(Collection<? extends T> c) {
|
2007-07-29 23:32:23 +00:00
|
|
|
map = new HashMap(c.size());
|
|
|
|
addAll(c);
|
|
|
|
}
|
|
|
|
|
2007-07-21 22:36:51 +00:00
|
|
|
public HashSet(int capacity) {
|
|
|
|
map = new HashMap(capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashSet() {
|
|
|
|
this(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
|
|
|
return map.size();
|
|
|
|
}
|
|
|
|
|
2007-11-07 16:48:09 +00:00
|
|
|
public boolean isEmpty() {
|
|
|
|
return map.isEmpty();
|
|
|
|
}
|
|
|
|
|
2009-04-22 21:24:26 +00:00
|
|
|
public boolean contains(Object element) {
|
2007-07-22 19:06:21 +00:00
|
|
|
return map.containsKey(element);
|
|
|
|
}
|
|
|
|
|
2007-07-21 22:36:51 +00:00
|
|
|
public boolean add(T element) {
|
|
|
|
return map.put(element, Value) != Value;
|
|
|
|
}
|
|
|
|
|
2008-02-28 18:37:10 +00:00
|
|
|
public boolean addAll(Collection<? extends T> collection) {
|
|
|
|
boolean change = false;
|
|
|
|
for (T t: collection) if (add(t)) change = true;
|
|
|
|
return change;
|
|
|
|
}
|
|
|
|
|
2009-04-22 21:24:26 +00:00
|
|
|
public boolean remove(Object element) {
|
2007-07-21 22:36:51 +00:00
|
|
|
return map.remove(element) != Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterator<T> iterator() {
|
|
|
|
return new MyIterator(map.iterator());
|
|
|
|
}
|
|
|
|
|
2008-08-22 20:02:38 +00:00
|
|
|
public String toString() {
|
|
|
|
return Collections.toString(this);
|
|
|
|
}
|
|
|
|
|
2007-07-21 22:36:51 +00:00
|
|
|
private static class MyIterator<T> implements Iterator<T> {
|
|
|
|
private final Iterator<Map.Entry<T, Object>> it;
|
|
|
|
|
|
|
|
public MyIterator(Iterator<Map.Entry<T, Object>> it) {
|
|
|
|
this.it = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T next() {
|
|
|
|
return it.next().getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
return it.hasNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|