1. What is difference between List, Set and Map?
List
Table of Contents
- List is an ordered collection.
- List allows duplicate elements.
- List allows null elements and also store many null elements because it allows duplicate elements.
The following most useful classes implementing List interface
1. ArrayList 2. LinkedList 3. Vector
Set
- Set is unordered collection.
- Set does not allows duplicate elements.
- Set just allow one null element and does not allow more than one null element.
The following most useful classes implementing Set interface
1. HashSet 2. LinkedHashSet 3. TreeSet
Map
- Map is unordered collection
- Map hold two objects per Entry e.g. Key and Value and it may contain dupliate values but not keys
- Map allow only one null key and multiple null values.
The following most useful classes implementing Map interface
1. HashMap 2. LinkedHashMap 3. Hashtable 4. TreeMap
2. When to use List, Set and Map ?
List
- Frequently accessing elements by using index. Its implementation ArrayList is faster access if you know the index
- To store duplicate elements with order.
Set
- To store unique data elements.
Map
- To store data in the form of Key and Value.
3. What is difference between ArrayList, Vector and LinkedList?
ArrayList
- It is not thread-safe by default
- It is not synchronized by default
- Better performance in case of single thread
- Collections.synchronizedList using this method we can make .synchronized ArrayList is equivalent of Vector.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.