简体   繁体   English

Java订购集合

[英]Java order a collection

I have this map: 我有这张地图:

Map<Integer,List<EventiPerGiorno>> mapEventi=new HashMap<Integer,List<EventiPerGiorno>>();

where EventiPerGiorno is a Comparable object. 其中EventiPerGiorno是一个Comparable对象。 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. 但是Comparable.sort()不喜欢列表具有可比性。 Is there a ComparableList? 有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. Java Collections没有任何关联的顺序。 You could turn the Collection into a List first and then sort it. 您可以先将“ Collection转换为“ List ,然后对其进行排序。

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 . 为此,您将需要创建某种实现ComparableList See How do I correctly implement Comparable for List in this instance? 请参阅如何在这种情况下正确实现List的Comparable? for an example. 举个例子

Note that this is sorting the objects of type List<EventiPerGiorno> , not the objects of type EventiPerGiorno . 请注意,这是对List<EventiPerGiorno>类型的对象进行排序,而不是对EventiPerGiorno类型的对象进行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);

You are attempting to sort a List of Lists. 您正在尝试对列表列表进行排序。 List doesn't implement Comparable. 列表未实现可比性。 You'll have to create your own Comparator instance. 您必须创建自己的Comparator实例。

    Map<String, List<EventiPerGiorno>> map = new HashMap<String, List<EventiPerGiorno>>();

    List<List<EventiPerGiorno>> lists = new ArrayList(map.values());

    Collections.sort(lists, new Comparator<List<EventiPerGiorno>>() {
        @Override
        public int compare(List<EventiPerGiorno> o1, List<EventiPerGiorno> o2) {
            // ??? This is up to you.
            return 0;
        }
    });

This will sort every list in your map: 这将对地图中的每个列表进行排序:

for (List<EventiPerGiorno> list : mapEventi.values()) {
    Collections.sort(list);
}

Or if you perhaps want to retrieve a single sorted list without modifying the lists in the map: 或者,如果您想检索单个排序列表而不修改地图中的列表,请执行以下操作:

int someKey = ...;
List<EventiPerGiorno> list = new ArrayList<>(mapEventi.get(someKey));
Collections.sort(list);
return list;

You would need to extend List and make it implement Comparable. 您将需要扩展List并使其实现Comparable。 There is no default natural ordering that can be used to compare multiple Lists. 没有默认的自然顺序可用于比较多个列表。

The collections framework wouldnt know if you wanted to sort the list by number of items, number of duplicates, or values within the list. 集合框架不会知道您是否要按项目数,重复项数或列表中的值对列表进行排序。

Then to sort you use: 然后排序使用:

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List%29 http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM