如果我有这样的 Map
:
HashMap<Integer, ComparableObject> map;
我想得到一个集合的值排序使用自然顺序,哪种方法是最快的?
创建一个可排序集合(如 ArrayList
)的实例,添加值,然后对它进行排序:
List<ComparableObject> sortedCollection = new ArrayList<ComparableObject>(map.values());
Collections.sort(sortedCollection);
创建一个类似于 TreeSet
的有序集合的实例,然后添加值:
Set<ComparableObject> sortedCollection = new TreeSet<ComparableObject>(map.values());
注意,不会修改结果集合,因此排序只需要进行一次。