### Question:
SelectedSelectionKeySet extends AbstractSet<SelectionKey> { @Override public Iterator<SelectionKey> iterator() { return new Iterator<SelectionKey>() { private int idx; @Override public boolean hasNext() { return idx < size; } @Override public SelectionKey next() { if (!hasNext()) { throw new NoSuchElementException(); } return keys[idx++]; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } SelectedSelectionKeySet(); @Override boolean add(SelectionKey o); @Override boolean remove(Object o); @Override boolean contains(Object o); @Override int size(); @Override Iterator<SelectionKey> iterator();  }### Answer:
@Test public void iterator() { SelectedSelectionKeySet set = new SelectedSelectionKeySet(); assertTrue(set.add(mockKey)); assertTrue(set.add(mockKey2)); Iterator<SelectionKey> keys = set.iterator(); assertTrue(keys.hasNext()); assertSame(mockKey, keys.next()); assertTrue(keys.hasNext()); assertSame(mockKey2, keys.next()); assertFalse(keys.hasNext()); try { keys.next(); fail(); } catch (NoSuchElementException expected) { } try { keys.remove(); fail(); } catch (UnsupportedOperationException expected) { } }