0% found this document useful (0 votes)
10 views2 pages

Tree Set Demo

Uploaded by

roshan.prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Tree Set Demo

Uploaded by

roshan.prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

TreeSet;

class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(2);
numbers.add(5);
numbers.add(4);
numbers.add(6);
System.out.println("TreeSet: " + numbers);

// Returns the lowest element among those elements that are greater than
the specified element.
System.out.println("Using higher: " + numbers.higher(3));

// Returns the greatest element among those elements that are less than the
specified element.
System.out.println("Using lower: " + numbers.lower(4));

// Returns the lowest element among those elements that are greater than
the specified element.
//If the element passed exists in a tree set, it returns the element passed
as an argument.
System.out.println("Using ceiling: " + numbers.ceiling(4));

// Returns the greatest element among those elements that are less than the
specified element.
//If the element passed exists in a tree set, it returns the element passed
as an argument.
System.out.println("Using floor: " + numbers.floor(3));

// pollFirst() - returns and removes the first element from the set
System.out.println("Removed First Element: " + numbers.pollFirst());

// pollLast() - returns and removes the last element from the set
System.out.println("Removed Last Element: " + numbers.pollLast());

System.out.println("New TreeSet: " + numbers);

/*The headSet() method returns all the elements of a tree set before the specified
element (which is passed as an argument).
The booleanValue parameter is optional. Its default value is false.
If true is passed as a booleanValue, the method returns all the elements before the
specified element including the specified element.*/
System.out.println("Using headSet without boolean value: " +
numbers.headSet(5));

// Using headSet() with specified boolean value


System.out.println("Using headSet with boolean value: " +
numbers.headSet(5, true));

/* tailSet(element, booleanValue)
The tailSet() method returns all the elements of a tree set after the specified
element (which is passed as a parameter) including the specified element.
The booleanValue parameter is optional. Its default value is true.
If false is passed as a booleanValue, the method returns all the elements after the
specified element without including the specified element.
*/
System.out.println("Using tailSet without boolean value: " +
numbers.tailSet(4));
System.out.println("Using tailSet with boolean value: " +
numbers.tailSet(4, false));

/* The subSet() method returns all the elements between e1 and e2 including e1.

The bv1 and bv2 are optional parameters. The default value of bv1 is true, and the
default value of bv2 is false.

If false is passed as bv1, the method returns all the elements between e1 and e2
without including e1.

If true is passed as bv2, the method returns all the elements between e1 and e2,
including e1.*/

System.out.println("Using subSet without boolean value: " +


numbers.subSet(4, 6));
System.out.println("Using subSet with boolean value: " + numbers.subSet(4,
false, 6, true));
}
}

You might also like