欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list

 更新時(shí)間:2021年02月04日 15:13:21   作者:文耀文耀  
這篇文章主要介紹了Java8 實(shí)現(xiàn)stream將對(duì)象集合list中抽取屬性集合轉(zhuǎn)化為map或list的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

首先新建一個(gè)實(shí)體類Person

@Data
public class Person {
 /** 編碼 */
 private String code;
 /** 名字 */
 private String name;
 public Person(String code, String name) {
  this.code = code;
  this.name = name;
 }
}

實(shí)例化三個(gè)對(duì)象放入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()));
}

輸出結(jié)果為:

Person(code=001, name=張三)

Person(code=002, name=李四)

Person(code=002, name=王五)

1.抽取對(duì)象的code作為key,name作為value轉(zhuǎn)化為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() 方法作用是過濾掉名字為空的對(duì)象,當(dāng)對(duì)象的名字為null時(shí),會(huì)出現(xiàn)NPE空指針異常

(k1,k2)->k2 意思是遇到相同的key時(shí)取第二個(gè)值

(k1,k2)->k1 意思是遇到相同的key時(shí)取第一個(gè)值

調(diào)用這個(gè)方法

HashMap<String,String> personMap = listToMap(personList);
personMap.forEach((k,v)-> System.out.println(k.toString() + " - " + v.toString()));

輸出結(jié)果為:

001 - 張三

002 - 王五

2.抽取對(duì)象的name得到name的list集合

方法為

private static List<String> getNameList(List<Person> personList) {
 return personList.stream().map(Person::getName).collect(Collectors.toList());
}

調(diào)用這個(gè)方法

List<String> nameList = getNameList(personList);
nameList.forEach(t -> System.out.println(t.toString()));

輸出結(jié)果為:

張三

李四

王五

補(bǔ)充:java8 使用stream將List轉(zhuǎn)成Map,或者從List對(duì)象中獲取單個(gè)屬性List,List中根據(jù)某個(gè)字段排序

1.學(xué)生類

import lombok.Data; 
@Data
public class Student{ 
 private String stuId; 
 private String name; 
 private String age; 
 private String sex; 
}

2.測(cè)試類

public class Test {
 public static void main(String[] args) {
 
  // 創(chuàng)建學(xué)生List
  List<Student> list = createStudentList();
 
  // 1.獲取value為Student對(duì)象,key為學(xué)生ID的Map
  getStudentObjectMap(list);
 
  // 2.獲取value為學(xué)生姓名,key為學(xué)生ID的Map
  getStudentNameMap(list);
 
  // 3.獲取學(xué)生姓名List
  getStudentNameList(list);
 
  //4.List中刪除學(xué)生id = 1的對(duì)象
 
  list.removeIf(student -> student.getStuId().equals(1));
 
  //5.如果StudentId為Long類型如何轉(zhuǎn)?
 
  Map<String, String> mapStr = list.stream().collect(Collectors.toMap(student -> student.getStuId().toString(), student -> JSON.toJSONString(student)));
 
  //6.根據(jù)List中Student的學(xué)生姓名排序
  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根據(jù)某個(gè)字段過濾、排序
  listStu.stream()
    .filter(student -> student.getSex().equals("女"))
    .sorted(Comparator.comparing(Student::getName))
    .collect(Collectors.toList());
 
  //List根據(jù)某個(gè)字段分組
  Map<String,List<Student>> sexGroupMap = listStu.stream()
              .collect(Collectors.groupingBy(Student::getSex));
  //如果Map中多個(gè)名稱相同,則studentId用逗號(hào)間隔
  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;
 }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論