If we run above prog we will get Class Cast Exception because by default Tree Set uses comparable interface by using it we can compare same type of elements but not different type elements, here we are using different type elements i.e some are String type and some are Integer type , and so we get ClassCastException.

Output:

Case:

If we pass any element to head set and if passed element is not available in the set . then the passed element is same type of the existing element then JRE checks if that element is available in the set where its position will come. S

For example we pass ‘190’ as input to head set in the above example i.e.Set1.headset(190);

In above tree set example ‘190’ is not there if it is there in set that place will come after ‘80’ . so we will get o/p of all the elements before to that position i.e[10, 25, 30, 40, 62, 70, 90].Set1.headset(-10);

For example if we pass ‘-10’ has head set. ‘-10’ is not available in the list then JRE looks where its position will come but before that if there are no elements before to that position then we will get output as empty.

Case2:

If we pass different element then we will get class cast exception.Example set1.headset(“abc”);

tailSet( ):

In case tailset( ) we pass ‘190’ as input to tailset( ) but there are no elements after this value so that we will get output as empty.

Output:

Elements in set are:[10, 25, 30, 40, 70]
The head set elements by passing ‘190’ are:[10, 25, 30, 40, 70]
The head set elements by passing ‘-10’ are:[]
The head set elements by passing ‘190’ are:[]
The head set elements by passing ‘-10’ are:[10, 25, 30, 40, 70]

subSet( ):

The subSet( ) method will take inputs as two object types.In this first object value is included and second object type value is excluded and prints remaining values as the output.

Syntax:

public sortedset headset(object obj);

Note

The starting element should be less than the ending element otherwise we will get Illegal argument exception like below

About Java Padho

Einstein said, "It is the supreme art of the teacher to awaken joy in creative expression and knowledge." Great teachers are scarce, as they hold nothing back and give everything away. Javapadho is the brainchild of such an extraordinary teacher, i.e. Mr. Praveen Kumar Chandaliya, the founder of SDJ Infosoft.