文章目录
利用java8新特性,可以用简洁高效的代码来实现一些数据处理。
1.定义一个Apple对象
代码如下(示例):
import lombok.Data;
import java.util.List;
@Data
public class Student {
//学号
private Integer id;
//姓名
private String name;
//年龄
private Integer age;
//性别
private String gender;
//地址
private String address;
//有参构造函数
public Student(Integer id, String name, Integer age, String gender, String address) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
/**
* @description TODO 控制台输出结果集
* @param studentList Student对象集合数据信息
* @return void
* @author WangTianLiang
* @date 2021/5/8
*/
public static void printlnStudent(List<Student> studentList) {
System.out.println("学 号 姓 名 年 龄 性 别 地 址");
for (int i=0; i<studentList.size(); i++){
System.out.println(studentList.get(i).getId()+" "+studentList.get(i).getName()+" "+studentList.get(i).getAge()+" "+studentList.get(i).getGender()+" "+studentList.get(i).getAddress());
}
System.out.println();
}
}
2.集合对像定义(添加测试数据)
下面来添加一些测试用的数据,代码如下(示例):
/**
* @param
* @return List<Student>
* @description TODO 获取Student对象集合数据信息
* @author WangTianLiang
* @date 2021/5/8
*/
public static List<Student> getStudentData() {
//存放Student对象集合
List<Student> studentList = new ArrayList<Student>();
//使用有参构造给对象赋值
Student student1 = new Student(2021001, "张三", 24, "男", "北京");
Student student2 = new Student(2021002, "李四", 25, "男", "北京");
Student student3 = new Student(2021003, "王五", 20, "男", "上海");
Student student4 = new Student(2021004, "赵六", 20, "女", "天津");
Student student5 = new Student(2021005, "邹七", 22, "女", "天津");
//将对象添加到List集合中
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
studentList.add(student5);
return studentList;
}
3.分组
List里面的对象元素,以某个属性来分组,例如,以Address(地址)分组,将Address相同的放在一起。
分组代码如下(示例):
/**
* @description TODO 分组
* List里面的对象元素,以某个属性来分组,例如,以Address(地址)分组,将Address相同的放在一起
* @param
* @return void
* @author WangTianLiang
* @date 2021/5/8
*/
public static void groupBy() {
List<Student> studentList = getStudentData();
//List 以address分组 Map<String,List<Student>>
Map<String, List<Student>> groupBy = studentList.stream().collect(Collectors.groupingBy(Student::getAddress));
System.err.println(groupBy);
}
分组后结果:
{上海=[Student(id=2021003, name=王五, age=20, gender=男, address=上海)], 天津=[Student(id=2021004, name=赵六, age=20, gender=女, address=天津), Student(id=2021005, name=邹七, age=22, gender=女, address=天津)], 北京=[Student(id=2021001, name=张三, age=24, gender=男, address=北京), Student(id=2021002, name=李四, age=25, gender=男, address=北京)]}
4. List 转 Map
需要注意的是:
- toMap 如果集合对象有重复的key,会报错Duplicate key …
- student1,student2的Address都为北京,可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
List 转 Map代码如下(示例):
/**
* @description TODO List 转 Map
* List -> Map
* @param
* @return void
* @author WangTianLiang
* @date 2021/5/8
*/
public static void listConversionMap() {
List<Student> studentList = getStudentData();
/**
* List -> Map
* 需要注意的是:
* toMap 如果集合对象有重复的key,会报错Duplicate key ....
* student1,student2 的Address都为 北京。
* 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
*/
Map<String, Student> studentMap = studentList.stream().collect(Collectors.toMap(Student::getAddress, a -> a,(k1,k2)->k1));
System.err.println(studentMap);
}
List 转 Map后结果:
{上海=Student(id=2021003, name=王五, age=20, gender=男, address=上海), 天津=Student(id=2021004, name=赵六, age=20, gender=女, address=天津), 北京=Student(id=2021001, name=张三, age=24, gender=男, address=北京)}
5. 去重
按照 Address(居住地) 去重。
去重代码如下(示例):
/**
* @description TODO 去重
* @param
* @return void
* @author WangTianLiang
* @date 2021/5/8
*/
public static void distinctList() {
List<Student> studentList = getStudentData();
System.err.println("---------------------去重前-------------------------");
Student.printlnStudent(studentList);
// 按照 Address(居住地) 去重
studentList = studentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(Student::getAddress))), ArrayList::new));
System.err.println("---------------------去重后-------------------------");
Student.printlnStudent(studentList);//控制台输出
}
去重后结果:
5. 过滤筛选
筛选年龄大于等于24岁的学生。
过滤筛选代码如下(示例):
/**
* @description TODO 过滤筛选
* @param
* @return void
* @author WangTianLiang
* @date 2021/5/8
*/
public static void filterList() {
List<Student> studentList = getStudentData();
System.err.println("---------------------过滤筛选前-------------------------");
Student.printlnStudent(studentList);
// 筛选年龄大于等于24岁的学生
studentList = studentList.stream().filter(item -> item.getAge() >= 24).collect(Collectors.toList());
System.err.println("---------------------过滤筛选后-------------------------");
Student.printlnStudent(studentList); //控制台输出
}
过滤筛选后结果:
5. 排序 注意:(排序字段可以为 null, null不会导致排序失败)
- 按照年龄从大到小排序。
- 按照年龄从小到大排序。
- 先按照年龄从小到大排序排序,再按照id从大到小排序
排序代码如下(示例):
/**
* @description TODO 排序 注意:(排序字段可以为 null, null不会导致排序失败)
* @param
* @return void
* @author WangTianLiang
* @date 2021/5/8
*/
public static void sortList() {
List<Student> studentList = getStudentData();
// 按照年龄从大到小排序
// 方式一
studentList.sort((stu1, stu2)
-> stu1.getAge() == null ? 1 : (stu2.getAge() == null ? -1 : stu2.getAge().compareTo(stu1.getAge())));
System.out.println("---------------------按照年龄从大到小排序后 方式一-------------------------");
Student.printlnStudent(studentList); //控制台输出
// 方式二
studentList.sort(Comparator.comparing(Student::getAge, (stu1, stu2)
-> stu1 == null ? 1 : (stu2 == null ? -1 : stu2.compareTo(stu1))));
System.out.println("---------------------按照年龄从大到小排序后 方式二-------------------------");
Student.printlnStudent(studentList); //控制台输出
// 按照年龄从小到大排序
// 方式一
studentList.sort((stu1, stu2)
-> stu2.getAge() == null ? 1 : (stu1.getAge() == null ? -1 : stu1.getAge().compareTo(stu2.getAge())));
System.out.println("---------------------按照年龄从小到大排序后 方式一-------------------------");
Student.printlnStudent(studentList); //控制台输出
// 方式二
studentList.sort(Comparator.comparing(Student::getAge, (stu1, stu2)
-> stu2 == null ? 1 : (stu1 == null ? -1 : stu1.compareTo(stu2))));
System.out.println("---------------------按照年龄从小到大排序后 方式二-------------------------");
Student.printlnStudent(studentList); //控制台输出
// 先按照年龄从小到大排序排序,再按照id从大到小排序
studentList.sort(Comparator.comparing(Student::getAge, (stu1, stu2)
-> stu1 == null ? 1 : (stu2 == null ? -1 : stu1.compareTo(stu2))).thenComparing((stu1, stu2)
-> stu1.getId() == null ? 1 : (stu2.getId() == null ? -1 : stu2.getId().compareTo(stu1.getId()))));
System.out.println("---------------------先按照年龄从小到大排序排序,再按照id从大到小排序后-------------------------");
Student.printlnStudent(studentList); //控制台输出
}
排序后结果:
6.分组后获取某个属性拼接,类似于Sql类转行
- 按照年龄从大到小排序。
- 按照年龄从小到大排序。
- 先按照年龄从小到大排序排序,再按照id从大到小排序
排序代码如下(示例):
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @Description:
* @Auther: Wangtianliang
* @Date: 2021/12/13 20:59
*/
@Data
public class Student {
//学号
private Integer id;
//姓名
private String name;
//年龄
private Integer age;
//性别
private String gender;
//地址
private String address;
public Student(){
}
//有参构造函数
public Student(String gender, String address) {
this.gender = gender;
this.address = address;
}
//有参构造函数
public Student(Integer id, String name, Integer age, String gender, String address) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
public static void main(String[] args) {
Student stu01 = new Student("1001","a");
Student stu02 = new Student("1001","b");
Student stu03 = new Student("1002","c");
Student stu04 = new Student("1002","d");
List<Student> list = new ArrayList<> ();
list.add(stu01);
list.add(stu02);
list.add(stu03);
list.add(stu04);
Map<String, List<Student>> mapListInfo = list.stream().collect(Collectors.groupingBy(Student::getGender));
Map<String, Optional<String>> collect = mapListInfo.keySet ().stream ().collect (Collectors.toMap (a -> a, b -> mapListInfo.get (b).stream ().map (Student::getAddress).reduce ((c, d) -> c + "," + d)));
System.out.println (JSONObject.toJSON (collect));
for (String key : collect.keySet ()) {
String value = collect.get (key).get ();
}
}
排序后结果:
希望可以帮助到您…