准备工作:
public class People
{
public People(int age, String name, boolean sex, int weight, int height)
{
super();
this.age = age;
this.name = name;
this.sex = sex;
this.weight = weight;
this.height = height;
}
public People()
{
super();
}
private int age;
private String name;
private boolean sex;
private int weight;
private int height;
......
}
List<People> list1 = new ArrayList<>();
list1.add(new People(12, "ZS", true, 100, 160));
list1.add(new People(10, "LS", false, 110, 150));
list1.add(new People(10, "WW", true, 100, 170));
- List =>Map
Map<String, People> map = list1.stream().collect(Collectors.toMap(People::getName, people -> people));
- List 分组
Map<String, List<People>> mapList = list1.stream().collect(Collectors.groupingBy(People::getName));
- List 排序 – 从低到高
list1.sort((p1, p2) -> p1.getAge() - p2.getAge());
- List 多条件排序 – 先按照年龄,再按照身高
list1.sort(Comparator.comparing(People::getAge).thenComparing(People::getHeight));