I have this map:
Map<Integer,List<EventiPerGiorno>> mapEventi=new HashMap<Integer,List<EventiPerGiorno>>();
where EventiPerGiorno is a Comparable object. How can I get a sorted list from the map?
I tried with
Collection<List<EventiPerGiorno>> collection=mapEventi.values()
Comparable.sort(collection);
But Comparable.sort() doesn't like the list as comparable. Is there a ComparableList?
EDIT
this is the comparable method...
public class EventiPerGiorno implements Comparable<EventiPerGiorno>{
@Override
public int compareTo(EventiPerGiorno o) {
return this.getPrimoSpettacolo().compareTo(o.getPrimoSpettacolo());
}
}
Java Collections
don't have any order associated with them. You could turn the Collection
into a List
first and then sort it.
Collection<List<EventiPerGiorno>> collection = mapEventi.values()
YourComparableList<List<EventiPerGiorno>> list = new YourComparableList(collection);
Collections.sort(list);
For this, you will need to create some sort of List
that implements Comparable
. See How do I correctly implement Comparable for List in this instance? for an example.
Note that this is sorting the objects of type List<EventiPerGiorno>
, not the objects of type EventiPerGiorno
. If you are interested in sorting the latter, you might want this instead:
ArrayList<EventiPerGiorno> bigList = new ArrayList<EventiPerGiorno>();
for (List<EventiPerGiorno> list : mapEventi.values()) {
bigList.addAll(list);
}
Collections.sort(bigList);