[英]Peek at next element in a Tree set using an iterator
I was wondering if there was a way to accomplish this, or if there are alternative data structures. 我想知道是否有办法实现这一目标,或者是否有其他数据结构。 It has to be sorted, without duplicates, and have an iterator.
它必须排序,没有重复,并有一个迭代器。
TreeSet
has an iterator
, is sorted, won't have duplicates, and is able to see the next higher element by using higher
. TreeSet
有一个iterator
,被排序,不会有重复,并且能够通过使用higher
元素来查看下一个更高的元素。
For example: 例如:
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));
}
The output is: 输出是:
current: 1 next: 3
current: 3 next: 4
current: 4 next: null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.