zoukankan      html  css  js  c++  java
  • Java8学习笔记_实例1

    1)集合排序

    Collections.sort(inventory, new Comparator<Apple>() {

      public int compare(Apple a1, Apple a2){

        return a1.getWeight().compareTo(a2.getWeight());

      }

    });

    inventory.sort(comparing(Apple::getWeight));

     

    2)要筛选一个目录中的所有隐藏文件

    File[] hiddenFiles = new File(".").listFiles(new FileFilter() {

    public boolean accept(File file) {

    return file.isHidden();

    }

    });

    File[] hiddenFiles = new File(".").listFiles(File::isHidden);

     

    3)集合过滤

    仅仅选出绿苹果

    public static List<Apple> filterGreenApples(List<Apple> inventory){

    List<Apple> result = new ArrayList<>();

    for (Apple apple: inventory){

    if ("green".equals(apple.getColor())) {

    result.add(apple);

    }

    }

    return result;

    }

     

     

    List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
    new Apple(155, "green"),
    new Apple(120, "red"));

    // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
    List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
    System.out.println(greenApples);


    仅仅选出重的苹果

     

     

     

     

     

  • 相关阅读:
    PHP如何获取内网IP
    开源的世界并不纯净
    在linux下玩上了第一人称射击
    终于,在linux下上网了
    我的理想
    vista是什么
    我傻了一阵子
    又是大端小端!!!
    谈谈最近的编程状态
    如何快速发布你的C++Builder程序
  • 原文地址:https://www.cnblogs.com/mjzhang/p/7652319.html
Copyright © 2011-2022 走看看