Java8学习笔记_实例1
Posted 轨道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
仅仅选出重的苹果
以上是关于Java8学习笔记_实例1的主要内容,如果未能解决你的问题,请参考以下文章