Java TreeSet descendingSet() Method Example - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Thursday, December 15, 2016

Java TreeSet descendingSet() Method Example

What is Java TreeSet

TreeSet creates a collection that uses a tree for storage. Objects are stored in sorted, ascending order according to the natural order. Optionally, you can construct a TreeSet with a constructor that lets you give the collection your own rules for what the order should be (rather than relying on the ordering defined by the elements' class) by using a Comparable or Comparator.

Description of descendingSet()

The descendingSet() method returns a reverse order view of the elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. If either set is modified while an iteration over either set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.

Declaration

Following is the declaration for java.util.TreeSet.descendingSet() method.

public NavigableSet<E> descendingSet()
Parameters : NA.
Return value : Returns a reverse order view of the elements contained in this set.
Exception : NA.

Simple example of TreeSet.descendingSet

The following example shows the usage of java.util.TreeSet.descendingSet() method. This method available in java since 1.6.

package com.walking.techie;

import java.util.TreeSet;

public class TreeSetDescendingSet {

 public static void main(String[] args) {
  // create an empty tree set
  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  treeSet.add(23);
  treeSet.add(34);
  treeSet.add(2);
  treeSet.add(21);
  treeSet.add(45);
  treeSet.add(15);

  // printing the elements of tree set using for-each
  System.out.println("***Elements of the treeSet***");
  for (Integer val : treeSet) {
   System.out.println(val);
  }

  // Lets reverse the elements of the tree set
  TreeSet<Integer> reverseTreeSet = (TreeSet<Integer>) treeSet.descendingSet();

  // printing the elements of the reverseTreeSet
  System.out.println("***Elements of the reverseTreeSet***");
  for (Integer val : reverseTreeSet) {
   System.out.println(val);
  }
 }
}

Output of above program is shown below:

***Elements of the treeSet***
2
15
21
23
34
45
***Elements of the reverseTreeSet***
45
34
23
21
15
2

java.util.TreeSet.descendingSet() with ConcurrentModificationException

The descendingSet() method returns a reverse order view of elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. While iterating over set if either set is modified then ConcurrentModificationException. In below program, you can see while iterating over set using for-each, any modification in either set will throw ConcurrentModificationException.

You can also see the code using the TreeSet's iterator() method by uncommenting in below code and comment for-each part. Once you create an iterator over either set, any modification on either set while iterating over set will throw ConcurrentModificationException.

package com.walking.techie;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDescendingSetException {

 public static void main(String[] args) {
  // create an empty tree set
  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  treeSet.add(23);
  treeSet.add(34);
  treeSet.add(2);
  treeSet.add(21);
  treeSet.add(45);
  treeSet.add(15);

  TreeSet<Integer> reverseTreeSet = (TreeSet<Integer>) treeSet.descendingSet();

  // printing the elements of tree set
  System.out.println("***Elements of the treeSet***");
  System.out.println(treeSet);
  System.out.println("***Elements of the reverseTreeSet***");
  System.out.println(reverseTreeSet);

  for (Integer val : treeSet) {
   if (val == 21) {
    reverseTreeSet.add(30);// ConcurrentModificationException
   }
  }

  // create an iterator of reverse tree set
  /*Iterator<Integer> iterator = reverseTreeSet.iterator();
  while (iterator.hasNext()) {
   Integer val = iterator.next();
   if (val == 21) {
    treeSet.add(40); // ConcurrentModificationException
   }
  }*/
 }
}

Output of above program is shown below:

***Elements of the treeSet***
[2, 15, 21, 23, 34, 45]
***Elements of the reverseTreeSet***
[45, 34, 23, 21, 15, 2]
Exception in thread "main" java.util.ConcurrentModificationException
 at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1211)
 at java.util.TreeMap$KeyIterator.next(TreeMap.java:1265)
 at com.walking.techie.TreeSetDescendingSetException.main(TreeSetDescendingSetException.java:26)

No comments :

Post a Comment