answersLogoWhite

0


Best Answer

The difference between these two sets is simply the order/algorithm in which items are added to/accessed/removed from the map. The circumstances determine which particular implementation is better/faster.

A treeSet organizes data in a tree through use of Comparator (natural ordering) and the hashSet organizes data in a hash table (using a hash function).

See the Java API for more info:

http://www.j2ee.me/javase/6/docs/api/java/util/TreeSet.html

http://java.sun.com/javase/6/docs/api/java/util/HashSet.html

The previous answer (unless I'm mistaken) is pretty much completely wrong...

----------- Previous Answer ---------

In a treeset data are placed in order... while in a hashset data is randomly placed...

A treeset is more efficient in case of search element.

As hashSet use the hash function to store the values, it does not maintain the order of elements, but TreeSet maintains the natural odering of elements (you don't need to sort it).

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between a treeset and hashset in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What is difference between java 2 and java 5?

They are different versions. Java 5 is newer than Java 2. Think of it like the difference between the Playstation 1 and the Playstation 3.


Major difference between c and java?

Java is object oriented, C is not...


What is the difference between connectivity in java?

kamina


What is difference between connectivity in java?

kamina


What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


Difference between recordset and resultset in java?

Rowset


What is the difference between JAD and JAR?

JAD-Java Application Description JAR-Java archive


Difference between HashSet and Linkedhash set in java?

The only difference is that the LinkedHashSet maintains the order of the items added to the Set. It does this by maintaining a doubly linked list containing the hash and the original order of the items. According to Sun, the LinkedHashSet should run nearly as fast as the HashSet.LinkedHashSet A LinkedHashSet is an ordered version of HashSet thatmaintains a doubly-linked List across all elements. Use this class instead of HashSetwhen you care about the iteration order. When you iterate through a HashSet theorder is unpredictable, while a LinkedHashSet lets you iterate through the elementsin the order in which they were inserted.HashSet A HashSet is an unsorted, unordered Set. It uses the hashcodeof the object being inserted, so the more efficient your hashCode() implementationthe better access performance you'll get. Use this class when you want a collectionwith no duplicates and you don't care about order when you iterate through it.


What is hashing in Java?

Creating a Integer out of an object is called hashing. Hashing is commonly used in HashTable, HashMaps and HashSet. For instance you have Alex John Peter Hashing on each value would generate something like 123455 Alex 123344 John 123987 Peter when put in hashtable or hashset would be quicker to find each piece of information. There are many algorithms available with java to get the hash of an object.


What is the difference between java and object oriented programming?

java is a programming language/platform that embodies object oriented programming concepts. The question of what is the difference is like asking what is the difference between cars and a Volvo.


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


What difference between InputStreamReader and DatainputStream in java?

Both are same