Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the NavigableSet.descendingIterator() method in Java?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The NavigableSet.desendingIterator() method is present in the NavigableSet interface in the java.util package. It is used to return an iterator over the elements present in the set, and is done so in descending order.

When we add the elements in the NavigableSet, they get stored in a sorted form. If the NavigableSet is of String type, the elements get stored in alphabetical order irrespective of string length.

Parameters

The NavigableSet.desendingIterator() method does not accept any parameters.

Return value

The NavigableSet.descendingIterator() method returns an iterator in descending order over the elements present in the set.

Code

Let’s have a look at the code below.

import java.util.NavigableSet;
import java.util.TreeSet;
import java.util.*;
class Main
{
public static void main(String[] args)
{
NavigableSet<Integer> s = new TreeSet<Integer>();
s.add(6);
s.add(8);
s.add(5);
s.add(3);
s.add(9);
s.add(10);
s.add(17);
Iterator<Integer> i = s.descendingIterator();
System.out.println("Values in descending order after using NavigableSet.descendingIterator() are: ");
while (i.hasNext())
System.out.println(i.next() + " ");
}
}
Using the NavigableSet.descendingIterator() method in Java

Explanation

  • In lines 1 to 3, we imported the required packages and classes.

  • In line 4, we made a Main class.

  • In line 6, we made a main() function.

  • In line 8, we created a TreeSet of Integer type. The NavigableSet is inherited from SortedSet which is actually inherited from TreeSet. As SortedSet and NavigableSet are interfaces, we cannot instantiate an object of them.

  • In lines 10 to 16, we added the elements into the NavigableSet by using the add() method.

  • In line 18, we used the NavigableSet.desendingIterator() method to create an iterator to iterate over elements in descending order.

  • In lines 21 and 22, we used a while loop to iterate over elements using the iterator and display each element’s value in descending order.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring