Example of headSet() method of navigableSet in java.
Example of headSet() method of navigableSet in java.
The headSet() in a method of java navigableSet. It returns a set of element. It holds all the element, that is less than or equal to the given element.
Code:
ExampleOfHeadSet.java
import java.util.Iterator; import java.util.NavigableSet; import java.util.SortedSet; import java.util.TreeSet; public class ExampleOfHeadSet { public static void main(String[] args) { NavigableSet obSet = new TreeSet(); obSet.add(2); obSet.add(3); obSet.add(4); obSet.add(5); obSet.add(6); obSet.add(7); obSet.add(8); Iterator obIterator = obSet.iterator(); System.out.println("Elements of navigableset."); while (obIterator.hasNext()) { System.out.print(obIterator.next() + " "); } SortedSet oSortedSet = obSet.headSet(6); Iterator headSetIterator = oSortedSet.iterator(); System.out.println("\nElement in another set."); while (headSetIterator.hasNext()) { System.out.print(headSetIterator.next() + " "); } } }Output:
Elements of navigableset. 2 3 4 5 6 7 8 Element in another set. 2 3 4 5 |
Download this code.
[ 0 ] Comments