List对象类(StudentInfo)

public class StudentInfo implements Comparable<StudentInfo> {

    //名称
private String name; //性别 true男 false女
private Boolean gender; //年龄
private Integer age; //身高
private Double height; //出生日期
private LocalDate birthday; public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday){
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.birthday = birthday;
} public String toString(){
String info = String.format("%s\t\t%s\t\t%s\t\t\t%s\t\t%s",this.name,this.gender.toString(),this.age.toString(),this.height.toString(),birthday.toString());
return info;
} public static void printStudents(List<StudentInfo> studentInfos){
System.out.println("[姓名]\t\t[性别]\t\t[年龄]\t\t[身高]\t\t[生日]");
System.out.println("----------------------------------------------------------");
studentInfos.forEach(s->System.out.println(s.toString()));
System.out.println(" ");
} @Override
public int compareTo(StudentInfo ob) {
return this.age.compareTo(ob.getAge());
//return 1;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Boolean getGender() {
return gender;
} public void setGender(Boolean gender) {
this.gender = gender;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Double getHeight() {
return height;
} public void setHeight(Double height) {
this.height = height;
} public LocalDate getBirthday() {
return birthday;
} public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
}

StudentInfo对象类

测试数据

//测试数据,请不要纠结数据的严谨性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));
studentList.add(new StudentInfo("张小丽",false,18,1.61,LocalDate.of(2001,6,3)));
studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));
studentList.add(new StudentInfo("陈小跑",false,17,1.67,LocalDate.of(2002,10,18)));

提取某一列(以name为例)

 //输出List
StudentInfo.printStudents(studentList);
//从对象列表中提取一列(以name为例)
List<String> nameList = studentList.stream().map(StudentInfo::getName).collect(Collectors.toList());
//提取后输出name
nameList.forEach(s-> System.out.println(s));

输出结果如下图:

提取age列并排重(使用distinct()函数)

//提取前输出
StudentInfo.printStudents(studentList);
//从对象列表中提取age并排重
List<Integer> ageList = studentList.stream().map(StudentInfo::getAge).distinct().collect(Collectors.toList());
ageList.forEach(a-> System.out.println(a));

结果如下图:

Java8 使用 stream().map()提取List对象的某一列值及排重的更多相关文章

  1. Java8之Stream/Map

    本篇用代码示例结合JDk源码讲了Java8引入的工具接口Stream以及新Map接口提供的常用默认方法.    参考:http://winterbe.com/posts/2014/03/16/java ...

  2. 使用Java Stream,提取集合中的某一列/按条件过滤集合/求和/最大值/最小值/平均值

    不得不说,使用Java Stream操作集合实在是太好用了,不过最近在观察生产环境错误日志时,发现偶尔会出现以下2个异常: java.lang.NullPointerException java.ut ...

  3. Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)

    内容简介 本文主要说明在Java8及以上版本中,使用stream().filter()来过滤一个List对象,查找符合条件的对象集合. List对象类(StudentInfo) public clas ...

  4. 内置对象Clob对从数据库表中取的字符大对象CLOB类型的列值进行读取操作

    package readclobDemo.bao; import java.io.IOException; import java.io.Reader; import java.sql.Clob; i ...

  5. java8 list转map,list集合中的元素的属性转set,list集合中对象的属性转list

    一.使用java8对list操作 1.1list转map private Map<String, Member> getMemberMap() { List<Member> m ...

  6. java8中stream的map和flatmap的理解

    转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...

  7. java8中optional和.stream().map()

    使用optional的好处:是一个可以包含或不可以包含非空值的容器对象,更加友好的处理程序中的空对象. Optional<T>有方法 isPresent() 和 get() 是用来检查其包 ...

  8. java8使用stream的collect进行list转map注意事项

    1.创建Person类 package com.xkzhangsan.normal.collectors; public class Person { private Integer id; priv ...

  9. java8 stream().map().collect()用法

    有一个集合: List<User> users = getList(); //从数据库查询的用户集合 现在想获取User的身份证号码:在后续的逻辑处理中要用: 常用的方法我们大家都知道,用 ...

随机推荐

  1. delphi数据库的备份及还原

    实例应用1: //备份procedure TF_DataBaseBackUp.Btn_bfClick(Sender: TObject); var i:integer; begin if SaveDia ...

  2. Linux下开放防火墙端口

    方法一:1.vi /etc/sysconfig/iptables 2.-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEP ...

  3. 分享知识-快乐自己:Liunx 安装MySQL

    第一步: 1):下载mysql安装包:这里选择下载版本 5.6.33,通用版,linux下64位 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql- ...

  4. Vue2.0 探索之路——vuex入门教程和思考

    Vuex是什么 首先对于vuex是什么,我先引用下官方的解释. Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可 ...

  5. 利用Hibernate 框架,实现对数据库的增删改查

    增: package com.maya.test; import org.hibernate.*; import org.hibernate.cfg.*; import com.maya.model. ...

  6. Android开发中高效的数据结构

    android开发中,在java2ee或者android中常用的数据结构有Map,List,Set,但android作为移动平台,有些api(很多都是效率问题)显然不够理想,本着造更好轮子的精神,an ...

  7. Mysql 排序null值 排序问题分析

    mysql中null值的排序问题分析   如下表t_user:  name age zhangsan 1 lisi NULL wangwu 2   www.2cto.com   执行一下sql:  S ...

  8. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

  9. BZOJ3307:雨天的尾巴

    浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...

  10. BZOJ1067:[SCOI2007]降雨量

    浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...