按对象的属性对Map进行排序

时间:2021-08-10 19:17:01

Disclaimer : I have already once posted such a question and it was marked duplicate. Please try and help me. I have gone through all previous methods on * and none was of any help. All the methods mentioned to sort Map(Key,Values) didn't work in my case as I have one step further i.e. retrieving the attribute of the value. This time, I tried to go with full detail.

免责声明:我已经发布了这样一个问题并且标记为重复。请尝试帮助我。我已经完成了*上所有以前的方法,没有任何帮助。所有提到的对Map(Key,Values)进行排序的方法在我的情况下都不起作用,因为我还有一步,即检索值的属性。这一次,我试着详细介绍。

I've a Map (String,Object) in Java and I want to sort it using one of the attributes of the Object.

我在Java中有一个Map(String,Object),我想使用Object的一个属性对其进行排序。

e.g. Suppose I have a class

例如假设我有一堂课

class Entry
{
    int id;
    String name;
    String address;
    //Rest of the code
}

Now, I created a map

现在,我创建了一张地图

 Map<String,Entry>

I want to sort the map by the attribute id of the class Entry (Entry.id)

我想按类Entry(Entry.id)的属性id对地图进行排序

Please help !

请帮忙 !

For example, I have three objects of Entry class

例如,我有三个Entry类对象

entry1 :
        id=1
        name="abc"
        address="india"
entry2 :
        id=2
        name="xyz"
        address="india"
entry3 :
        id=3
        name="pqr"
        address="india"

Now, I have the Map initially as follows :

现在,我最初的地图如下:

Key :   Value
first:  entry2
second: entry3
third : entry1

After sorting, it should be like

排序后,应该是这样的

Key :   Value
third : entry1
first:  entry2
second: entry3

3 个解决方案

#1


1  

You can easily accomplish the task with the stream API:

您可以使用流API轻松完成任务:

Map<String, Entry> resultSet = myMap.entrySet()
                .stream()
                .sorted(Comparator.comparingInt(e -> e.getValue().getId()))
                .collect(Collectors.toMap(Map.Entry::getKey,
                        Map.Entry::getValue,
                        (left, right) -> left,
                        LinkedHashMap::new));

#2


1  

Your requirement is typically a symptom of bad data-structure usage.

您的要求通常是数据结构使用不良的症状。

If you wanted the map to be sorted by an attribute of key objects, you would just write a custom comparator, but since you want to sort by values, it's a bit more complicated.

如果您希望按键对象的属性对映射进行排序,您只需编写自定义比较器,但由于您希望按值排序,因此它会更复杂一些。

Try understanding answers to this question: Sort a Map<Key, Value> by values. And then try using a custom comparator in combination.

尝试理解这个问题的答案:按值对地图 <键,值> 进行排序。然后尝试组合使用自定义比较器。

#3


-3  

You can’t sort a map.

您无法对地图进行排序。

You can have a map that keeps its entries sorted, or you can sort a List<Map.Entry>.

您可以使用映射来保持其条目的排序,也可以对List 进行排序。

Try this:

Map<String, Entry> map; // your map
Map<String, Entry> sorted = new TreeMap<>(Comparator.comparing(s -> map.get(s).getId());
sorted.putAll(map);

#1


1  

You can easily accomplish the task with the stream API:

您可以使用流API轻松完成任务:

Map<String, Entry> resultSet = myMap.entrySet()
                .stream()
                .sorted(Comparator.comparingInt(e -> e.getValue().getId()))
                .collect(Collectors.toMap(Map.Entry::getKey,
                        Map.Entry::getValue,
                        (left, right) -> left,
                        LinkedHashMap::new));

#2


1  

Your requirement is typically a symptom of bad data-structure usage.

您的要求通常是数据结构使用不良的症状。

If you wanted the map to be sorted by an attribute of key objects, you would just write a custom comparator, but since you want to sort by values, it's a bit more complicated.

如果您希望按键对象的属性对映射进行排序,您只需编写自定义比较器,但由于您希望按值排序,因此它会更复杂一些。

Try understanding answers to this question: Sort a Map<Key, Value> by values. And then try using a custom comparator in combination.

尝试理解这个问题的答案:按值对地图 <键,值> 进行排序。然后尝试组合使用自定义比较器。

#3


-3  

You can’t sort a map.

您无法对地图进行排序。

You can have a map that keeps its entries sorted, or you can sort a List<Map.Entry>.

您可以使用映射来保持其条目的排序,也可以对List 进行排序。

Try this:

Map<String, Entry> map; // your map
Map<String, Entry> sorted = new TreeMap<>(Comparator.comparing(s -> map.get(s).getId());
sorted.putAll(map);