
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between List, Set, and Map in Java
Java has a java.util package that consists of a Collection interface. This interface has many sub-interfaces and classes like List, Set, and Map. In this article, we will learn the difference between List, Set, and Map.
What is List Interface?
Java has a Collection interface and List is the sub-interface of Collection. This interface consists of methods like insert, update, delete, and search. Developers are also allowed to insert null elements.
List Interface Example
The List interface can be found in the java.util package. An example of a List is given below ?
import java.util.*; public class ListExample { public static void main (String args[]) { List<String> mylist = new ArrayList<>(); mylist.add("James Bond"); mylist.add("Superman"); mylist.add("Spiderman"); for(String superhero : mylist) System.out.println(superhero); } }
Output
James Bond Superman Spiderman
What is a Set Interface?
Set is an interface which extends the Collection interface. It is available in the java.util package. This is an interface in which duplicate items are not considered and will be ignored when the output is printed.
Set Interface Example
Here is a simple example of the Set interface.
import java.util.*; public class MySet { public static void main (String args[]) { Set<String> setmyalp = new HashSet<String>(); setmyalp.add("A"); setmyalp.add("B"); setmyalp.add("C"); setmyalp.add("D"); setmyalp.add("E"); System.out.println(setmyalp); } }
Output
[A, B, C, D, E]
What is Map Interface?
The map is an interface which can be found in the java.util package. This is an interface which is used to match a key with a value. After storing the key and value in the Map interface, users can just use the key to get its value.
Map Interface Example
Here is an example of using the Map interface.
import java.util.*; public class MyMap { public static void main (String args[]) { Map<String, String> mapvalue =new HashMap<String, String>(); mapvalue.put("Fruit" , "Apple"); mapvalue.put("Vegetable" , "Potato"); mapvalue.put("Nut" , "Groundnut"); for(Map.Entry me : mapvalue.entrySet()) { System.out.println(me.getKey() + " " + me.getValue()); } } }
Output
Fruit Apple Nut Groundnut Vegetable Potato
Difference between List, Set, and Map Interfaces in Java
List, Set, and Map interfaces belong to the java.util package and they extend the Collection Interface. There are a few differences between them which can be found in the table below ?
List | Set | Map |
---|---|---|
Duplicate values are allowed in the List interface. | No duplicate values are allowed in the Set interface. | No duplicate values are allowed in the Map interface. |
An insertion order is maintained in the list. | No insertion order is maintained. | No insertion order is maintained. |
Any number of null values can be added to the list. | Only one null value can be added to the set. | Map allows a single null key but null values can be of any number. |
The classes that implement the List interface are
|
The classes that implement the Set interface are ?
|
Classes that implement the Map interface are ?
|
An element from an index can be extracted through the get() method in the List interface | No get method is available in the Set interface to retrieve an element from a particular index. | No get() method is available in the Map interface to retrieve an element from a particular index. |
List can be used to retrieve elements from different indexes. | The set interface can be used to create a collection which consists of only unique values. | Map can be used to keep the data in the format of a key-value pair. |
List can be traversed with the help of ListIterator. | Set elements can be traversed with the help of Iterator. | Key value pairs can be traversed with the help of keyset, value, and entrySet. |
Conclusion
List, Set, and Map interfaces belong to the java.util package and they extend the Collection interface. List allows duplicate values and the insertion of values is ordered. Set and Map interfaces do not allow duplicate values and the insertions are not organized. All these interfaces are used for different purposes.
FAQs on List Vs. Set Vs. Map
1. To which package do List, Set, and Map interfaces belong?
List, Map, and Set interfaces belong to the java.util package. They extend the Collection interface. The List and Set interfaces are used to add, remove, or update elements while the Map interface follows a key-value pair.
2. How many null values can be inserted in all these interfaces?
Any amount of null values can be inserted in the List interface while only one null value is supported by Set. In the Map interface, the key can only have one null but the value can have any number of null.
3. Which classes implement the List interface?
List interface can be implemented by
- ArrayList
- LinkedList
4. Which interface allows duplicate values to be inserted?
Duplicate values can be inserted in the List interface but this is not the case with the Set and Map interfaces.
5. Which of the interfaces has get method to retrieve elements at a particular index?
Only the List interface has the get() method to retrieve elements from a particular index.