Java TreeSet clear() method

The clear() method of TreeSet class is used to clear elements from the set and the set becomes empty.

Syntax:

Example 1

Output:

TreeSet: [ 1, 2, 5, 8, 10 ]
TreeSet after clear: []

Example 2

Output:

TreeSet: [ A, B, a, b ] [ 1, 3, 6, 9 ]
TreeSet after clear: [ A, B, a, b ] [ ]