Proposed rename of Map.forEach(BiBlock<? super K, ? super V> block)

Raab, Donald Donald.Raab at gs.com
Tue Dec 25 19:34:39 PST 2012


Can we rename the forEach(BiBlock<? super K, ? super V> block) method to forEachKeyValue(BiBlock<? super K, ? super V> block) on Map please?

In GS Collections, our MapIterable<K, V> interace extends RichIterable<V> which extends Iterable<V>.  This is a choice which makes it consistent with Smalltalk.  This results in us having a method forEach(Procedure<? super V>) defined on all our Map implementations.  This will also cause us to have a method forEach(Block<? super V> block) defined when JDK 8 is released.  Having a third overloaded forEach() method will cause a lot of confusion for us.  

https://github.com/goldmansachs/gs-collections/blob/master/collections-api/src/main/java/com/gs/collections/api/map/MapIterable.java#L33

Hopefully there are no plans to have Map re-defined as Map<K, V> extends Iterable<Entry<K, V>> in JDK 8.  Otherwise this would result in forEach() having to be redefined as forEach<Map.Entry<K, V>>.  

For future reference, Trove defines these methods in THashMap:

forEachKey(TObjectProcedure<? super K> procedure)
forEachValue(TObjectProcedure<? super V> procedure)
forEachEntry(TObjectObjectProcedure<? super K,? super V> procedure)

GS Collections defines these methods in MapIterable:

forEach(Procedure<? super V> procedure) // extended from RichIterable<V> which ultimately extends Iterable<V>
forEachKey(Procedure<? super K> procedure) 
forEachValue(Procedure<? super V> procedure)
forEachKeyValue(Procedure2<? super K, ? super V> procedure)

I would suggest adding the other two methods forEachKey(Block<? super K>) and forEachValue(Block<? super V>) to Map for JDK 8 even though this will result in more casting at call sites for users of Trove and GS Collections since our methods will become overloads of the equivalent methods on Map.  The current recommended workaround as I understand it will be to have our function types extend Block and BiBlock when JDK 8 is released.

  




More information about the lambda-libs-spec-observers mailing list