In this example, We will discuss about how to get the head set from java TreeSet object.
Get Head Set from Java TreeSet example
In this example, We will discuss about how to get the head set from
java TreeSet object. the head set can be obtained by giving specific
values using headSet method of Java TreeSet class. To get the head set we use SortedSet headSet(Object Element) method. This method returns a view of the portion of
TreeSet whose elements are Less than the specified Element.
The Example is as follows-
HeadSetOfTreeSetExample.java
package devmanuals.com; import java.util.SortedSet; import java.util.TreeSet; public class HeadSetOfTreeSetExample { public static void main(String args[]) { TreeSet oTreeSet = new TreeSet(); oTreeSet.add("C"); oTreeSet.add("A"); oTreeSet.add("B"); oTreeSet.add("E"); oTreeSet.add("F"); oTreeSet.add("D"); System.out.println("The elements are: " + oTreeSet); SortedSet Osort = oTreeSet.headSet("D"); System.out.println("The headset of TreeSet is: " + Osort); } }
Output:-
The elements are: [A, B, C, D, E, F]
The headset of TreeSet is: [A, B, C] |
[ 0 ] Comments