Assume an application producing a number of HashMap<String, MyClass>
data structures, each containing dozens to hundreds of Comparable
objects of type MyClass
, which need to end up in a single and sorted Collection
.
Two possible implementations of this functionality return a SortedSet or a sorted List, as follows:
public static Set<MyClass> getSortedSet(HashMap<String, MyClass>... allMaps)
{
SortedSet<MyClass> set = new TreeSet<MyClass>();
Collection<MyClass> c;
for (HashMap<String, MyClass> map:allMaps)
{
c = map.values();
set.addAll(c);
}
return set;
}
public static List<MyClass> getSortedList(HashMap<String, MyClass>... allMaps)
{
List<MyClass> list = new ArrayList<MyClass>();
Collection<MyClass> c;
for (HashMap<String, MyClass> map:allMaps)
{
c = map.values();
list.addAll(c);
}
Collections.sort(list);
return list;
}
Would there be any clear performance advantage to any of the above 2 methods?
Is there a faster way of implementing the same functionality?