When to use HashMap over LinkedList or ArrayList and vice-versa

前端 未结 4 709
挽巷
挽巷 2020-12-02 05:58

What is the reason why we cannot always use a HashMap, even though it is much more efficient than ArrayList or LinkedList in add,remove operations, also irrespective of the

4条回答
  •  猫巷女王i
    2020-12-02 06:29

    Lists and Maps are different data structures. Maps are used for when you want to associate a key with a value and Lists are an ordered collection.

    Map is an interface in the Java Collection Framework and a HashMap is one implementation of the Map interface. HashMap are efficient for locating a value based on a key and inserting and deleting values based on a key. The entries of a HashMap are not ordered.

    ArrayList and LinkedList are an implementation of the List interface. LinkedList provides sequential access and is generally more efficient at inserting and deleting elements in the list, however, it is it less efficient at accessing elements in a list. ArrayList provides random access and is more efficient at accessing elements but is generally slower at inserting and deleting elements.

提交回复
热议问题