java8collect类型转换_Java8实现stream将对象集合list中抽取
属性集。。。java stream
⾸先新建⼀个实体类Person
@Data
public class Person {
/** 编码 */
private String code;
/** 名字 */
private String name;
public Person(String code, String name) {
this.name = name;
}
}
实例化三个对象放⼊list集合中
public static void main(String[] args) {
Person person1 = new Person("001", "张三");
Person person2 = new Person("002", "李四");
Person person3 = new Person("002", "王五");
List personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.forEach(t -> System.out.String()));
}
输出结果为:
Person(code=001, name=张三)
Person(code=002, name=李四)
Person(code=002, name=王五)
1.抽取对象的code作为key,name作为value转化为map集合
⽅法为
private static HashMap listToMap(List personList) {
return (HashMap)personList.stream()
.filter(t -> t.getName()!=null)
.Map(Person::getCode,Person::getName,(k1,k2)->k2));
}
filter() ⽅法作⽤是过滤掉名字为空的对象,当对象的名字为null时,会出现NPE空指针异常
(k1,k2)->k2 意思是遇到相同的key时取第⼆个值
(k1,k2)->k1 意思是遇到相同的key时取第⼀个值
调⽤这个⽅法
HashMap personMap = listToMap(personList);
personMap.forEach((k,v)-> System.out.String() + " - " + v.toString()));
输出结果为:
001 - 张三
002 - 王五
2.抽取对象的name得到name的list集合
⽅法为
private static List getNameList(List personList) {
return personList.stream().map(Person::getName).List());
}
调⽤这个⽅法
List nameList = getNameList(personList);
nameList.forEach(t -> System.out.String()));
输出结果为:
张三
李四
王五
补充:java8 使⽤stream将List转成Map,或者从List对象中获取单个属性List,List中根据某个字段排序1.学⽣类
import lombok.Data;
@Data
public class Student{
private String stuId;
private String name;
private String age;
private String sex;
}
2.测试类
public class Test {
public static void main(String[] args) {
// 创建学⽣List
List list = createStudentList();
// 1.获取value为Student对象,key为学⽣ID的Map
getStudentObjectMap(list);
/
/ 2.获取value为学⽣姓名,key为学⽣ID的Map
getStudentNameMap(list);
// 3.获取学⽣姓名List
getStudentNameList(list);
//4.List中删除学⽣id = 1的对象
//5.如果StudentId为Long类型如何转?
Map mapStr = list.stream().Map(student -> StuId().toString(), student -> JSONString(student)));
//6.根据List中Student的学⽣姓名排序
Collections.sort(list, (o1, o2) -> {
if (o1.getName()Name()) > 0) {
return 1;
} else if (o1.getName()Name()) < 0) {
return -1;
} else {
return 0;
}
});
//7.List遍历
List listStr = new ArrayList<>();
List listStu = new ArrayList<>();
listStr.forEach(studentStr -> {
listStu.add(JSON.parseObject(studentStr, Student.class));
});
//List根据某个字段过滤、排序
listStu.stream()
.filter(student -> Sex().equals("⼥"))
.sorted(Comparatorparing(Student::getName))
.List());
//List根据某个字段分组
Map> sexGroupMap = listStu.stream()
.upingBy(Student::getSex));
//如果Map中多个名称相同,则studentId⽤逗号间隔
Map studentNameIdMap = listStu.stream()
.collect(toMap(Student::getName,Student::getStuId,(s,a)->s+","+a)); }
public static List createStudentList() {
List list = new ArrayList();
Student lily = new Student();
lily.setStuId("1");
lily.setName("lily");
lily.setAge("14");
lily.setSex("⼥");
Student xiaoming = new Student();
xiaoming.setStuId("2");
xiaoming.setName("xiaoming");
xiaoming.setAge("15");
xiaoming.setSex("男");
list.add(lily);
list.add(xiaoming);
return list;
}
public static Map getStudentObjectMap(List list) {
Map map = list.stream().Map(Student::getStuId, student -> student));
map.forEach((key, value) -> {
System.out.println("key:" + key + ",value:" + value);
});
return map;
}
public static Map getStudentNameMap(List list) {
Map map = list.stream().Map(Student::getStuId, Student::getName));
map.forEach((key, value) -> {
System.out.println("key:" + key + ",value:" + value);
});
return map;
}
public static List getStudentNameList(List list) {
List result = list.stream().map(student -> Name()).List());
for (String name : result) {
System.out.println("name:" + name);
}
return result;
}
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持云海天教程。如有错误或未考虑完全的地⽅,望不吝赐教。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论