作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对这个问题做了很多研究,但我还没有找到一种方法来对自定义对象列表 ( Map<String, List<CustomObj>>
) 的映射进行排序,基于 CustomObj
的比较。属性(如 SORT_BY_NAME
、 SORT_BY_DATE
等)。
我的问题的一个激励性示例是:
Person
(属性为 Name
、 DateOfBith
等...);Map
的 Person
对象 List
如:Map<String, List<Person>>
.映射键是 String
用于其他目的;Person
的属性之间的比较按升序对 map 进行排序。对象(姓名、日期等..)为简单起见,我报告了真实代码,但适用于 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"))
]
}
}
最佳答案
首先,让我们重申:HashMap
是无序的,所以您需要其他东西。您的 Sorter.sort()
方法实际上将值收集到 LinkedHashMap
中,它提供基于插入顺序的迭代顺序,并且适合您的用例。明确一点(也是为了其他人着想):这不会对 map 本身进行排序,而是创建一个新的 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());
}
关于java - 如何对 Map<String, List<CustomObject>> 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70861142/
我是一名优秀的程序员,十分优秀!