import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class List4 {
public static void main(String[] args) {
List<User> list = new ArrayList<>(List.of(new User(11,"李四",18),new User(19,"周强强",68),new User(12,"张三",25),new User(13,"张三丰",22),new User(14,"赵四",38)));
System.out.println(list);
//乱序
Collections.shuffle(list);
System.out.println(list);
//根据年龄降序
Collections.sort(list,(a,b)->b.getAge()-a.getAge());
System.out.println(list);
//根据年龄升序
Collections.sort(list,(a,b)->a.getAge()-b.getAge());
System.out.println(list);
}
public static void test(String[] args) {
List<Integer> list = new ArrayList<>(List.of(10,5,20,6,3,100,200,9));
System.out.println(list);
//乱序
Collections.shuffle(list);
System.out.println(list);
//升序
Collections.sort(list);
System.out.println(list);
//升序
Collections.sort(list,(a,b)->a-b);
System.out.println(list);
//降序
Collections.sort(list,(a,b)->b-a);
System.out.println(list);
//字符串List<String> 排序
List<String> ls = new ArrayList<>(List.of("javascript","c","go","ruby","java","php","python"));
System.out.println(ls);
Collections.shuffle(ls);//乱序 洗牌
System.out.println(ls);
Collections.sort(ls);//升序
System.out.println(ls);
Collections.sort(ls, Comparator.reverseOrder());//降序
System.out.println(ls);
//字符串根据长度升序
Collections.sort(ls,(a,b)->a.length()-b.length());
System.out.println(ls);
//字符串根据长度降序
Collections.sort(ls,(x,y)->y.length()-x.length());
System.out.println(ls);
}
}
List集合包括(Intger、String、对象)等集合的升序、降序、乱序
最新推荐文章于 2024-04-27 17:30:34 发布