Jjava8 Lambda 神操作

 public class Lambda {

     @FunctionalInterface
public interface AddInter {
void add(int x, long y);
} public interface AddInterTow extends AddInter {
void add(int x, long y);
} ///Predicate 传入apple类型的参数
private static List<Apple> findApple1(List<Apple> appleList, Predicate<Apple> applePredicate) {
ArrayList<Apple> newArrayList = Lists.newArrayList();
for (Apple apple : appleList) {
if (applePredicate.test(apple)) {
newArrayList.add(apple);
}
}
return newArrayList;
} ///Predicate 传入Long类型的参数 一个参数调用括号可以省略
private static List<Apple> findApple2(List<Apple> appleList, Predicate<Long> applePredicate) {
ArrayList<Apple> newArrayList = Lists.newArrayList();
for (Apple apple : appleList) {
if (applePredicate.test(apple.getWeight())) {
newArrayList.add(apple);
}
}
return newArrayList;
} ///两个参数的
private static List<Apple> findApple3(List<Apple> appleList, BiPredicate<String, Long> applePredicate) {
ArrayList<Apple> newArrayList = Lists.newArrayList();
for (Apple apple : appleList) {
if (applePredicate.test(apple.getColor(), apple.getWeight())) {
newArrayList.add(apple);
}
}
return newArrayList;
} ///一个参数的Consumer,accept方法没有返回值 具体干什么你来定
private static void findApple4(List<Apple> appleList, Consumer<Apple> appleConsumer) {
for (Apple apple : appleList) {
appleConsumer.accept(apple);
}
}
///两个参数的
private static void findApple5(String d,List<Apple> appleList, BiConsumer<Apple,String> appleConsumer) {
for (Apple apple : appleList) {
appleConsumer.accept(apple,d);
}
}
private static String findApple6(Apple apple, Function<Apple,String> stringFunction) {
return stringFunction.apply(apple);
} public static void main(String[] args) {
ArrayList<Apple> list = Lists.newArrayList(new Apple("gree", 150L), new Apple("red", 200L), new Apple("blue", 300L));
///Predicate一个参数 || BiPredicate两个参数 || IntPredicate int参数的
List<Apple> apple1 = findApple1(list, (apple) -> apple.getColor().equals("red"));
System.out.println(JSON.toJSONString(apple1));
List<Apple> apple2 = findApple2(list, num -> num > 150L);
System.out.println(JSON.toJSONString(apple2));
List<Apple> apple3 = findApple3(list, (a, b) -> a.equals("red") && b >= 200);
System.out.println(apple3);
System.out.println("==========================================>"); ///Consumer 一个参数的
findApple4(list, a -> System.out.println("{"+a.getColor()+":"+a.getWeight()+"}"));
findApple5("consumer",list,(a,b)-> System.out.println(a.getColor()+":"+a.getWeight()+":"+b));
System.out.println("=======================================>");
///function 传入一个值 返回一个值
String apple6 = findApple6(new Apple("apple", 3000L), (x) -> x.toString());
System.out.println(apple6); IntFunction<Double> apple7 = i->i*20.0d;
System.out.println(apple7.apply(20));
//Predicate || Function ||Supplier || Consumer 常用的几个类 System.out.println("===================================>推导==================");
Consumer<String> consumerString = (s -> System.out.println(s));
consumer(consumerString,"坚持");
consumer(s-> System.out.println(s),"hellword consunmer");
consumer(System.out::println,"system.out.pring"); Integer aaa = Integer.parseInt("12345"); SFunction<String, Integer> stringIntegerSFunction = Integer::parseInt;
Integer integer = stringIntegerSFunction.apply("1234");
System.out.println(integer); BiFunction<String,Long,Apple> biFunction = Apple::new;
Apple biapple = biFunction.apply("biapple", 1000L);
System.out.println(biapple);
} public static <T> void consumer(Consumer<T> consumer,T t){
consumer.accept(t);
consumer.accept(t);
}
public static void LambdaRun() {
SFunction<String, Object> stringObjectSFunction = (String s) -> s.length();
System.out.println(stringObjectSFunction.apply("大傻大傻大傻大傻")); Predicate<Apple> applePredicate = (apple -> apple.getColor().equals("red1"));
System.out.println(applePredicate.test(new Apple("red", 123L))); Supplier<Apple> appleSupplier = Apple::new;
System.out.println(appleSupplier.get()); Runnable run = new Runnable() {
@Override
public void run() {
System.out.println("hello word");
}
};
Runnable runnable = () -> System.out.println("hello word");
startRunnable(runnable);
startRunnable(run);
startRunnable(() -> System.out.println("hello"));
} public static void startRunnable(Runnable runnable) {
runnable.run();
}
}
上一篇:SecureCRT 下MySQL中文乱码问题终极解决方案-乾颐堂


下一篇:jsp中文乱码六种情况---解决方案