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

what are advantages of HQL? what are advantages of Native SQL? what are advantages of Criteria API?

1 Answers   TCS,


please mail me the interview question based on java/j2ee

0 Answers   Wipro,


What is java collection? : java collections

0 Answers  


What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with simple example? Thanks, Bose. Infosys 2 In which way does a Primitive data type is passed ? Sun- Microsystems 4 what is the use of declaring constructor as private? Sai- Softech 5 what is difference between global methods and local methods? 1 What is meant by class loader and how many types are there? Apple 1 what is meaning of JIT? 4 What is an abstract class? Wipro 5 what is filters and its use? Virtusa 2 why is multiple inheritance not allowed in java? Elementus-Technologies 7 Differences between jdk 1.4 and 1.5 Wipro 4 is JVM platform dependent or independent..? IBM 6 what is main purpose of abstract class? 5 what is difference between colection and collections? Tech-Mahindra 11 What error occurs if a try-catch-finally statement sequence does not have a catch clause? 2 Explain the difference between scrollbar and scrollpane? 1 Explain the differences between public, private, protected and static? 4 Explain the difference between getAppletInfo and getParameterInfo? 1 Is Cegonsoft Pvt.Ltd. a good Institute? 4 Why only one Class is public in one file? Explain in details. Thanks in Advance. 11 Which method will get invoked first in a stand alone application?

0 Answers  


what is jndi?

2 Answers  


How is hashset maintained in memory by java ?

0 Answers  


what is difference between vector and arraylist?.

4 Answers   C DAC, HCL,


What are the different types of collections views being provided by the map interface? : java collections

0 Answers  


What is the servletcontext listener and what is JspContext and page context pls tell in simple words.

1 Answers   IBM,


How many controlers are there in spring? Plz send me ans

1 Answers   HCL,


Which server-side script takes the input from JavaScript, can access the database if it needs to, and processes the data.

0 Answers  


What are the interfaces in java collections? : java collections

0 Answers  


Categories