Which one is faster? List.contains() or Map.containsKey()

后端 未结 4 1437
星月不相逢
星月不相逢 2021-01-31 17:34

I\'m writing an algorithm where I look for pairs of values which when added together results in another value I\'m looking for.

I figured out that using a Map

4条回答
  •  被撕碎了的回忆
    2021-01-31 18:19

    Map.containsKey() considering that you are using a HashMap since searching in HashMap is done in O(1).

    List.contains() generally should resort to sequential search or a binary search thus the complexity will be atleast O(log n)

提交回复
热议问题