除某些项目外,按字母顺序对java列表进行排序
我有一个叫Country的java类除某些项目外,按字母顺序对java列表进行排序,java,Java,我有一个叫Country的java类 public class Conutry { private String Code; private String name; public Conutry(String code, String name) { Code = code; this.name = name; } public String getCode() { return Code; } public void setCode(String code) {
public class Conutry {
private String Code;
private String name;
public Conutry(String code, String name) {
Code = code;
this.name = name;
}
public String getCode() {
return Code;
}
public void setCode(String code) {
Code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我需要创建一个按名称排序的国家列表。然而,美国和加拿大应该是列表中的前两个项目,其余的按国家名称的字母顺序排列。我知道我没有访问国家类别的权限。我知道我必须使用Comparator,但我不知道这里的确切逻辑
public class TestDrive {
public static void main(String[] args) {
List<Conutry> list = new ArrayList<Conutry>();
list.add(new Conutry("AR","Argentina"));
list.add(new Conutry("AL","Albania"));
list.add(new Conutry("US","United States of America"));
list.add(new Conutry("JO","Jordan"));
list.add(new Conutry("DZ","Algeria"));
list.add(new Conutry("CA","Canada"));
list.add(new Conutry("FR","France"));
list.add(new Conutry("TR","Turkey"));
list.add(new Conutry("BR","Brazil"));
list.add(new Conutry("AE","United Arab Emirates"));
//This is my code. I think there should be a better solution
Comparator<Conutry> comparator = new Comparator<Conutry>() {
@Override
public int compare(Conutry c1, Conutry c2) {
if("US".equals(c1.getCode()))
return -1;
if("CA".equals(c1.getCode()))
if(c2.getCode()!="US")
return -1;
else
return 1;
if("US".equals(c2.getCode()))
return 1;
if("CA".equals(c2.getCode()))
if("US".equals(c1.getCode()))
return -1;
else
return 1;
return c1.getName().compareTo(c2.getName()) ;
}
};
list.stream().sorted(comparator).forEach(c-> System.out.println(c.getName()));
}
公共类TestDrive{
公共静态void main(字符串[]args){
列表=新的ArrayList();
增加(新国家(“AR”、“阿根廷”);
增加(新国家(“AL”、“阿尔巴尼亚”);
添加(新国家(“美国”、“美利坚合众国”);
增加(新国家(“JO”、“Jordan”);
增加(新国家(“DZ”、“阿尔及利亚”);
增加(新国家(“CA”、“加拿大”);
增加(新国家(“法国”);
增加(新国家(“TR”、“土耳其”);
增加(新国家(“巴西”);
增加(新国家(“阿拉伯联合酋长国”);
//这是我的代码。我认为应该有更好的解决方案
比较器比较器=新比较器(){
@凌驾
公共整数比较(国家c1,国家c2){
如果(“US”.equals(c1.getCode()))
返回-1;
如果(“CA”.equals(c1.getCode()))
如果(c2.getCode()!=“美国”)
返回-1;
其他的
返回1;
如果(“US”.equals(c2.getCode()))
返回1;
如果(“CA”.equals(c2.getCode()))
如果(“US”.equals(c1.getCode()))
返回-1;
其他的
返回1;
返回c1.getName().compareTo(c2.getName());
}
};
list.stream().sorted(comparator).forEach(c->System.out.println(c.getName());
}
}事情是这样的:
public static void main(String[] args) {
List<Conutry> list = new ArrayList<Conutry>();
// putting these two at the start of the list
list.add(new Conutry("US","United States of America"));
list.add(new Conutry("CA","Canada"));
// building the rest of the list
list.add(new Conutry("AR","Argentina"));
list.add(new Conutry("AL","Albania"));
list.add(new Conutry("JO","Jordan"));
list.add(new Conutry("DZ","Algeria"));
list.add(new Conutry("FR","France"));
list.add(new Conutry("TR","Turkey"));
list.add(new Conutry("BR","Brazil"));
list.add(new Conutry("AE","United Arab Emirates"));
// comparator that compares conutry objects based on their names
public class ConutryComparator implements Comparator<Conutry> {
public int compare(Conutry a, Conutry b) {
// I will leave the null handling part as exercise
return a.getName().compareTo(b.getName());
}
}
// then sort the sublist starting with third element (index 2)
Collections.sort(list.sublist(2, list.size(), new ConutryComparator());
}
publicstaticvoidmain(字符串[]args){
列表=新的ArrayList();
//把这两个放在列表的开头
添加(新国家(“美国”、“美利坚合众国”);
增加(新国家(“CA”、“加拿大”);
//构建列表的其余部分
增加(新国家(“AR”、“阿根廷”);
增加(新国家(“AL”、“阿尔巴尼亚”);
增加(新国家(“JO”、“Jordan”);
增加(新国家(“DZ”、“阿尔及利亚”);
增加(新国家(“法国”);
增加(新国家(“TR”、“土耳其”);
增加(新国家(“巴西”);
增加(新国家(“阿拉伯联合酋长国”);
//比较器,根据conutry对象的名称对其进行比较
公共类ConutryComparator实现Comparator{
公共整数比较(国家a、国家b){
//我将把空处理部分留作练习
返回a.getName().compareTo(b.getName());
}
}
//然后从第三个元素(索引2)开始对子列表进行排序
Collections.sort(list.sublist(2,list.size(),new ConutryComparator());
}
完成您将需要对“自定义比较器”进行一些研究,或者添加除美国和加拿大以外的所有国家,排序,然后插入美国和加拿大?然后使用!这里有一个更紧凑的比较器:
Map priority=Map.of(“US”、-2,“CA”、-1);
comparator comparator=comparator.comparingit(country->priority.getOrDefault(country.getCode(),0))。然后比较(country::getName,Comparator.naturalOrder());
@boot and bonne非常感谢,这对我来说效果很好。我认为应该有更好的解决方案谈论chutzpah——为什么不向我们展示一下您在@Mido上的尝试?