implement java.util.TreeMap

This commit is contained in:
Joel Dice
2009-07-25 15:41:43 -06:00
parent 08e9a99cb5
commit d327f6ba5a
5 changed files with 341 additions and 66 deletions

View File

@ -64,6 +64,22 @@ public class Collections {
return sb.toString();
}
static String toString(Map m) {
StringBuilder sb = new StringBuilder();
sb.append("{");
for (Iterator<Map.Entry> it = m.entrySet().iterator(); it.hasNext();) {
Map.Entry e = it.next();
sb.append(e.getKey())
.append("=")
.append(e.getValue());
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
static class IteratorEnumeration<T> implements Enumeration<T> {
private final Iterator<T> it;
@ -312,4 +328,44 @@ public class Collections {
public static <T> Set<T> unmodifiableSet(Set<T> hs) {
return new UnmodifiableSet<T>(hs);
}
static class KeyIterator<K, V> implements Iterator<K> {
private final Iterator<Map.Entry<K, V>> it;
public KeyIterator(Iterator<Map.Entry<K, V>> it) {
this.it = it;
}
public K next() {
return it.next().getKey();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
static class ValueIterator<K, V> implements Iterator<V> {
private final Iterator<Map.Entry<K, V>> it;
public ValueIterator(Iterator<Map.Entry<K, V>> it) {
this.it = it;
}
public V next() {
return it.next().getValue();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
}