我想得到一个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]
}
Run Code Online (Sandbox Code Playgroud)
tailSet的javadoc建议从域中的下一个元素开始请求子集(即对于字符串调用s.tailSet(start+"\0");),但是我实际上正在使用对象,这样创建它就会更多.
什么是创建独有尾部集的有效且干净的通用方法?
NavigableSet接口是SortedSet的子实现.如果我正确理解你的问题,你可以使用NavigableSet的tailset方法,它具有包容性或独占性,具体取决于你提供的布尔值.
NavigableSet<E> tailSet(E fromElement, boolean inclusive)
Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.
Run Code Online (Sandbox Code Playgroud)
http://download.oracle.com/javase/6/docs/api/java/util/NavigableSet.html
| 归档时间: |
|
| 查看次数: |
1836 次 |
| 最近记录: |