treeset example java

Example 1: java treeset

import java.util.*;

// Declare the variable using the interface of the object for flexibility.
// Non-primative data types only.
Set<String> movies = new TreeSet<String>();

movies.add(E);
// TreeSet will be sorted by element.
// Work with any comparable object.

movies.add("The Shining");
movies.add("Dawn of the Dead");
movies.add("Zombieland");

Example 2: treeset

TreeSet: Can contain only unique values and it is sorted in ascending order
TreeMap: Can contain only unique keys and keys are sorted in ascending order.

Tags:

Java Example