【发布时间】:2016-01-24 19:42:42
【问题描述】:
我在 Java 中使用 TreeSet 来保存整数列表:
TreeSet<Integer> num = new TreeSet<Integer>();
由于我的应用程序有多个线程,我希望能够同步地访问它。我正在使用Collections.synchronizedSortedSet(num);,这会返回一个Collections$SortedSet。我的问题是这不能转换为TreeSet - 它只能转换为SortedSet。我无法更改 num 对象的类,因此它必须转换为 TreeSet。
有谁知道我如何将Collections$SortedSet 转换为TreeSet,或者我可以通过其他方法来同步这个TreeSet?
【问题讨论】:
标签: java collections synchronization treeset