Skip to content

Instantly share code, notes, and snippets.

@ThomasLau
Created March 20, 2015 01:54
Show Gist options
  • Save ThomasLau/27c9880b5af7e5989ded to your computer and use it in GitHub Desktop.
Save ThomasLau/27c9880b5af7e5989ded to your computer and use it in GitHub Desktop.
new function method HashTable.forEach
@SuppressWarnings("unchecked")
@Override
public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action); // explicit check required in case
// table is empty.
final int expectedModCount = modCount;
Entry<?, ?>[] tab = table;
for (Entry<?, ?> entry : tab) {
while (entry != null) {
action.accept((K)entry.key, (V)entry.value);
entry = entry.next;
if (expectedModCount != modCount) {
throw new ConcurrentModificationException();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment