Java TreeSet add() method

The add() method of TreeSet class is used to add elements in the set. Add() method stores elements in increasing order. Duplicate elements are not allowed.

Syntax:

Parameter:

e - element to be added in the set

Return:

It returns true if set does not contain specified element.

Example 1

Output:

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

Example 2

Output:

TreeSet: [ A, B, a, b ]

Note: In the Example 2 sorting is done by ASCII value of Characters. As the ASCII value of "A" is 65 and ASCII value of "a" is 97.

Example 3

Output:

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

Example 4

Output:

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