如何对Map<String, List<CustomObject>>进行排序?

3

我已经为这个问题做了很多研究,但是我没有找到一种方法来对自定义对象列表的Map(Map<String, List<CustomObj>>)进行排序。排序依据是基于CustomObj属性(如SORT_BY_NAME,SORT_BY_DATE等)进行比较。

一个激励我的例子是:

  • 我有一个自定义对象: Person (具有名称、出生日期等属性);
  • 我有一个Person对象ListMap,如下所示:Map<String, List<Person>>。映射键用于其他目的;
  • 我想创建一个比较器和排序方法,根据Person对象的属性(名称、日期等)进行升序排序映射。

为简单起见,我报告了真实代码,但将其适应了一个简化的Person对象案例,因为它已经代表了实体的概念。

Person.java -> 自定义对象

public class Person {

     private String name;
     private Date dateOfBirth;
     ...

     // Empty and Full attrs Constructors
     ...

     // Getter and Setter
     ...

     // Comparator by name
     public static Comparator<Person> COMPARE_BY_NAME = Comparator.comparing(one -> one.name);
     // Comparator by date
     public static Comparator<Person> COMPARE_BY_DATE = Comparator.comparing(one -> one.dateOfBirth);

}

Sorter.java -> 排序器对象

public class Sorter {

     // List Comparator of Person by Date 
     public static final Comparator<? super List<Person>> COMPARATOR_BY_DATE = (Comparator<List<Person>>) (p1, p2) -> {
          for (Persontab person1: p1) {
              for (Person person2: p2) {
                  return Person.COMPARE_BY_DATE.compare(person1, person2);
              }
          }
          return 0;
     };

     // List Comparator of Person by Name
     public static final Comparator<? super List<Person>> COMPARATOR_BY_NAME = (Comparator<List<Person>>) (p1, p2) -> {
          for (Persontab person1: p1) {
              for (Person person2: p2) {
                  return Person.COMPARE_BY_NAME.compare(person1, person2);
              }
          }
          return 0;
     };

     // Sorting method
     public Map<String, List<Person>> sort(Map<String, List<Person>> map, Comparator<? super List<Person>> comparator) {
          return map.entrySet()
            .stream()
            .sorted(Map.Entry.comparingByValue(comparator))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
     }

}

Main.java -> 开始代码

public class MainApp {

     public static void main(String[] args) {

          Map<String, List<Person>> exampleMap = new HashMap<>();
          List<Person> personList = new ArrayList<>();
          personList.add(new Person("name1", new Date("2022-01-01")));              
          personList.add(new Person("name12", new Date("2022-01-05")));
          personList.add(new Person("name13", new Date("2022-01-03")));
          map.put("2022-01", personList);

          personList.clear();
          personList.add(new Person("name14", new Date("2021-02-01")));              
          personList.add(new Person("name3", new Date("2021-02-05")));
          personList.add(new Person("name4", new Date("2021-02-03")));
          map.put("2021-02", personList);

          Sorter sorter = new Sorter();

          // Example of sorting by date
          map = sorter.sort(exampleMap, Sorter.COMPARATOR_BY_DATE);
          // In this case the sorting works correctly, or rather it sorts the items by date as I expect
         
          // Example of sorting by name
          map = sorter.sort(exampleMap, Sorter.COMPARATOR_BY_NAME);
          // In this case, I don't think sorting works correctly. Sort each list of elements for each key in ascending order. But it doesn't sort the map elements.

          /* I expect to have the following map when sort by date:
             "2021-02": [
               Person("name14", new Date("2021-02-01")),
               Person("name4", new Date("2021-02-03")),
               Person("name3", new Date("2021-02-05"))
             ], 
             "2022-01": [
               Person("name14", new Date("2021-02-01")),
               Person("name13", new Date("2022-01-03")),
               Person("name12", new Date("2022-01-05"))
             ]
             
     }

}

5
注意:你无法对 HashMap 进行排序。这是它的基本属性之一,即它没有任何顺序。如果你需要一种排序方式,可以使用 TreeMap,它按键进行排序,或者使用 LinkedHashMap,它按插入顺序排序。 - Thomas
如果你想排序,可以使用entrySet()。例如:https://dev59.com/EF0b5IYBdhLWcg3wIeMw,但这并不会对支持HashMap进行排序。只是提供信息。 - JCompetence
意图是继续对列表中的所有元素进行排序。特别是,整个映射必须排序。- 那么你想要排序什么?列表?通过LinkedHashMap排序映射条目?还是两者都要排序? - Thomas
谢谢@Thomas。没错,意图是对两者进行排序:整个映射和其每个值(例如,按其列表中所有对象的日期对映射进行排序)。 - Giuseppe Mondelli
1
因此,如果您需要对列表进行排序,您需要首先这样做,然后对流中的元素进行排序,最后构造映射。根据列表是否应该原地排序(这也会反映在原始映射中)或者您想要创建一个副本,您可以使用map(list -> new ArrayList<>(list))来创建副本,并使用peek(list-> Collections.sort(list, comparator))来对每个列表进行排序,然后应用流排序和收集。 - Thomas
显示剩余4条评论
2个回答

1
首先,让我们重申一下:HashMap是无序的,所以你需要其他东西。你的Sorter.sort()方法实际上将值收集到一个LinkedHashMap中,该集合基于插入顺序提供迭代顺序,并且对于你的用例来说可能也可以使用。只是为了明确(也为了他人的利益):这不会对地图本身进行排序,而是创建一个新的LinkedHashMap。
现在来看看你的比较器:如果你想比较2个列表,你可能想要比较相等索引位置的元素。因此,你的比较器需要像这样:
Comparator<List<Person>> = (l1, l2) -> {
   Iterator<Person> itr1 = l1.iterator();
   Iterator<Person> itr2 = l2.iterator();

   while( itr1.hasNext() && itr2.hasNext() ) {
     Person p1 = itr1.next();
     Person p2 = itr1.next();

     int result = Person.COMPARE_BY_DATE.compare(p1, p2);
     if( result != 0 ) {
       return result;
     }
   }

   return 0;
};
 

然而,列表的长度也可能不同,因此您可能也需要处理这种情况:
Comparator<List<Person>> = (l1, l2) -> {
   //iterators and loop here

   //after the loop it seems all elements at equal indices are equal too
   //now compare the sizes

   return Integer.compare(l1.size(), l2.size());
}

1
通过更改代码中使用的Map类型,如@Thomas所建议的,在TreeMap<>中,我发现了解决问题的方法,具体如下:
  1. 首先将所有Person对象列表合并成一个列表。然后按所选标准对其进行排序,例如Person.COMPARE_BY_NAME
  2. 我创建了一个算法,根据项目的标准重新分组排序列表,并在映射中进行。该映射的键对应于Person对象的月份+年份的连接。该算法在评论底部报告;
  3. 基于所选属性,例如Sorter.COMPARATOR_BY_NAME,对映射进行排序;
以下是代码示例:
将所有的List<Person>合并为一个主列表或在创建Map之前的某个位置。
    ...
    //
    List<Person> newPersonList = new ArrayList<>();
    newPersonList.addAll(oldPersonList1);
    newPersonList.addAll(oldPersonList2);
    ...

主要是在地图创建之前或其他位置

    ...
    groupList(Person.COMPARE_BY_NAME, Sorter.COMPARATOR_BY_NAME);
    ...

GroupPerson -> 将合并后的List<Person>按照姓名首字母分组存储在一个TreeMap<String, List<Person>>中的方法。

    public Map<String, List<Person>> groupList(final Comparator<? super Person> itemComparator, final Comparator<? super List<Person>> listComparator)
         
         // Sort Person list by comparator before create TreeSet
         newPersonList.sort(itemComparator);

         Map<String, List<Person>> personMapGrouped = new TreeMap<>();
         
         // Here, create a Map of list
         for (Person person: newPersonList) {
             final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM", Locale.getDefault());
             final String groupKey = dateFormat.format(person.getDateOfBirth());

             if (personMapGrouped.containsKey(groupKey)) {
                // The key is already in the TreeMap; add the Person object against the existing key.
                final List<Person> personListGrouped = personMapGrouped.get(groupKey);
                if (personListGrouped!= null) {
                   personListGrouped.add(person);
                }
             } else {
                // The key is not there in the TreeMap; create a new key-value pair
                final List<Person> personListGrouped = new ArrayList<>();
                personListGrouped.add(person);
                personMapGrouped.put(groupKey, personListGrouped);
             }
         }
         // Here sort the Map by params passed
         final TabPersonSorter sorter = new TabPersonSorter();
         personMapGrouped = sorter.sort(personMapGrouped, listComparator);
    }

在这种情况下,使用上面创建的列表,得到的结果如下:
    "List<Person> mergedList": [
        Person("name1", new Date("2022-01-01")),
        Person("name3", new Date("2021-02-05")),
        Person("name4", new Date("2021-02-03")),
        Person("name12", new Date("2022-01-05")),
        Person("name13", new Date("2022-01-03")),
        Person("name14", new Date("2021-02-01"))
    ]

    "Map<String, List<Person>> mergedMap": {
        "2022-01": [
            Person("name1", new Date("2022-01-01")),
            Person("name12", new Date("2022-01-05")),
            Person("name13", new Date("2022-01-03"))
        ], 
        "2021-02": [
            Person("name3", new Date("2021-02-05")),
            Person("name4", new Date("2021-02-03"))
        ],
        "2022-02": [
            Person("name14", new Date("2021-02-01"))
        ]
    } 

显然,如果地图中的分组不受仅限于年份+月份的限制,排序将会在不同的组中产生预期的效果。实际上,在按日期排序的情况下,这一点得到了很好的尊重。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接