mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8315850: Improve AbstractMap anonymous Iterator classes
Reviewed-by: liach, smarks
This commit is contained in:
parent
424de295a6
commit
d95b548c78
1 changed files with 20 additions and 32 deletions
|
@ -350,23 +350,9 @@ public abstract class AbstractMap<K,V> implements Map<K,V> {
|
||||||
public Set<K> keySet() {
|
public Set<K> keySet() {
|
||||||
Set<K> ks = keySet;
|
Set<K> ks = keySet;
|
||||||
if (ks == null) {
|
if (ks == null) {
|
||||||
ks = new AbstractSet<K>() {
|
ks = new AbstractSet<>() {
|
||||||
public Iterator<K> iterator() {
|
public Iterator<K> iterator() {
|
||||||
return new Iterator<K>() {
|
return new KeyIterator();
|
||||||
private Iterator<Entry<K,V>> i = entrySet().iterator();
|
|
||||||
|
|
||||||
public boolean hasNext() {
|
|
||||||
return i.hasNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
public K next() {
|
|
||||||
return i.next().getKey();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove() {
|
|
||||||
i.remove();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
|
@ -409,23 +395,9 @@ public abstract class AbstractMap<K,V> implements Map<K,V> {
|
||||||
public Collection<V> values() {
|
public Collection<V> values() {
|
||||||
Collection<V> vals = values;
|
Collection<V> vals = values;
|
||||||
if (vals == null) {
|
if (vals == null) {
|
||||||
vals = new AbstractCollection<V>() {
|
vals = new AbstractCollection<>() {
|
||||||
public Iterator<V> iterator() {
|
public Iterator<V> iterator() {
|
||||||
return new Iterator<V>() {
|
return new ValueIterator();
|
||||||
private Iterator<Entry<K,V>> i = entrySet().iterator();
|
|
||||||
|
|
||||||
public boolean hasNext() {
|
|
||||||
return i.hasNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
public V next() {
|
|
||||||
return i.next().getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove() {
|
|
||||||
i.remove();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
|
@ -924,4 +896,20 @@ public abstract class AbstractMap<K,V> implements Map<K,V> {
|
||||||
public <T> T[] toArray(T[] a) { return view().toArray(a); }
|
public <T> T[] toArray(T[] a) { return view().toArray(a); }
|
||||||
public String toString() { return view().toString(); }
|
public String toString() { return view().toString(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterator implementations.
|
||||||
|
|
||||||
|
final class KeyIterator implements Iterator<K> {
|
||||||
|
private final Iterator<Entry<K,V>> i = entrySet().iterator();
|
||||||
|
public boolean hasNext() { return i.hasNext(); }
|
||||||
|
public void remove() { i.remove(); }
|
||||||
|
public K next() { return i.next().getKey(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
final class ValueIterator implements Iterator<V> {
|
||||||
|
private final Iterator<Entry<K,V>> i = entrySet().iterator();
|
||||||
|
public boolean hasNext() { return i.hasNext(); }
|
||||||
|
public void remove() { i.remove(); }
|
||||||
|
public V next() { return i.next().getValue(); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue