我正在尝试对 map 进行排序。我的 map 的键为整数,值为 dto 对象。但是,问题就到这里了。 dto 对象将具有该国家的阿拉伯语描述。因此我想使用阿拉伯语名称进行排序。我尝试使用
@Override
public int compare(String s1, String s2) {
if(map.get(s1) >= map.get(s2)){
return -1;
}else{
return 1;
}
}
但是,这仅适用于字符串对象。
我的实际代码是
Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();
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);
System.out.println(m);
更新的代码
public class TestArabic {
public static void main(String[] args) {
Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();
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);
System.out.println(m);
Map sortedMap = sortByValue(m);
}
private static Map sortByValue(Map m) {
Map sortedMap = new TreeMap(new ValueComparator(m));
sortedMap.putAll(m);
return sortedMap;
}
}
class ValueComparator implements Comparator {
Map map;
public ValueComparator(Map map) {
this.map = map;
}
@Override
public int compare(String s1, String s2) {
return map.get(s1).getDesc().compareTo(map.get(s2).getDesc());
}
}
ValueComparator 类不允许使用字符串和字符串参数的比较方法。代码有什么问题吗?
最佳答案
好的,这是有效的完整代码。
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class TestArabic {
public static void main(String[] args) {
Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();
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);
System.out.println(m);
Map sortedMap = sortByValue(m);
System.out.println(sortedMap);
}
private static Map 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());
}
}
希望对你有帮助。
关于java - 在java中使用值对象内的阿拉伯值对映射进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42282778/