1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/* Get Head Set from Java TreeSet example This Java Example shows how to get the portion of TreeSet containing the values less than the specified value using headSet method of Java TreeSet class. */ import java.util.SortedSet; import java.util.TreeSet; public class GetHeadSetFromTreeSetExample { public static void main(String[] args) { //create TreeSet object TreeSet tSet = new TreeSet(); //add elements to TreeSet tSet.add("1"); tSet.add("3"); tSet.add("2"); tSet.add("5"); tSet.add("4"); /* To get a Head Set from Java TreeSet use, SortedSet headSet(Object fromElement) method of Java TreeSet class. This method returns the portion of TreeSet containing elements less than fromElement. Please note that, the SortedSet returned by this method is backed by the original TreeSet. So any changes made to SortedSet will be reflected back to original TreeSet. */ SortedSet sortedSet = tSet.headSet("3"); System.out.println("Head Set Contains : " + sortedSet); } } /* Output would be Head Set Contains : [1, 2] */ |
Add Comment