码迷,mamicode.com
首页 > 编程语言 > 详细

Java8学习笔记_实例1

时间:2017-10-11 20:38:28      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:目录   pre   accept   public   style   filter   arrays   app   getc   

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);


仅仅选出重的苹果

 

 

 

 

 

Java8学习笔记_实例1

标签:目录   pre   accept   public   style   filter   arrays   app   getc   

原文地址:http://www.cnblogs.com/mjzhang/p/7652319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!