Java8 使用stream().map()提取List对象的某一列值及去重
一、对象类User
package com.example.listdemo.entity;
import lombok.Data;
import java.time.LocalDate;
import java.util.List;
/**
* @author qzz
*/
@Data
public class User {
/**
* 姓名
*/
private String name;
/**
* 工号
*/
private String jobNumber;
/**
* 年龄
*/
private Integer age;
/**
* true:男 false:女
*/
private Boolean gender;
/**
* 身高
*/
private Double height;
/**
* 出生日期
*/
private LocalDate birthday;
public User(String name, String jobNumber, Integer age, Boolean gender, Double height, LocalDate birthday){
this.name = name;
this.jobNumber = jobNumber;
this.gender = gender;
this.age = age;
this.height = height;
this.birthday = birthday;
}
/**
* 输出打印信息
* @param list
*/
public static void printUsers(List<User> list){
System.out.println("[姓名]\t\t[工号]\t\t[性别]\t\t[年龄]\t\t[身高]\t\t[生日]");
System.out.println("-----------------------------------------------------------------------");
list.forEach(u-> System.out.println(printValue(u)));
System.out.println(" ");
}
/***
* 输出list结果集
* @param user
* @return
*/
public static String printValue(User user){
String str=String.format("%s\t\t\t%s\t\t%s\t\t%s\t\t\t%s\t\t%s",
user.name,user.jobNumber,user.gender.toString(),user.age.toString(),
user.height.toString(),user.birthday.toString());
return str;
}
}
二、测试数据
//构建测试数据
List<User> list = new ArrayList<User>();
list.add(new User("张三","000001",26,true,1.76, LocalDate.of(1996,1,18)));
list.add(new User("小莉","000002",21,false,1.61, LocalDate.of(2001,1,18)));
list.add(new User("李四","000003",22,true,1.83, LocalDate.of(2000,1,18)));
list.add(new User("程曦","000004",20,false,1.64, LocalDate.of(2002,1,18)));
list.add(new User("喜喜","000005",20,false,1.67, LocalDate.of(2002,1,18)));
三、提取某一列
//1.提取某一列
List<String> nameList = list.stream().map(User::getName).collect(Collectors.toList());
System.out.println(JSON.toJSONString(nameList));
四、提取age列并去重(使用distinct()函数)
//2.从列表中提取age并去重
List<Integer> ageList = list.stream().map(User::getAge).distinct().collect(Collectors.toList());
System.out.println(JSON.toJSONString(ageList));
五、完成代码
package com.example.listdemo.utils;
import com.alibaba.fastjson.JSON;
import com.example.listdemo.entity.User;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* List集合去重:使用distinct()函数
* 使用stream().map()提取List对象的某一列值及去重
* @author qzz
*/
public class ListDistinct {
public static void main(String[] args) {
//构建测试数据
List<User> list = new ArrayList<User>();
list.add(new User("张三","000001",26,true,1.76, LocalDate.of(1996,1,18)));
list.add(new User("小莉","000002",21,false,1.61, LocalDate.of(2001,1,18)));
list.add(new User("李四","000003",22,true,1.83, LocalDate.of(2000,1,18)));
list.add(new User("程曦","000004",20,false,1.64, LocalDate.of(2002,1,18)));
list.add(new User("喜喜","000005",20,false,1.67, LocalDate.of(2002,1,18)));
//输出list列表
// User.printUsers(list);
//1.提取某一列
List<String> nameList = list.stream().map(User::getName).collect(Collectors.toList());
System.out.println(JSON.toJSONString(nameList));
//2.从列表中提取age并去重
List<Integer> ageList = list.stream().map(User::getAge).distinct().collect(Collectors.toList());
System.out.println(JSON.toJSONString(ageList));
}
}