What is the difference between Lists, ArrayLists, Maps, Hashmaps, Collections etc..?

后端 未结 10 1752
-上瘾入骨i
-上瘾入骨i 2021-01-30 01:38

I\'ve been using HashMaps since I started programming again in Java without really understanding these Collections thing.

Honestly I am not really sure if using HashMaps

10条回答
  •  梦毁少年i
    2021-01-30 02:19

    List Vs Set Vs Map 1) Duplicity: List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes. Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements. Map stored the elements as key & value pair. Map doesn’t allow duplicate keys while it allows duplicate values.

    2) Null values: List allows any number of null values. Set allows single null value at most. Map can have single null key at most and any number of null values.

    3) Order: List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order. Similar to Set Map also doesn’t stores the elements in an order, however few of its classes does the same. For e.g. TreeMap sorts the map in the ascending order of keys and LinkedHashMap sorts the elements in the insertion order, the order in which the elements got added to the LinkedHashMap.enter code here

提交回复
热议问题