Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Java provides two important Set implementations in the Collections framework: HashSet
and TreeSet
. Both are used to store unique elements, but they differ in how elements are stored and accessed. In this tutorial, we'll explore the differences between HashSet
and TreeSet
and show examples of their usage.
HashSet
HashSet
is an unordered collection that implements the Set
interface. It uses a hash table for storing elements, providing fast insertion, deletion, and retrieval of elements. The main characteristics of HashSet
are:
Example of using a HashSet
:
import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> colors = new HashSet<>(); colors.add("Red"); colors.add("Blue"); colors.add("Green"); colors.add("Yellow"); colors.add("Red"); System.out.println("HashSet elements: " + colors); } }
In this example, even though we tried to add "Red" twice, the HashSet
will only store unique elements.
TreeSet
TreeSet
is a sorted collection that implements the NavigableSet
interface, which extends the SortedSet
interface. It uses a Red-Black Tree (a self-balancing binary search tree) for storing elements. The main characteristics of TreeSet
are:
Example of using a TreeSet
:
import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) { Set<String> colors = new TreeSet<>(); colors.add("Red"); colors.add("Blue"); colors.add("Green"); colors.add("Yellow"); colors.add("Red"); System.out.println("TreeSet elements: " + colors); } }
In this example, the TreeSet
stores the unique elements in a sorted order.
When to use HashSet or TreeSet?
Choose HashSet
when:
Choose TreeSet
when:
NavigableSet
and SortedSet
, like first()
, last()
, headSet()
, tailSet()
, etc.In this tutorial, we covered the basics of HashSet
and TreeSet
in Java, their differences, and when to use each implementation. Both HashSet
and TreeSet
are useful for storing unique elements, but their use cases differ depending on the performance and ordering requirements.
Java Set Interface and Implementations:
Set
interface represents an unordered collection of unique elements. HashSet
and TreeSet
are implementations.Set<String> hashSet = new HashSet<>(); Set<Integer> treeSet = new TreeSet<>();
Adding Elements to HashSet in Java:
HashSet
.Set<String> names = new HashSet<>(); names.add("Alice"); names.add("Bob");
Removing Elements from HashSet in Java:
HashSet
.names.remove("Alice");
Java HashSet Example Code:
HashSet
.Set<String> fruits = new HashSet<>(); fruits.add("Apple"); fruits.add("Banana");
Sorting Elements in TreeSet in Java:
TreeSet
are automatically sorted.Set<Integer> numbers = new TreeSet<>(); numbers.add(5); numbers.add(2);
Java TreeSet Example Code:
TreeSet
.Set<String> colors = new TreeSet<>(); colors.add("Red"); colors.add("Blue");
HashSet and TreeSet with Custom Objects in Java:
HashSet
and TreeSet
.Set<Person> personSet = new HashSet<>(); Set<Person> sortedPersonSet = new TreeSet<>(Comparator.comparing(Person::getLastName));
HashSet and TreeSet Iterator in Java:
Iterator<String> hashSetIterator = names.iterator(); while (hashSetIterator.hasNext()) { System.out.println(hashSetIterator.next()); }