Javatpoint Logo
Javatpoint Logo

List vs Set vs Map in Java

In this article, you will learn about the difference between List, Set, and Map. But before discussing the differences, you must know about List, Set, and Map with their examples.

What is List interface?

The Java collections interface's sub-interface is called the List interface. It offers index-based ways to add, update, remove, and search for elements. Duplicate elements are also possible, and the null entries can also be kept in the list. Lists enable positional access and the insertion of elements while maintaining the insertion order. It was located in the package java.util. For a better understanding, let's look at an illustration where you can see how to add elements in Java using the list interface.




What is Set?

The Set is present in Java and uses an unordered approach. Java's collection interface enhances with the util package. In the Set, duplicate items will not be printed in the output and will not be taken into account. For a better understanding, let's look at an example where you will see how to add items using a set interface in Java. Let's look at it.



[Java, Operating System, DBMS, Machine Learning, Python]

What is Map?

Java Map, or java.util.Map, is a java interface. A key and a value are mapped by the term "map". A Java Map is able to hold pairs of keys and values in more detail. An individual value is connected to each key, and the value can later be retrieved using only the key after being saved in a Map. For a better understanding, let's look at an example where you can see how to add items in Java by utilising the Map interface.



Rahul 103
Shyam 102
Krishna 104
Rajat 101

Head-to-head comparison between List, Set and Map

Here, you will learn the head-to-head comparisons between List, Set, and Map. The main differences between List, Set, and Map are as follows:

List Set Map
The elements can be duplicated in the list interface. Duplicate elements are not permitted in a set. Duplicate elements are not allowed on the map.
The list preserves the order of inclusion. There is no insertion order maintained by sets. Furthermore, there is no insertion order maintained by the map.
Any quantity of null values can be added. But there is only really one null value in the set. Null values can be present in any number and up to one null key in the map.
LinkedList and Array List are classes used for list implementation. The classes used to implement sets include HashSet, LinkedHashSet, and TreeSet. HashMap, HashTable, ConcurrentHashMap, and LinkedHashMap are the different map implementation classes.
The list has a get() method that allows you to retrieve an entry by index. The elements at a given index cannot be retrieved from a set using the get method. The map provides no get method to retrieve the elements at a certain index.
If you regularly need to access the elements using the index, you can utilise the list. Set can be used to build a collection of distinct elements if you wish to. The map can be used to store data in the form of key-value pairs.
Listlterator method returns the listlterator to iterate through the list's elements. The set elements can be traversed using an iterator. It offers three type of iterators, including value set, and keyset.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA