info@meritcampus.com    +91-85006-22255
...
`

Collections NavigableSet
What will be the output of the following program?
`import java.util.*;public class Order {    public static void main(String[] args) {        NavigableSet<Integer> number = new TreeSet<Integer>();        number.add(0);    number.add(10);        number.add(6);    number.add(15);        number.add(-4);    number.add(10);        number.add(8);    number.add(16);        number.add(594);    number.add(154);        number.add(+2);        Iterator num = number.descendingIterator();        String result = "";        while (num.hasNext()) {            result += num.next() + ", ";        }        System.out.println(extracted(result));    }    private static String extracted(String result) {        return result.substring(0, result.length() - 2);    }}`

 A. 594, 154, 16, 15, 10, 8, 6, 2, 0, -4 B. 594, 154, 16, 15, 10, 8, 6, 2, 0, - C. -4, 0, 2, 6, 8, 10, 15, 16, 154, 594 D. -4, 0, 2, 6, 8, 10, 15, 16, 154, 59 E. Some other output F. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

Posted by Gopinath Manchikanti    2014-02-24 08:56:25

answer is B .descendingIterator() method is used to return an iterator over the elements in this set in descending order.

Posted by Kumarcs Cskc    2014-02-24 09:37:30

Answer is A. A NavigableSet is a SortedSet meaning it maintains the ordering of its elements and it can be accessed in the ascending or descending order. TreeSet automatically sorts its elements. Here we are using descendingIterator() method to get a descending view of the set. Then, "result" is passed to extracted() method which extracts the sub string from 0 to {(length - 2) - 1} i.e., 0 to 35 here (Total string length is 38).

Posted by Shaileshwar Sharma    2014-02-24 09:41:47

Congratulations Shaileshwar Sharma. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-25 04:34:11