暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Java新特性解读JDK8之流操作max和min函数

227decision 2020-02-04
3569

max函数

相关源码:

    /**
    *Returns the maximum element of this stream according to the provided
    *即返回最大元素
    */
    Optional<T> max(Comparator<? super T> comparator);


    /**
    * Comparator是一个函数式接口
    */
    @FunctionalInterface
    public interface Comparator<T> {
        int compare(T o1, T o2);
    }
    复制

    max函数使用,代码如下:

      package com.example.streamdemo;


      import java.util.Arrays;
      import java.util.List;
      import java.util.Optional;


      public class MaxDemo {
      public static void main(String[] args) {
      List<Integer> list = Arrays.asList(1,2,3);
      Optional<Integer> optional = list.stream().max((a,b)->Integer.compare(a, b));
      System.out.println("max: "+optional.get());
      Optional<Integer> optional2 = list.stream().min((a,b)->Integer.compare(a, b));
      System.out.println("min: "+optional2.get());
        }
      }


      复制

      min函数

      同max函数

      最后修改时间:2020-07-01 13:35:48
      文章转载自227decision,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论