When to use the Collection Classes(HashSet, LinkedHashSet,
TreeSet....etc) in real time scenario ?



When to use the Collection Classes(HashSet, LinkedHashSet, TreeSet....etc) in real time scenario ?..

Answer / manja

LinkedHashSet, TreeSet and HashSet are three of most popular implementation of Set interface in Java Collection Framework. Since they implement Set interface, they follow it's contract for not allowing duplicates. All these implementation except, TreeSet uses equals() method to check for duplicates, on the other hand TreeSet use compareTo() or compare() method for comparing objects and can break Set interface contract of unique element, if equals method is not consistent with compareTo() or compare() method. In this Java Collection tutorial, we will see difference between LinkedHashSet vs TreeSet vs HashSet on different points e.g. speed, performance, ordering, synchronization etc. Based upon these differences we can also decide when to use LinkedHashSet vs TreeSet vs HashSet in Java. TL;DR, Use HashSet for all general purpose usage i.e. where you need to store only unique elements without any ordering requirement. If you need to maintain order on which elements are added into Set then use LinkedHashSet, it provides ordering with little impact on performance. Use TreeSet when you absolutely nee to keep elements in specific sorted order e.g. keeping employee in increasing order of their age or salary. Remember, TreeSet is significantly slower than LinkedHashSet and HashSet because of this sorting overhead.

Is This Answer Correct ?    6 Yes 0 No

Post New Answer

More Java J2EE AllOther Interview Questions

i need java 1+ experience resume on java developer kindly send me my mail id:srikanthreddy1.marla@gmail.com

1 Answers  


Which sorting algorithm is used by collections.sort() in java ?

0 Answers  


Explain JSP life cycle?

2 Answers   Accenture,


How do I find jre path in windows?

0 Answers  


Hi frnds how to lock an user when he enter wrong credentials more than 3 time using java or j2ee tech take username and password in a bean no need to connect DB and validate give me some sample application code or links its urgent for me thanks in advance

0 Answers   NSN,






httt method

2 Answers   HCL,


Can any one tell me How to explain bank project desription in interview

3 Answers   Infosys, Wipro,


VSS Objectives

1 Answers  


how to deploy the web application ?

2 Answers   Accent,


What is the use of the list interface in the java collection? : java collections

0 Answers  


What are the main classes of the list interfaces? : java collections

0 Answers  


What is the Spring2.5 MVC Navigation flow?

1 Answers   IBM,


Categories