美好的一天
我正在尝试根据阿拉伯语 desc 对 map 进行排序,这是 dto 中的字段之一。但排序时,其中一个 map 对象不会被删除。所有的 key 都不同。这是我的代码
public class TestArabic {
public static void main(String[] args) {
Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();
NationalityDto n10 = new NationalityDto();
n10.setNatid(110);
n10.setDesc("");
m.put(110, n10);
NationalityDto n2 = new NationalityDto();
n2.setNatid(102);
n2.setDesc("الهند");
m.put(102, n2);
NationalityDto n3 = new NationalityDto();
n3.setNatid(103);
n3.setDesc("سعودي");
m.put(103, n3);
NationalityDto n1 = new NationalityDto();
n1.setNatid(101);
n1.setDesc("مصر");
m.put(101, n1);
NationalityDto n4 = new NationalityDto();
n4.setNatid(104);
n4.setDesc("الكويت");
m.put(104, n4);
NationalityDto n5 = new NationalityDto();
n5.setNatid(105);
n5.setDesc("لبنان");
m.put(105, n5);
NationalityDto n6 = new NationalityDto();
n6.setNatid(106);
n6.setDesc("");
m.put(106, n6);
System.out.println(m);
Map<Integer, NationalityDto> sortedMap = sortByValue(m);
System.out.println("About to sort the map");
System.out.println(sortedMap);
List<NationalityDto> list = new ArrayList<>();
//Add elements
for(Map.Entry<Integer, NationalityDto> m1 : sortedMap.entrySet()) {
list.add(m1.getValue());
}
Collections.sort(list, (e1, e2) -> e1.getDesc().compareTo(e2.getDesc()));
Map<Integer, NationalityDto> map = new LinkedHashMap<>();
for(NationalityDto dto : list){
map.put(dto.getNatid(), dto);
}
System.out.println("Sorted map");
System.out.println(map);
}
private static Map<Integer, NationalityDto> sortByValue(Map m) {
Map<Integer, NationalityDto> sortedMap = new TreeMap(new ValueComparator(m));
sortedMap.putAll(m);
return sortedMap;
}
}
class ValueComparator implements Comparator<Integer> {
Map<Integer, NationalityDto> map;
public ValueComparator(Map map) {
this.map = map;
}
public int compare(Integer s1, Integer s2) {
// TODO Auto-generated method stub
return ((NationalityDto) map.get(s1)).getDesc().compareTo(((NationalityDto) map.get(s2)).getDesc());
}
}
输出
{101=NationalityDto [natid=101, desc=مصر], 102=NationalityDto [natid=102, desc=الهند], 103=NationalityDto [natid=103, desc=سعودي], 104=NationalityDto [natid=104, desc=الكويت], 105=NationalityDto [natid=105, desc=لبنان], 106=NationalityDto [natid=106, desc=], 110=NationalityDto [natid=110, desc=]}
About to sort the map
{106=NationalityDto [natid=110, desc=], 104=NationalityDto [natid=104, desc=الكويت], 102=NationalityDto [natid=102, desc=الهند], 103=NationalityDto [natid=103, desc=سعودي], 105=NationalityDto [natid=105, desc=لبنان], 101=NationalityDto [natid=101, desc=مصر]}
Sorted map
{110=NationalityDto [natid=110, desc=], 104=NationalityDto [natid=104, desc=الكويت], 102=NationalityDto [natid=102, desc=الهند], 103=NationalityDto [natid=103, desc=سعودي], 105=NationalityDto [natid=105, desc=لبنان], 101=NationalityDto [natid=101, desc=مصر]}
我不明白为什么 106 键有 110 对象。有什么想法
更新:
现在 106 键丢失了。
最佳答案
您在示例中使用 HashMap
,它在检索元素时不保证任何顺序,因此无论如何您都不会按预期顺序获取元素。
所以,我建议如下:
- 将元素存储在列表中
- 对列表进行排序
- 创建一个
linkedHashMap
并添加所有元素
下面是一个例子;
List<NationalityDto> list = new ArrayList<>();
//Add elements
Collections.sort(list, (e1, e2) -> e1.getDesc().compareTo(e2.getDesc()));
Map<Integer, NationalityDto> map = new LinkedHashMap<>();
for(NationalityDto dto : list){
map.put(dto.getId(), dto);
}
关于java - 基于阿拉伯语 desc 的 map 排序无法使用 java 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42330967/