Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the classes in the java collection framework? : java collections

1024


can u draw class/object diagram for ATM

6081


Which java collection class can be used to maintain the entries in the order in which they were last accessed?

1027


What is deque in the java collections framework? : java collections

1051


Explain the OOPS concept in Realtime Scenarion ? Take example as CAR. Please explain indetail ?

3027


What are the differences between the java collection and the java list? : java collections

1096


my interviewer asked me what technical specification you used how to answer that question

2364


What are the considerations to be made in case of loops in java ?

1017


What is iterator in the java collections framework? : java collections

957


How do I find jre path in windows?

1005


what is mean by hasing and maping in java platform and advantage?

2356


who will give req's to u?. how they send req's to u? . what design documents contains?. when bugs raised on other developer code how to report to them?.(throgh mail or ........). how to retrive 100 recods from dao layer to presentation layer.using which collection?. what is sequence diagram.?.

2310


In hyderabad, which s/w training center is best for java, other than corejava what r the new tools to learn in java,which tool is best & have current requirement,pls give me information about java to learn ?

2147


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

2079


Should we create system software ( e.g operating system ) in java ?

1028