如何获得 SortedSet 的独占尾集?
我想获得 SortedSet 的唯一尾部集。我能想到的最短方法是:
private void exclusiveTailSet(SortedSet<String> s, String start) {
System.out.println(s); // [Five, Four, One, Six, Start, Three, Two]
SortedSet<String> t = s.tailSet(start);
System.out.println(t); // [Start, Three, Two]
Iterator<String> i = t.iterator();
i.next();
SortedSet<String> u = t.tailSet(i.next());
System.out.println(u); // [Three, Two]
}
tailSet 的 javadoc 建议要求从域中的下一个元素开始的子集(即对于调用s.tailSet(start+"\0");
),但是我实际上正在使用对象,因此创建它会产生更多的开销。
创建专属尾巴组的高效且干净的通用方法是什么?
I want to get an exclusive tail set of a SortedSet. The shortest method I can come up with is:
private void exclusiveTailSet(SortedSet<String> s, String start) {
System.out.println(s); // [Five, Four, One, Six, Start, Three, Two]
SortedSet<String> t = s.tailSet(start);
System.out.println(t); // [Start, Three, Two]
Iterator<String> i = t.iterator();
i.next();
SortedSet<String> u = t.tailSet(i.next());
System.out.println(u); // [Three, Two]
}
The javadoc for tailSet suggests asking for the subset starting from the next element in the domain (i.e. for Strings calling s.tailSet(start+"\0");
), however I'm actually working with objects such that it would be much more of an overhead to create it.
What is an efficient and clean general method to create an exclusive tail set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NavigableSet 接口是 SortedSet 的子实现。如果我正确理解你的问题,你可以使用 NavigableSet 的 tailset 方法,它能够包含或排除,具体取决于你提供的布尔值。
http://download.oracle.com/javase/6 /docs/api/java/util/NavigableSet.html
NavigableSet interface is a subimplementation of the SortedSet. If i understand your question correctly you can use NavigableSet's tailset method, which has the ability to be either inclusive or exclusive, depending on what boolean you provide.
http://download.oracle.com/javase/6/docs/api/java/util/NavigableSet.html
我没有看到比你描述的更好的方法。
不过,您可以通过使其成为通用方法来使其更加通用:
I don't see any better method than the one you describe.
You can make it slightly more general though, by making it a generic method:
这有帮助吗?
(我只是复制并粘贴您有问题的代码,并做了一些更改:
如果您有“start”元素作为参数,它也将是
is this helpful?
(I just copy and paste your codes in question, and did some change:
and if you have "start" element as parameter, it would also be
Guava 版本 11:
ps 还有一个针对 SortedSet 的 hack,以防您无法使用 NavigableSet。解决方案是为搜索模式添加一个特殊符号(start+“\0”)。这个加法符号将更改 hashCode,因此 SortedSet 的默认 tailset 实现将正常工作:
它将相当于 java.util.TreeSet#tailSet(E fromElement, boolean inclusive) 和 <第二个参数中的strong>false值。
上帝保佑最后一个 com.google.common.collect.ImmutableSortedSet(自版本 12 起)也有 NavigableSet 的实现。
Guava version 11:
p.s. There is also a hack for SortedSet in case you couldn't use NavigableSet. The solution is to add for the search pattern a special symbol (start+"\0"). And this addition symbol will change the hashCode and thus the default tailset implementation of a SortedSet will work fine:
It will be an equivalent of java.util.TreeSet#tailSet(E fromElement, boolean inclusive) with false value in a second parameter.
God bless that the last com.google.common.collect.ImmutableSortedSet (since version 12) has an implementation of NavigableSet also.