假设一个应用程序产生了许多HashMap< String,MyClass>数据结构,每个包含数十到数百个MyClass类型的可比较对象,这些对象最终需要在一个单独的排序集合中.
此功能的两种可能实现返回SortedSet或排序列表,如下所示:
public static Set getSortedSet(HashMap... allMaps)
{
SortedSet set = new TreeSet();
Collection c;
for (HashMap map:allMaps)
{
c = map.values();
set.addAll(c);
}
return set;
}
public static List getSortedList(HashMap... allMaps)
{
List list = new ArrayList();
Collection c;
for (HashMap map:allMaps)
{
c = map.values();
list.addAll(c);
}
Collections.sort(list);
return list;
}
上述两种方法中是否有明显的性能优势?
有没有更快的方法来实现相同的功能?