Performance: Creating an ArrayList from HashMap.values()

后端 未结 5 704
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 10:58

Question is how much it costs to create an ArrayList from a HashMap.values() Collection? Or creating the values Collection alone? Assuming Map.size() > 100k. Objects could also

5条回答
  •  [愿得一人]
    2021-02-12 11:16

    To elaborate on @Bozho's solution you can just do.

    int count = 0;
    for(Value value: map.values())
       if(count++ % 5 == 0)
         // do something.
    

提交回复
热议问题