Java TreeSet subSet() Method

The subSet() method of TreeSet class is used to get the set elements which are in the range from fromElement to toElement.

Syntax:

Parameter:

fromElement- the Minimum endpoint of the returned set

toElement- the Maximum endpoint of the returned set.

Return:

It returns the set which range from fromElement to toElement.

Throws:

ClassCastException: Throws exception when the range from fromElement to toElement cannot be compared to elements present in the set.

NullPointerException: Throws exception when fromElement to toElement is null.

IllegalArgumentException: Throws exception when fromElement is greater than toElement, or out of range.

Example 1

Output:

TreeSet: [4, 6, 9, 12, 18]
Elements of  TreeSet: [6, 9]

Example 2

Output:

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

Example 3

Output:

Elements in TreeSet: 
1  Shristee  III B.tech(C.S)
2  Shweta    I   B.tech(I.t)
3  Suma      II  Msc(I.t)
4  Dolly     V   MCA
Elements of  TreeSet: [2  Shweta    I   B.tech(I.t), 3  Suma      II  Msc(I.t)]

Java TreeSet subSet() Method

The subSet() method of TreeSet class is used to get the elements of the set which are in the range from fromElement to toElement. It returns the null set if fromElement and toElement are equal except fromInclusive and toInclusive both are true.

Syntax:

Parameter:

fromElement - the Minimum endpoint of the returned set

fromInclusive - it is true if the Minimum endpoint is to be included in the view that is to be returned

toElement - the Maximum endpoint of the returned set.

toInclusive - true if the Maximum endpoint is to be included in the view that is to be returned

Return:

It returns the set which range from fromElement to toElement.

Throws:

ClassCastException: Throws exception when the range from fromElement to toElement cannot be compared to elements present in the set.

NullPointerException: Throws exception when fromElement to toElement is null.

IllegalArgumentException: Throws exception when fromElement is greater than or equal to toElement is out of range.

Example 1

Output:

TreeSet: [2, 3, 4, 6, 9, 12, 13, 16, 18, 25]
Elements in TreeSet object 1[4, 6, 9, 12, 13]
Elements in TreeSet object 2[6, 9, 12, 13]
Elements in TreeSet object 3[4, 6, 9, 12, 13]
Elements in TreeSet object 4[6, 9, 12, 13]

Example 2

Output:

TreeSet: [1, 3, 6, 9, A, B, a, b]
Elements in TreeSet object 1[3, 6, 9, A, B, a]
Elements in TreeSet object 2[6, 9, A, B]
Elements in TreeSet object 3[3, 6, 9, A, B]
Elements in TreeSet object 4[6, 9, A, B, a]

Example 3

Output:

Elements in TreeSet: 
1 C Programming in C 200
2 C++ Programming in C++ 340
3 VB Programming in Visual Basic 300
4 Java Programming in JAVA 400
5 DS Data Structure 400
6 SAD System Analysis and Design  400
7 SE Software eEngineering 400
Elements in TreeSet object 1[2 C++ Programming in C++ 340, 3 VB Programming in Visual Basic 300,
 4 Java Programming in JAVA 400, 5 DS Data Structure 400, 6 SAD System Analysis and Design  400]
Elements in TreeSet object 2[3 VB Programming in Visual Basic 300, 4 Java Programming in JAVA 400, 
5 DS Data Structure 400]
Elements in TreeSet object 3[2 C++ Programming in C++ 340, 3 VB Programming in Visual Basic 300, 
4 Java Programming in JAVA 400, 5 DS Data Structure 400]
Elements in TreeSet object 4[3 VB Programming in Visual Basic 300, 4 Java Programming in JAVA 400, 
5 DS Data Structure 400, 6 SAD System Analysis and Design  400]