forEach on HashMap and IdentityHashMap

Remi Forax forax at
Wed Jun 12 09:28:22 UTC 2013

On 06/12/2013 10:20 AM, Paul Sandoz wrote:
> On Jun 12, 2013, at 9:45 AM, Peter Levart <peter.levart at> wrote:
>> On 06/11/2013 11:06 PM, Mike Duigou wrote:
>>> On Jun 11 2013, at 09:59 , Remi Forax wrote:
>>>> Hi guys,
>>>> This mail was originality sent to lambda-lib EG but Brian ask me to also send it here to not forget.
>>>> We should provide an implementation of forEach for HashMap that avoid to create an Iterator,
>>>> HashMap is too common to not have it's dedicated implementation
>>> I assume you mean the forEach on the entrySet.
>>>> (ArrayList by example has its own implementation of forEach).
>>>> There is also another implementation that should have a specific implementation IdentityHashMap because
>>>> entrySet() creates map entries on the fly so having a specific implementation that will not allocatin entries will improve performance
>>>> (a lot for big identity hashmap).
>>> Are you suggesting adding a IdentityMap.forEach(BiConsumer) or re-using the Map.Entry passed to the consumer in entrySet().forEach(Consumer)?
>> That's a very good idea. Why not adding the following default method to Map interface:
>> default void forEach(BiConsumer<? super K, ? super V> sink) {
>>     for (Map.Entry<K, V> entry : entrySet()) {
>>         sink.apply(entry.getKey(), entry.getValue());
>>     }
>> }
> It's already there :-)
> Just need some Map impls to override it. I logged a bug, JDK-8016446, to track it.
> Paul.

Thanks Paul,
By the way, could you add to the description of the bug that if we 
override Map.forEach for HashMap we also need to override it for 


>> ...and override it in IHM, HM/LHM, CHM with optimal implementations...
>> Regards, Peter
>>> Mike
>>>> cheers,
>>>> Rémi

More information about the core-libs-dev mailing list