前言
jdk1.8新增了对list的快捷操作,对开发有极大的便利,特记录,便于日后查看
1.List<Object>转化为List<Object.xxx>
将对象列表转化为对象某一属性的列表
@Data
class Person{
private String name;
private String sex;
private String age;
public Person(String name,String sex,Integer age){
this.name = name;
this.sex = sex;
this.age = age;
}
}
List<Person> listPerson = new ArrayList();
listPerson.add(new Person("羊肉泡馍","男",23));
listPerson.add(new Person("牛肉罩杯","男",28));
listPerson.add(new Person("驴肉火烧","女",25));
listPerson.add(new Person("牛肉板面","女",27));
List<String> nameList = listPerson.stream().map(Person::getName).
collect(Collectors.toList());
System.out.println(nameList);
##输出如下
[羊肉泡馍, 牛肉罩杯, 驴肉火烧, 牛肉板面]
2.List<Object>转化为Map
- 转化为Map<Object.xxx,List<Object>>
##沿用上面的赋值操作
Map<String,List<Person>> mapPerson = listPerson.stream().
collect(Collectors.groupingBy(Person::getSex));
##输出如下
{女=[MonitorScreenServiceImpl.Person(name=驴肉火烧, sex=女, age=25),
MonitorScreenServiceImpl.Person(name=牛肉板面, sex=女, age=27)],
男=[MonitorScreenServiceImpl.Person(name=羊肉泡馍, sex=男, age=23),
MonitorScreenServiceImpl.Person(name=牛肉罩杯, sex=男, age=28)]}
- 转化为Map<Object.xxx,List<Object.ooo>>
##省略赋值操作
Map<String,List<Integer>> mapPerson2 = listPerson.stream().
collect(Collectors.groupingBy(Person::getSex,
Collectors.mapping(Person::getAge,Collectors.toList())));
System.out.println(mapPerson2);
##输出如下
{女=[25, 27], 男=[23, 28]}
- 转化为Map<Object.xxx,Object>
Map<String,Person> map = listPerson.stream().collect(Collectors.toMap(Person::getName,t->t,(t1,t2)->t1));
System.out.println(map);
##输出
{羊肉泡馍=MonitorScreenServiceImpl.Person(name=羊肉泡馍, sex=男, age=23),
羊杂汤=MonitorScreenServiceImpl.Person(name=羊杂汤, sex=男, age=18),
驴肉火烧=MonitorScreenServiceImpl.Person(name=驴肉火烧, sex=女, age=25),
牛肉板面=MonitorScreenServiceImpl.Person(name=牛肉板面, sex=女, age=27),
牛肉罩杯=MonitorScreenServiceImpl.Person(name=牛肉罩杯, sex=男, age=28)}
(t1,t2)->t1) 的意思是如果遇到重复的key则保留第一个忽略第二个
- 转化为Map<Object.xxx,Object.ooo>
Map<String,Integer> mapAge = listPerson.stream().collect(Collectors.toMap(Person::getName,Person::getAge,(t1,t2)->t1));
System.out.println(mapAge);
##输出
{羊肉泡馍=23, 羊杂汤=18, 驴肉火烧=25, 牛肉板面=27, 牛肉罩杯=28}
3.List<Object>按照Object属性排序
Comparator提供了多种默认实现,包含 comparingInt、comparingLong、comparingDouble、comparing(可直接用于String排序),本示例使用comparingInt
- 按照单个属性进行排序(正序)
listPerson.sort(Comparator.comparingInt(Person::getAge));
System.out.println(listPerson);
##输出如下
[MonitorScreenServiceImpl.Person(name=羊肉泡馍, sex=男, age=23),
MonitorScreenServiceImpl.Person(name=驴肉火烧, sex=女, age=25),
MonitorScreenServiceImpl.Person(name=牛肉板面, sex=女, age=27),
MonitorScreenServiceImpl.Person(name=牛肉罩杯, sex=男, age=28)]
- 按照单个属性进行排序(倒序)
listPerson.sort(Comparator.comparingInt(Person::getAge).reversed());
System.out.println(listPerson);
##输出如下
[MonitorScreenServiceImpl.Person(name=牛肉罩杯, sex=男, age=28),
MonitorScreenServiceImpl.Person(name=牛肉板面, sex=女, age=27),
MonitorScreenServiceImpl.Person(name=驴肉火烧, sex=女, age=25),
MonitorScreenServiceImpl.Person(name=羊肉泡馍, sex=男, age=23)]
- 自主实现排序方法,可实现多属性排序
男的排后面并按照年龄排序
listPerson.sort((o1,o2)->{
if(!o1.getSex().equals(o2.getSex())){
if("男".equals(o1.getSex())){
return 1;
}else {
return -1;
}
}
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return -1;
} else {
return 0;
}
});
System.out.println(listPerson);
## 输出如下
[MonitorScreenServiceImpl.Person(name=驴肉火烧, sex=女, age=25),
MonitorScreenServiceImpl.Person(name=牛肉板面, sex=女, age=27),
MonitorScreenServiceImpl.Person(name=羊杂汤, sex=男, age=18),
MonitorScreenServiceImpl.Person(name=羊肉泡馍, sex=男, age=23),
MonitorScreenServiceImpl.Person(name=牛肉罩杯, sex=男, age=28)]
4.过滤功能
List<Person> listBack = listPerson.stream().filter( person -> person.getAge() > 23).collect(Collectors.toList());
System.out.println(listBack);
##输出
[MonitorScreenServiceImpl.Person(name=驴肉火烧, sex=女, age=25),
MonitorScreenServiceImpl.Person(name=牛肉罩杯, sex=男, age=28),
MonitorScreenServiceImpl.Person(name=牛肉板面, sex=女, age=27)]
只罗列一些常用功能,其他功能用到后持续更新~
网友评论