A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/util/treeset_headset.htm below:

java TreeSet headSet() Method

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 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 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 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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4