java TreeSet headSet() Method



Description

The headSet(E toElement) method is used to return a view of the portion of this set whose elements are strictly less than toElement(input).The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.

Declaration

Following is the declaration for java.util.TreeSet.headSet() method.

public SortedSet<E> headSet(E toElement)

Parameters

toElement − This is the high endpoint (exclusive) of the returned set.

Return Value

The method call returns a view of the portion of this set whose elements are strictly less than toElement.

Exception

  • ClassCastException − This exception is thrown if 'toElement' cannot be compared to elements currently in the set.

  • NullPointerException − This exception is thrown if 'toElement' is null and this set uses natural ordering, or its comparator does not permit null elements.

  • IllegalArgumentException − This exception is thrown if this set itself has a restricted range, and 'toElement' lies outside the bounds of the range.

java TreeSet headSet(E toElement,boolean inclusive) Method

Description

The headSet(E toElement,boolean inclusive) method is used to return a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.

Declaration

Following is the declaration for java.util.TreeSet.headSet() method.

public NavigableSet<E> headSet(E toElement,boolean inclusive)

Parameters

  • toElement − This is the high endpoint of the returned set.

  • inclusive − This is true if the high endpoint is to be included in the returned view.

Return Value

The method call returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

Exception

  • ClassCastException − This is thrown if toElement is not compatible with this set's comparator.

  • NullPointerException − This is thrown if toElement is null and this set uses natural ordering, or its comparator does not permit null elements.

  • IllegalArgumentException − This is thrown if this set itself has a restricted range, and toElement lies outside the bounds of the range.

Getting Sub Set from the TreeSet of Integer Example

The following example shows the usage of Java TreeSet headSet(K element) method to get the portion of this set whose elements are strictly less than element. We've created two TreeSet objects of Integer. Then few entries are added using add() method to the set. Using headset(K element) method, we've received the subset and printed it.

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating TreeSet 
      TreeSet <Integer>tree = new TreeSet<>();
      TreeSet <Integer>treeheadset = new TreeSet<>();

      // adding in the tree
      tree.add(12);
      tree.add(13);
      tree.add(14);
      tree.add(15);
      tree.add(16);
      tree.add(17);

      // getting values less than 15
      treeheadset = (TreeSet)tree.headSet(15);  
    
      // displaying the Tree set data
      System.out.println("Tree set data in headset: " + treeheadset);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Tree set data in headset: [12, 13, 14]

Getting Sub Set from the TreeSet of String Example

The following example shows the usage of Java TreeSet headSet(K element) method to get the portion of this set whose elements are strictly less than element. We've created two TreeSet objects of String. Then few entries are added using add() method to the set. Using headset(K element) method, we've received the subset and printed it.

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating TreeSet 
      TreeSet <String>tree = new TreeSet<>();
      TreeSet <String>treeheadset = new TreeSet<>();

      // adding in the tree
      tree.add("A");
      tree.add("B");
      tree.add("C");
      tree.add("D");
      tree.add("E");
      tree.add("F");

      // getting values less than D
      treeheadset = (TreeSet)tree.headSet("D");  
    
      // displaying the Tree set data
      System.out.println("Tree set data in headset: " + treeheadset);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Tree set data in headset: [A, B, C]

Getting Sub Set from the TreeSet of Object Example

The following example shows the usage of Java TreeSet headSet(K element, boolean inclusive) method to get the portion of this set whose elements are strictly less than (or equal to if inclusive is true) element. We've created two TreeSet objects of String. Then few entries are added using add() method to the set. Using headset(K element) method, we've received the subset and printed it.

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating TreeSet 
      TreeSet <String>tree = new TreeSet<>();
      TreeSet <String>treeheadset = new TreeSet<>();

      // adding in the tree
      tree.add("A");
      tree.add("B");
      tree.add("C");
      tree.add("D");
      tree.add("E");
      tree.add("F");

      // getting values less than D
      treeheadset = (TreeSet)tree.headSet("D", true);  
    
      // displaying the Tree set data
      System.out.println("Tree set data in headset: " + treeheadset);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Tree set data in headset: [A, B, C, D]
java_util_treeset.htm
Advertisements