Java TreeSet descendingIterator method program - Walking Techie

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

Wednesday, December 14, 2016

Java TreeSet descendingIterator method program

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.

Print elements of the TreeSet in reverse order using descendingIterator

descendingIterator method belongs to TreeSet class. This method returns an iterator over the elements in this set in descending order. This method available in java since 1.6. Iterator returned by TreeSet's descendingIterator method is not fail-fast.

Here we will see the program to print the tree set in reverse dictionary order of string elements.

package com.walking.techie;

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

public class TreeSetDescendingIterator {

 public static void main(String[] args) {
  TreeSet<String> phones = new TreeSet<String>();
  phones.add("Nokia");
  phones.add("Samsung");
  phones.add("MI");
  phones.add("Zenfone");
  System.out.println("Set of phone in ascending order " + phones);

  // iterator of this set in descending order.
  Iterator<String> iterator = phones.descendingIterator();
  System.out.print("Reverse dictionary order=[");
  while (iterator.hasNext()) {
   String phoneName = iterator.next();
   System.out.print(phoneName + " ");
  }
  System.out.println("]");

  // Iterator returned by descendingIterator method
  // does not throw ConcurrentModificationException
  iterator = phones.descendingIterator();
  System.out.print("Reverse dictionary order=[");
  while (iterator.hasNext()) {
   String phoneName = iterator.next();
   System.out.print(phoneName + " ");
   if (phoneName.compareTo("MI") == 0) {
    phones.add("Motorola");// not throw
      // ConcurrentModificationException
   }

  }
  System.out.println("]");
  System.out.println("element of phones after add method "
        + "call on tree set phones  " + phones);

  // Iterator returned by TreeSet's iterator() method are fail-fast,
  // throw ConcurrentModificationException when set is modified
  // after the iterator is created, in any way except through the
  // iterator's own remove() method.

  iterator = phones.iterator();
  System.out.print("TreeSet iterator: elements of phones[");
  while (iterator.hasNext()) {
   String phoneName = iterator.next();
   System.out.print(phoneName + " ");
   if (phoneName.compareTo("Nokia") == 0) {
    System.out.println();
    phones.add("HTC");
    // throw ConcurrentModificationException
   }
  }
 }
}

Output of above program is shown below:

Set of phone in ascending order [MI, Nokia, Samsung, Zenfone]
Reverse dictionary order=[Zenfone Samsung Nokia MI ]
Reverse dictionary order=[Zenfone Samsung Nokia MI ]
element of phones after add method call on tree set phones  [MI, Motorola, Nokia, Samsung, Zenfone]
TreeSet iterator: elements of phones[MI Motorola Nokia
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.TreeSetDescendingIterator.main(TreeSetDescendingIterator.java:50)

No comments :

Post a Comment