Set<Integer> treeSet = new TreeSet<Integer>(); |
Set interface does not have ceiling() method |
TreeSet<Integer> treeSet = new TreeSet<Integer>(); |
if we use Set interface rather than TreeSet class, we cannot use ceiling() method |
treeSet.add(10); |
adds the element to the treeset |
treeSet2.addAll(treeSet); |
System.out.println("treeSet2 after adding elements" + treeSet2); |
print treeeSet |
treeSet.remove(20); |
removes the specific element from the treeset |
for (Integer item : treeSet) { System.out.println(item); } |
Iterating over tree set items |
Iterator<Integer> iterator = treeSet.iterator(); |
Iterating using iterator |
Iterator<Integer> descendingIterator = treeSet.descendingIterator(); |
returns an iterator over the elements in this set in descending order. |
int value1 = treeSet.ceiling(25); |
returns the least element in this set greater than or equal to the given element |
int value2 = treeSet.floor(25); |
returns the greatest element in this set less than or equal to the given element |
Set<Integer> descendingSet = treeSet.descendingSet(); |
returns a reverse order view of the elements contained in this set. |
Set<Integer> headSet = treeSet.headSet(30); |
returns a view of the portion of this set whose elements are strictly less than toElement. |
Set<Integer> headSet2 = treeSet.headSet(30, true); |
returns a view of the portion of this set whose elements are less than or equal to toElement. |
Set<Integer> tailSet = treeSet.tailSet(30); |
returns a view of the portion of this set whose elements are greater than or equal to fromElement. |
Set<Integer> tailSet2 = treeSet.tailSet(30, false); |
returns a view of the portion of this set whose elements are strictly greater than fromElement. |
Set<Integer> subSet = treeSet.subSet(20, 40); |
returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. |
Set<Integer> subSet2 = treeSet.subSet(20, true, 40, true); |
returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, inclusive. |
Integer pollFirst = treeSet.pollFirst(); |
retrieves and removes the first (lowest) element, or returns null if this set is empty. |
Integer pollLast = treeSet.pollLast(); |
retrieves and removes the last (highest) element, or returns null if this set is empty. |
boolean remove = treeSet.remove(20); |
returns true if this set contained the specified element |
boolean isEmpty = treeSet.isEmpty(); |
returns true if this set contains no elements. |
int size = treeSet.size(); |
returns the number of elements in this set (its cardinality). |
boolean contains = treeSet.contains(20); |
returns true if this set contains the specified element. |
treeSet.clear(); |