zoukankan      html  css  js  c++  java
  • java8处理

    记一下java流处理的操作

    1.去重,按照billTypeCode去重

    list = list.stream().collect(
    Collectors.collectingAndThen(Collectors.toCollection(
    () -> new TreeSet<>(Comparator.comparing(o -> o.getBillTypeCode()))), ArrayList::new));
    排序:
    List<DictValue> sortedDictValues = dictValues.stream()
    .sorted(Comparator.comparingInt(DictValue::getOrderNum))
    .collect(Collectors.toList());

    2.求和

    Double sum = workDoc.getItemList().stream().mapToDouble(e -> e.getAllocatedQuantity().doubleValue()).reduce(0, Double::sum);
    Integer ageSum = persons .stream() .reduce(0, (sum, p) -> sum += p.age, (sum1, sum2) -> sum1 + sum2);

    3.遍历赋值

    list.stream().forEach(e -> e.setStatus("30"));

    4.过滤

    List<ItemInfo> itemone = new ArrayList<>(itemInfos);
    itemone = itemone.stream().filter(o -> matnr.equals(o.getItemCode())).collect(Collectors.toList());

    5.map: stream().map()可以让你转化一个对象成其他的对象

    List<String> lineList = itemCodeNoExistList.stream().map(ShopOrderExcelDto::getLine).collect(Collectors.toList());

    6.把list转化为map,key是id,value是实体,如果存在重复的key,取第一个实体

    Map<Integer, Person> mapp = list.stream().collect(Collectors.toMap(Person::getId, Function.identity(),(k1, k2) -> k1));

    7.取值去重

    List<String> targetFactoryCode = targetLocationList.stream().map(Location::getFactoryCode).distinct().collect(Collectors.toList());

    8.分组,对于list中的对象按照名字分组,返回一个map<String,List>

    Map<String,List<Person>> m = list.stream().collect(Collectors.groupingBy(l->l.getName()));

    9.分组,返回一个Map<Boolean,List>,如果分组了,就是list的size>1,则是true,否则是false

    Map<Boolean,List<Person>> m = list.stream().collect(Collectors.groupingBy(p->p.getName().equals("haha")));

    10.groupingBy()提供第二个参数,表示downstream,即对分组后的value作进一步的处理,返回map<String,Set>

    Map<String,Set<Person>> m = list.stream().collect(Collectors.groupingBy(l->l.getName(),Collectors.toSet()));

    11.分组,返回value集合中元素的数量

    Map<String,Long> m = list.stream().collect(Collectors.groupingBy(l->l.getName(),Collectors.counting()));

    12.分组,对value集合中元素求和

    Map<String,Integer> m = list.stream().collect(Collectors.groupingBy(l->l.getName(),Collectors.summingInt(Person::getId)));

    13.分组,并取value集合中某个元素最大的实体,在这里先按照name分组,然后取id最大的的实体,注意value是Optional的

    Map<String,Optional<Person>> m = list.stream().collect(Collectors.groupingBy(l->l.getName(),Collectors.maxBy(Comparator.comparing(Person::getId))));

    14.分组,并通过mapping对value字段进行处理

    Map<String,Set<Integer>> m = list.stream().collect(Collectors.groupingBy(l->l.getName(),Collectors.mapping(Person::getId,Collectors.toSet())));

     15.对于list集合的空指针问题java8的处理方式:如果list不为空,那就赋值给newList,否则重新new一个list

    public static void main(String[] args) {
    List<String> list = null;
    List<String> newList = Optional.ofNullable(list).orElse(Lists.newArrayList());
    newList.forEach(x -> System.out.println(x));
    }

     16.循环

    IntStream.range(1, 4) .forEach(System.out::println); // 相当于 for (int i = 1; i < 4; i++) {} // 1 // 2 // 3

    17.原始类型聚合操作

    Arrays.stream(new int[] {1, 2, 3})
    .map(n -> 2 * n + 1) // 对数值中的每个对象执行 2*n + 1 操作
    .average() // 求平均值
    .ifPresent(System.out::println); // 如果值不为空,则输出 // 5.0
     
    18.常规对象转为原始类型
    Stream.of("a1", "a2", "a3") .map(s -> s.substring(1)) // 对每个字符串元素从下标1位置开始截取
    .mapToInt(Integer::parseInt) // 转成 int 基础类型类型流
    .max() // 取最大值
    .ifPresent(System.out::println); // 不为空则输出 // 3
     
    19.原始类型转化为对象
    IntStream.range(1, 4) .mapToObj(i -> "a" + i) // for 循环 1->4, 拼接前缀
    a .forEach(System.out::println); // for 循环打印
    20.元素连接
    String phrase = persons .stream() .filter(p -> p.age >= 18) // 过滤出年龄大于等于18的
    .map(p -> p.name) // 提取名字
    .collect(Collectors.joining(" and ", "In Germany ", " are of legal age.")); // 以 In Germany 开头,and 连接各元素,再以 are of legal age. 结束
    System.out.println(phrase); // In Germany Max and Peter and Pamela are of legal age.
    21.流转化为map
    Map<Integer, String> map = persons .stream()
    .collect(Collectors.toMap( p -> p.age, p -> p.name, (name1, name2) -> name1 + ";" + name2)); // 对于同样 key 的,将值拼接 System.out.println(map);
    // {18=Max, 23=Peter;Pamela, 12=David}
    22.自定义收集器,不使用内置收集器Collectors
    Collector<Person, StringJoiner, String> personNameCollector = Collector.of(
    () -> new StringJoiner(" | "), // supplier 供应器
    (j, p) -> j.add(p.name.toUpperCase()), // accumulator 累加器
    (j1, j2) -> j1.merge(j2), // combiner 组合器
    StringJoiner::toString); // finisher 终止器
    String names = persons .stream() .collect(personNameCollector); // 传入自定义的收集器
    System.out.println(names); // MAX | PETER | PAMELA | DAVID
    归约
     
    23.找出年龄最大的人
    persons .stream() .reduce((p1, p2) -> p1.age > p2.age ? p1 : p2) .ifPresent(System.out::println); // Pamela
     
    24.第二种reduce方法接受标识值和BinaryOperator累加器。此方法可用于构造一个新的 Person,其中包含来自流中所有其他人的聚合名称和年龄:
    Person result = persons .stream()
    .reduce(new Person("", 0), (p1, p2) -> { p1.age += p2.age; p1.name += p2.name; return p1; });
    System.out.format("name=%s; age=%s", result.name, result.age);
    // name=MaxPeterPamelaDavid; age=76
     
    25.第三种reduce方法接受三个参数:标识值,BiFunction累加器和类型的组合器函数BinaryOperator。由于初始值的类型不一定为Person,我们可以使用这个归约函数来计算所有人的年龄总和:
    Integer ageSum = persons .stream() .reduce(0, (sum, p) -> sum += p.age, (sum1, sum2) -> sum1 + sum2);
    System.out.println(ageSum); // 76
     
     
    26.集合操作
    CollectionUtils.subList:截取list
    27.
    我不是程序员,我只是程序的搬运工
  • 相关阅读:
    阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_5_InvalidClassException异常_原理
    阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_4_transient关键字_瞬态关键字
    阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_3_对象的反序列化流_ObjectInputStream
    阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_2_对象的序列化流_ObjectOutputStream
    阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_5_InputStreamReader介绍&代码实现
    Linux启动流程
    静态库和动态库的区别
    uboot各种目录下的文件作用
    编译选项含义
    C++转换构造函数与类型转换构造函数
  • 原文地址:https://www.cnblogs.com/keith0/p/14140197.html
Copyright © 2011-2022 走看看