从苹果筛选的实现过程来一步一步的熟悉Java8语法
package top
.hengshare
.interviewer
.java8
.lambda
;
import com
.google
.common
.collect
.Lists
;
import lombok
.Data
;
import java
.util
.ArrayList
;
import java
.util
.Collection
;
import java
.util
.Comparator
;
import java
.util
.List
;
import java
.util
.function
.Predicate
;
import static com
.google
.common
.collect
.Collections2
.filter
;
@Data
public class Apple {
public Apple(String color
) {
this.color
= color
;
}
private String color
;
private double weight
;
public static List
<Apple> filterGreenApples(List
<Apple> inventory
) {
List
<Apple> result
= new ArrayList<Apple>();
for(Apple apple
: inventory
){
if( "green".equals(apple
.getColor()) ){
result
.add(apple
);
}
}
return result
;
}
public static boolean isGreenApple(Apple apple
) {
return "green".equals(apple
.getColor());
}
static List
<Apple> filterApples(List
<Apple> inventory
, Predicate
<Apple> p
) {
ArrayList
<Apple> result
= Lists
.newArrayList();
for (Apple apple
: inventory
) {
if (p
.test(apple
)) {
result
.add(apple
);
}
}
return result
;
}
public static List
<Apple> filterApplesByColor(List
<Apple> inventory
, String color
) {
List
<Apple> result
= new ArrayList<Apple>();
for (Apple apple
: inventory
){
if ( apple
.getColor().equals(color
) ) {
result
.add(apple
);
}
}
return result
;
}
public static List
<Apple> filterApples(List
<Apple> inventory
, String color
,
double weight
, boolean flag
) {
List
<Apple> result
= new ArrayList<Apple>();
for (Apple apple
: inventory
){
if ( (flag
&& apple
.getColor().equals(color
)) ||
(!flag
&& apple
.getWeight() > weight
) ){
result
.add(apple
);
}
}
return result
;
}
public static void main(String
[] args
) {
List
<Apple> appleList
= Lists
.newArrayList();
Apple green
= new Apple("green");
green
.setWeight(2D);
appleList
.add(green
);
Apple red
= new Apple("red");
red
.setWeight(3D);
appleList
.add(red
);
System
.out
.println(appleList
);
List
<Apple> list1
= Apple
.filterGreenApples(appleList
);
System
.out
.println(list1
);
List
<Apple> list2
= Apple
.filterApplesByColor(appleList
, "green");
System
.out
.println(list2
);
List
<Apple> list3
= Apple
.filterApplesByWeight(list2
, 2D);
System
.out
.println(list3
);
List
<Apple> list4
= Apple
.filterApples(appleList
, "green", 2D, false);
System
.out
.println(list4
);
List
<Apple> apples
= Apple
.filterApples(appleList
, Apple
::isGreenApple
);
System
.out
.println(apples
);
List
<Apple> list6
= Apple
.filterApples(appleList
, (Apple apple
) -> "red".equals(apple
.getColor()));
System
.out
.println(list6
);
Collection
<Apple> list7
= filter(appleList
, (Apple apple
) -> "red".equals(apple
.getColor()));
System
.out
.println(list7
);
appleList
.sort(new Comparator<Apple>() {
@Override
public int compare(Apple o1
, Apple o2
) {
return o1
.getColor().compareTo(o2
.getColor());
}
});
System
.out
.println(appleList
);
appleList
.sort((Apple a1
, Apple a2
) -> a2
.getColor().compareTo(a1
.getColor()));
System
.out
.println(appleList
);
}
public static List
<Apple> filterApplesByWeight(List
<Apple> inventory
, double weight
) {
List
<Apple> result
= new ArrayList<Apple>();
for (Apple apple
: inventory
){
if ( apple
.getWeight() > weight
){
result
.add(apple
);
}
}
return result
;
}
}
转载请注明原文地址: https://win8.8miu.com/read-11396.html