The Artima Developer Community
Sponsored Link

Java Buzz Forum
When to use LinkedHashSet vs TreeSet vs HashSet in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
When to use LinkedHashSet vs TreeSet vs HashSet in Java Posted: Jan 1, 2014 5:14 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: When to use LinkedHashSet vs TreeSet vs HashSet in Java
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
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.
Read more ยป

Read: When to use LinkedHashSet vs TreeSet vs HashSet in Java

Topic: Android Alert Dialog Example Previous Topic   Next Topic Topic: Project Student: Webservice Client with Jersey

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use