欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java8 实现stream将对象集合list中抽取属性集合转化为map或list

程序员文章站 2022-04-20 08:50:34
首先新建一个实体类person@datapublic class person { /** 编码 */ private string code; /** 名字 */ private string na...

首先新建一个实体类person

@data
public class person {
 /** 编码 */
 private string code;
 /** 名字 */
 private string name;
 public person(string code, string name) {
  this.code = code;
  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<person> personlist = new arraylist<>();
 personlist.add(person1);
 personlist.add(person2);
 personlist.add(person3);
 personlist.foreach(t -> system.out.println(t.tostring()));
}

输出结果为:

person(code=001, name=张三)

person(code=002, name=李四)

person(code=002, name=王五)

1.抽取对象的code作为key,name作为value转化为map集合

方法为

private static hashmap<string, string> listtomap(list<person> personlist) {
 return (hashmap<string, string>)personlist.stream()
   .filter(t -> t.getname()!=null)
   .collect(collectors.tomap(person::getcode,person::getname,(k1,k2)->k2));
}

filter() 方法作用是过滤掉名字为空的对象,当对象的名字为null时,会出现npe空指针异常

(k1,k2)->k2 意思是遇到相同的key时取第二个值

(k1,k2)->k1 意思是遇到相同的key时取第一个值

调用这个方法

hashmap<string,string> personmap = listtomap(personlist);
personmap.foreach((k,v)-> system.out.println(k.tostring() + " - " + v.tostring()));

输出结果为:

001 - 张三

002 - 王五

2.抽取对象的name得到name的list集合

方法为

private static list<string> getnamelist(list<person> personlist) {
 return personlist.stream().map(person::getname).collect(collectors.tolist());
}

调用这个方法

list<string> namelist = getnamelist(personlist);
namelist.foreach(t -> system.out.println(t.tostring()));

输出结果为:

张三

李四

王五

补充: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<student> 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的对象
 
  list.removeif(student -> student.getstuid().equals(1));
 
  //5.如果studentid为long类型如何转?
 
  map<string, string> mapstr = list.stream().collect(collectors.tomap(student -> student.getstuid().tostring(), student -> json.tojsonstring(student)));
 
  //6.根据list中student的学生姓名排序
  collections.sort(list, (o1, o2) -> {
   if (o1.getname().compareto(o2.getname()) > 0) {
    return 1;
   } else if (o1.getname().compareto(o2.getname()) < 0) {
    return -1;
   } else {
    return 0;
   }
  });
  //7.list遍历
  list<string> liststr = new arraylist<>(); 
  list<student> liststu = new arraylist<>(); 
  liststr.foreach(studentstr -> { 
   liststu.add(json.parseobject(studentstr, student.class));
 
  });
 
  //list根据某个字段过滤、排序
  liststu.stream()
    .filter(student -> student.getsex().equals("女"))
    .sorted(comparator.comparing(student::getname))
    .collect(collectors.tolist());
 
  //list根据某个字段分组
  map<string,list<student>> sexgroupmap = liststu.stream()
              .collect(collectors.groupingby(student::getsex));
  //如果map中多个名称相同,则studentid用逗号间隔
  map<string,string> studentnameidmap = liststu.stream()
              .collect(tomap(student::getname,student::getstuid,(s,a)->s+","+a));
 }
 
 public static list<student> createstudentlist() {
  list<student> list = new arraylist<student>();
  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<object, object> getstudentobjectmap(list<student> list) {
  map<object, object> map = list.stream().collect(collectors.tomap(student::getstuid, student -> student));
  map.foreach((key, value) -> {
   system.out.println("key:" + key + ",value:" + value);
  });
  return map;
 }
 
 public static map<string, string> getstudentnamemap(list<student> list) {
  map<string, string> map = list.stream().collect(collectors.tomap(student::getstuid, student::getname));
  map.foreach((key, value) -> {
   system.out.println("key:" + key + ",value:" + value);
  });
  return map;
 }
 
 public static list<string> getstudentnamelist(list<student> list) {
  list<string> result = list.stream().map(student -> student.getname()).collect(collectors.tolist());
  for (string name : result) {
   system.out.println("name:" + name);
  }
  return result;
 }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。