java集合对List进行排序

java集合对List进行排序

List排序4种写法

方式1:JAVA中我们可以使用java.util.Collections类的sort(List list)方法对list集合中的元素排序。

方式2:JDK8之后特别是lambda表达式的盛行,而且Collections的sort方法其实是调用了List接口自己的sort方法;所以可以使用List接口自己的sort方法排序

方式3:方式2的lambda写法

方式4:Stream流的sort方法写法

集合元素是基本类型包装类型

public static void main(String[] args) {

List numList=new ArrayList<>();

numList.add(999);

numList.add(123);

numList.add(456);

numList.add(66);

numList.add(9);

Collections.sort(numList); //使用Collections类的方法排序

numList.sort(new Comparator() {//使用List接口的方法排序

@Override

public int compare(Integer o1, Integer o2) {

return o1.compareTo(o2);

}

});

//lambda表达式实现List接口sort方法排序

numList.sort((num1,num2)->{return num1.compareTo(num2);});

System.out.println(numList);

}

对象的集合根据某个属性排序

例如:对User对象集合根据User类的sex属性排序,默认是升序

public static void main(String[] args) {

List numList=new ArrayList<>();

User u=new User();

u.setSex(12);

numList.add(u);

User u1=new User();

u1.setSex(34);

numList.add(u1);

User u2=new User();

u2.setSex(6);

numList.add(u2);

User u3=new User();

u3.setSex(99);

numList.add(u3);

//Collections类的sort方法对对象集合排序,要传集合和Comparator接口两个参数

Collections.sort(numList, new Comparator() {

@Override

public int compare(User o1, User o2) {

Integer sex1= o1.getSex();

Integer sex2= o2.getSex();

return sex1.compareTo(sex2);

}

});

//List接口自身的sort方法对对象集合排序,重写Comparator接口方法即可

numList.sort(new Comparator() {

@Override

public int compare(User u1, User u2) {

Integer sex1= u1.getSex();

Integer sex2= u2.getSex();

return sex1.compareTo(sex2);

}

});

//List接口的sort方法,lambda表达式写法

numList.sort((u4,u5)->{

Integer sex1= u4.getSex();

Integer sex2= u5.getSex();

return sex1.compareTo(sex2);

});

System.out.println(numList);

}

使用Stream流排序

list.stream().sorted(Comparator.comparing(Sortable::getOrder).reversed()).collect(Collectors.toList());

实现:

1.首先你需要list.parallelStream().sorted 进行流处理,使用parallelStream可以充分调度多核CPU。

2.使用Comparator.comparing进行排序,reversed()进行倒序排列,thenComparing进行下一个排序。

3.Comparator.comparing()里面的内容,也是就是Object::getter,例如KeywordCounterDTO::getKeyword

4.最后格式化为需要的格式 List 是.collect(Collectors.toList()) , Map 是 .collect(Collectors.toMap(KeywordCounterDTO::getKey, KeywordCounterDTO::getValue))

list = list.parallelStream().sorted(

Comparator.comparing(KeywordCounterDTO::getAllCounter).reversed().thenComparing(KeywordCounterDTO::getKeyword)

).collect(Collectors.toList());

使用 Comparator.reversed 进行排序

返回相反的排序规则

/**

* 相反的排序规则

*/

Collections.sort(employees, Comparator.comparing(Employee::getName).reversed());

employees.forEach(System.out::println);

使用 Comparator.nullsFirst进行排序

当集合中存在null元素时,可以使用针对null友好的比较器,null元素排在集合的最前面

employees.add(null); //插入一个null元素

Collections.sort(employees, Comparator.nullsFirst(Comparator.comparing(Employee::getName)));

employees.forEach(System.out::println);

Collections.sort(employees, Comparator.nullsLast(Comparator.comparing(Employee::getName)));

employees.forEach(System.out::println);

使用 Comparator.thenComparing 排序

首先使用 name 排序,紧接着在使用ege 排序

Collections.sort(employees, Comparator.comparing(Employee::getAge).thenComparing(Employee::getName));

employees.forEach(System.out::println);

sort函数详解

升序、降序

sort方法排序默认是升序 ASC

List的Sort函数中的比较函数CompareTo有三种结果 1, -1 ,0分别代表大,小,相等。默认List的排序是升序排序。

举个例子:在比较函数CompareTo()中,如果 x>y return 1;则是按照升序排列。如果x>y return -1;则是按照降序排列。这就是1和-1大小的含义。其实你非要这么写 xy return 1;升序,如果想要降序只需return -1;即可。

Tips:系统List默认的排序是升序,如果你想要降序,可以直接在比较函数前面加个负号,把返回结果由1变成-1即可。例如:

List list = new List() { 2, 1, 3, 4 };

list.Sort((x, y) -> -x.CompareTo(y)); // CompareTo 默认返回 1 升序

Console.WriteLine(list); //4,3,2,1

非数值类型、string等排序

对于非数值类型、string等或者官方未来实现IComparable接口的类型,可通过实现IComparable接口重写CompareTo方法来排序。

举个例子:自定义一个类,然后继承字: IComparable<>,然后实现接口的方法 int CompareTo(object obj_)就可以了

public class Person : IComparable

{

public int id;

public string name;

public Person()

{

id = 0;

name = "name";

}

public Person(int id_, string name_)

{

id = id_;

name = name_;

}

public int CompareTo(Person obj_)

{

if (this.id > obj_.id)

return 1;

else

return -1;

}

}

多权重排序

其实就是对优先级最高的字段进行排序,然后在对次级优先级的字段进行排序就可以了。例如对Persond的对象先进行ID排序,然后再进行Name排序:

public int CompareToIDName(Person x_, Person y_)

{

if (x_.id > y_.id)

return 1;

else if (x_.id == y_.id)

{

return x_.name.CompareTo(y_.name);

}

else

return -1;

}

匿名函数来实现Comparison

虽然想实现排序上面的接口代码也不多,但有时候只是偶尔排序,并不想修改类,怎么办呢?当然有更简单的方法,委托和lambda表达式:

public void Init()

{

m_personList.Add(new Person(10001, "Zhao"));

m_personList.Add(new Person(10006, "Qian"));

m_personList.Add(new Person(10006, "Zhao"));

m_personList.Add(new Person(10004, "Li"));

m_personList.Add(new Person(10006, "Zhao"));

m_personList.Sort();

m_personList.Sort(delegate (Person x, Person y)

{

if (x.id > y.id)

return 1;

else

return -1;

});

m_personList.Sort((x, y) =>

{

return x.name.CompareTo(y.name);

});

m_personList.Sort((x, y) => CompareToIDName(x, y));

}

原文章地址:

https://blog.csdn.net/qq_42672770/article/details/119840956

https://blog.csdn.net/qq_29569183/article/details/103928183

https://blog.csdn.net/moshowgame/article/details/122052307

相关文章

山楂 料理與家常食譜
Bet体育365验证提款

山楂 料理與家常食譜

⌛ 01-04 👁️ 7342
传奇一打开就闪退怎么办?传奇老是掉线解决方法
英国投注网站365

传奇一打开就闪退怎么办?传奇老是掉线解决方法

⌛ 01-08 👁️ 5460