TreeSet in Java

In Java Tree Set is a data structure available in the java collection framework.

What is Tree Set

A TreeSet is a implementation of NavigableSet in java, which means TreeSet is a sorted, navigable set of element. TreeSet data structure is based on red black tree data structure.

Properties of TreeSet

  • Duplicate objects are not allowed
  • Insertion order not preserved
  • Heterogeneous objects are not allowed
  • Null insertion is possible but at most one.
  • All elements in TreeSet are sorted in natural order or according to some specified comparator.

TreeSet Java Example

import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet = new TreeSet<>();
        // Adding elements in the TreeSet
        // printing the TreeSet element(sorted element)
        // removing elements from TreeSet
       // updated TreeSet
TreeSet Java Example

Similar Java Tutorials

3 thoughts on “TreeSet in Java”

Leave a Comment