当前位置:主页 > 技术文档 > Java 下载
Java List提取某一列、分组排序操作

Java List提取某一列、分组排序操作

  • 更新:2021-05-08 15:40:26
  • 大小:0.1 MB
  • 热度:767
  • 审核:弘巧娜
  • 类别:技术文章
  • 格式:PDF

  • 资源介绍
  • 相关推荐

【实例截图】

from clipboard

【核心代码】

package com.llf.list;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @Author llf
 * @Date 2019/11/13 14:36
 **/
public class Test {
 
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        Person person1 = new Person(1, "张三", 1, 70, 1010);
        Person person2 = new Person(2, "李四", 0, 20, 1010);
        Person person3 = new Person(3, "王五", 1, 30, 1010);
        Person person4 = new Person(4, "赵达达", 0, 40, 1010);
        Person person5 = new Person(5, "钱多多", 0, 50, 1000);
        Person person6 = new Person(6, "马鸣", 1, 60, 1000);
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        personList.add(person5);
        personList.add(person6);
 
        // 1、提取某一列(从personList中获取name)
        List<String> nameList = personList.stream().map(Person::getName).collect(Collectors.toList());
        // 2、提取某一列时去重,distinct()函数
        List<String> nameList1 = personList.stream().map(Person::getName).distinct().collect(Collectors.toList());
        // 3、排序,按id升续排列(倒叙,改成:(a, b) -> b.getId() - a.getId();)
        List<Person> sortList = personList.stream().sorted((a, b) -> a.getId() - b.getId()).collect(Collectors.toList());
        // 4、过滤,年龄小于60的
        List<Person> filterList = personList.stream().filter(t -> t.getAge() < 60).collect(Collectors.toList());
        // 5、统计,注意double还是int
        double sum = personList.stream().mapToDouble(t -> t.getMoney()).sum();
        int count = personList.stream().mapToInt(t -> t.getId()).sum();
        // 6、分组,根据性别分组,(这里性别用int,0男1女)
        Map<Integer, List<Person>> map = personList.stream().collect(Collectors.groupingBy(t -> t.getSex()));
 
 
 
 
    }
}

 

资源下载

资源下载地址1:https://pan.baidu.com/s/10Ytu-kIwcHAhFY90hBwGFQ

相关资源

网友留言