mirror of
https://github.com/corda/corda.git
synced 2025-02-09 12:21:22 +00:00
Implement LinkedHashSet
This implementation is intentionally simple. If and when the need arises, we can always implement a more performant version. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
parent
58ea1442fd
commit
7fe3979280
89
classpath/java/util/LinkedHashSet.java
Normal file
89
classpath/java/util/LinkedHashSet.java
Normal file
@ -0,0 +1,89 @@
|
||||
/* Copyright (c) 2008-2013, Avian Contributors
|
||||
|
||||
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. */
|
||||
|
||||
package java.util;
|
||||
|
||||
public class LinkedHashSet<T> extends AbstractSet<T> implements Set<T> {
|
||||
private static final Object Value = new Object();
|
||||
|
||||
private final LinkedHashMap<T, Object> map;
|
||||
|
||||
public LinkedHashSet(Collection<? extends T> c) {
|
||||
map = new LinkedHashMap(c.size());
|
||||
addAll(c);
|
||||
}
|
||||
|
||||
public LinkedHashSet(int capacity) {
|
||||
map = new LinkedHashMap(capacity);
|
||||
}
|
||||
|
||||
public LinkedHashSet() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
public boolean contains(Object element) {
|
||||
return map.containsKey(element);
|
||||
}
|
||||
|
||||
public boolean add(T element) {
|
||||
return map.put(element, Value) != Value;
|
||||
}
|
||||
|
||||
public boolean addAll(Collection<? extends T> collection) {
|
||||
boolean change = false;
|
||||
for (T t: collection) if (add(t)) change = true;
|
||||
return change;
|
||||
}
|
||||
|
||||
public boolean remove(Object element) {
|
||||
return map.remove(element) != Value;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
public Iterator<T> iterator() {
|
||||
return new MyIterator(map.iterator());
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Collections.toString(this);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user