public class ListStudy {
public static void main(String[] args) {
/**
* --------------排序------------------
*/
//------------简单数据类型排序--------------
ArrayList<String> list = new ArrayList<>();
list.add("12");
list.add("8");
list.add("9");
list.add("20");
list.add("4");
Collections.sort(list); //降序:reverse()
System.out.println("简单数据类型排序");
System.out.println(list);
/**
* 字符串排序的规则:先从字符串中的第一个字符开始比较其编码的大小,
* 按照升序排序。如果第一个相同比较第二个,如果第二个相同比较第三个.......
*/
ArrayList<User> users = new ArrayList<>();
users.add(new User("Tom", "20", 180));
users.add(new User("Jack", "25", 170));
users.add(new User("Jhon", "30", 160));
users.add(new User("Lucy", "18", 165));
users.add(new User("Made", "40", 172));
//按照身高排序,升序:o1.getHeight()-o2.getHeight(),降序:o2.getHeight()-o1.getHeight()
users.sort(new Comparator<User>() {
public int compare(User o1, User o2) {
return o1.getHeight() - o2.getHeight();
}
});
//或直接使用lambda表达式:
// users.sort(((o1, o2) -> o1.getHeight()-o2.getHeight()));
// 方法引入:按身高顺序
// users.sort(Comparator.comparingInt(User::getHeight));
System.out.println("按照身高顺序:");
Iterator<User> iterator = users.iterator();
//快捷键:itit
while (iterator.hasNext()) {
User next = iterator.next();
System.out.println(next);
}
//-------------按照年龄顺序排序,age是String--------------
users.sort((u1, u2) -> u1.getAge().compareTo(u2.getAge()));
//或 users.sort(Comparator.comparing(User::getAge));
//或 users.sort(Comparator.comparing(User::getAge,(u1,u2)->u1.compareTo(u2)));
System.out.println("按照年龄顺序:");
Iterator<User> iterator1 = users.iterator();
while (iterator1.hasNext()) {
User next = iterator1.next();
System.out.println(next);
}
//-------------按照年龄降序排序,age是String,用compareTo比较--------------
users.sort((u1, u2) -> u2.getAge().compareTo(u1.getAge()));
//或 users.sort(Comparator.comparing(User::getAge,((u1,u2)->u2.compareTo(u1))));
//或 users.sort(Comparator.comparing(User::getAge,(Comparator.reverseOrder())));
System.out.println("按照年龄倒序:");
Iterator<User> iterator2 = users.iterator();
while (iterator2.hasNext()) {
User next = iterator2.next();
System.out.println(next);
}
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class User {
private String name;
private String age;
private Integer height;
}
结果:
简单数据类型排序
[12, 20, 4, 8, 9]
按照身高顺序:
User(name=Jhon, age=30, height=160)
User(name=Lucy, age=18, height=165)
User(name=Jack, age=25, height=170)
User(name=Made, age=40, height=172)
User(name=Tom, age=20, height=180)
按照年龄顺序:
User(name=Lucy, age=18, height=165)
User(name=Tom, age=20, height=180)
User(name=Jack, age=25, height=170)
User(name=Jhon, age=30, height=160)
User(name=Made, age=40, height=172)
按照年龄倒叙:
User(name=Made, age=40, height=172)
User(name=Jhon, age=30, height=160)
User(name=Jack, age=25, height=170)
User(name=Tom, age=20, height=180)
User(name=Lucy, age=18, height=165)
Process finished with exit code 0
初始化集合的方法
/**
* 初始化集合的方法:
*/
List<String> list1=Arrays.asList(new String[]{"1","2","3"});
List<String> list2= Arrays.asList("一","二","三");
//JDK 8
List<String> list3 = Stream.of("one", "two", "three").collect(Collectors.toList());
//JDK 9
List<Integer> list4 = lists.of(1, 2, 3);
//匿名内部类
List<Integer> list5 = new ArrayList<Integer>() {
{
add(1);
add(2);
add(3);
}
};
//使用Google Guava工具集Lists(需要引入Guava工具包)
List<Integer> list6 = Lists.newArrayList(1, 2, 3);
给个饭钱?
- Post link: http://sovzn.github.io/2021/09/15/List%E6%8E%92%E5%BA%8F/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub Issues