The isEmpty
and entrySet
Methods
// Returns true iff this map contains no key/value mappings. @Override public boolean isEmpty () { return size() == 0; } // Returns a set of the key/value pairs in this Map. public SetentrySet () { Iterator<K> it = keyIterator(); List entries = new ArrayList (); while (it.hasNext()) { K key = it.next(); V value = getVal (key); Entry<K,V> entry = new SimpleImmutableEntry<K,V> (key, value); entries.add (entry); } return new ListSet<K,V> (entries); }
The isEmpty
method calls size()
to compute its result.
That's fine if the size
method is fast.
If size
is slow in some implementation,
the isEmpty
method can be overridden
in a subclass that's part of that implementation.
The entrySet
method creates an iterator
by copying all keys into a List
and calling that list's iterator()
method.
This is a fairly common idiom in Java.
All of the other methods listed in the
Map<K,V>
interface are defined within
and inherited from AbstractMap<K,V>
.