Take a look at the next item in the tree set using an iterator

I was wondering if there is a way to do this, or if there are alternative data structures. It should be sorted without duplicates and have an iterator.

+4
source share
1 answer

TreeSet has an iterator , is sorted, there will be no duplicates, and can see the next higher element using higher .

For instance:

 TreeSet<Integer> ts = new TreeSet<Integer>(); ts.add(1); ts.add(4); ts.add(4); ts.add(3); for (Integer i : ts) { System.out.println("current: " + i + " next: " + ts.higher(i)); } 

Output:

 current: 1 next: 3 current: 3 next: 4 current: 4 next: null 
+12
source

All Articles