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();
}
}
Jjava8 Lambda 神操作的更多相关文章
- 可以让你神操作的手机APP推荐 个个都是爆款系列
手机在我们的生活中显得日益重要,根据手机依赖度调查显示,69%的人出门时必带手机,20%的人经常在吃饭睡觉.上卫生间时使用手机:43%的人早上起床第一件事就是查看手机,不用多说,我们对于手机的依赖性越 ...
- 《手把手教你》系列进阶篇之4-python+ selenium自动化测试 - python几种超神操作你都知道吗?(详细教程)
1. 简介 今天分享和讲解的超神操作,对于菜鸟来说是超神的操作,对于大佬来说也就是几个简单方法的封装和调用.这里讲解和分享这部分主要是为了培养小伙伴们和童鞋们的面向对象的开发思维,对比这样做的好处让你 ...
- python神操作将list拉平
python 神操作 将list 拉平 list_of_lists = [[1], [2, 3], [4, 5, 6]]sum(list_of_lists, [])
- 在Python中使用lambda高效操作列表的教程
在Python中使用lambda高效操作列表的教程 这篇文章主要介绍了在Python中使用lambda高效操作列表的教程,结合了包括map.filter.reduce.sorted等函数,需要的朋友可 ...
- JDK新特性-Lambda表达式的神操作
一.Lambda表达式的介绍 Lambda表达式是 Java8 中最重要的新功能之一.使用 Lambda 表达 式可以替代只有一个抽象函数的接口实现,告别匿名内部类,代码看 起来更简洁易懂.Lambd ...
- Java8新特性 1——利用流和Lambda表达式操作集合
Java8中可以用简洁的代码来操作集合,比如List,Map,他们的实现ArrayList.以此来实现Java8的充分利用CPU的目标. 流和Lambda表达式都是Java8中的新特性.流可以实现对集 ...
- Java自学-Lambda 聚合操作
java 集合的聚合操作 步骤 1 : 传统方式与聚合操作方式遍历数据 遍历数据的传统方式就是使用for循环,然后条件判断,最后打印出满足条件的数据 for (Hero h : heros) { if ...
- json和字符串/数组/集合的互相转换の神操作总结
一:前端字符串转JSON的4种方式 1,eval方式解析,恐怕这是最早的解析方式了. function strToJson(str){ var json = eval('(' + str + ')') ...
- 用Lambda表达式操作List集合
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
随机推荐
- HDU 1171 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=1171 基础的01背包,求出总值sum,背包体积即为sum/2 #include<stdio.h> # ...
- Docker环境安装与配置
Docker 简介 Docker使用Go语言编写的 安装Docker推荐LInux内核在3.10上 在2.6内核下运行较卡(CentOS 7.X以上内核是3.10) Docker 安装 安装yum-u ...
- Struts2下载及简介
Struts2下载及简介 一.Struts2下载: 进入网址:http://struts.apache.org/download.cgi#struts23163 可以下载最新的Struts2. 1.F ...
- Linux合上笔记本不进入休眠模式
最近一个问题困扰了我很久,入职之前和人事说过工作中会用自己的电脑,但是人事还是坚持要给我发一个电脑,没办法,公司没有补贴,那就领了吧,索性将这个笔记本配置成了Fedora系统,用来当测试机,但是一 ...
- HaProxy 负载均衡集群
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理,特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.H ...
- what does GIT PUSH do exactly?
有效理解 git 的对象模型 https://stackoverflow.com/questions/26005031/what-does-git-push-do-exactly 所谓分支, 就是一个 ...
- Struts2再爆远程命令执行漏洞![W3bSafe]Struts2-048 Poc Shell及防御修复方案抢先看!
漏洞概述 Apache Struts是美国阿帕奇(Apache)软件基金会负责维护的一个开源项目,是一套用于创建企业级Java Web应用的开源MVC框架.在Struts 2.3.x 系列的 Show ...
- 分布式任务调度系统xxl-job源码探究(一、客户端)
前面讲了xxl-job的搭建,现在来粗略的解析下该分布式调度系统的源码,先来客户点代码 客户端源码 客户端开启的时候会向服务中心进行注册,其实现用的是jetty连接,且每隔半分钟会发送一次心跳,来告诉 ...
- 谈谈我们对userAgent的看法,为什么爬虫中需要userAgent?
首先打开浏览器,按 F12 进入控制台(Console),然后输入:navigator.userAgent,即可看到 UA.例如: 1 2 Mozilla/5.0 (Windows NT 10.0; ...
- 撰写一篇博客要求讲述四则运算2的设计思想,源程序代码、运行结果截图、编程总结分析,并按照PSP0级的要求记录开发过程中的时间记录日志。
一.撰写一篇博客要求讲述四则运算2的设计思想,源程序代码.运行结果截图.编程总结分析,并按照PSP0级的要求记录开发过程中的时间记录日志. 1.设计思想: ①创建test.jsp建立第一个前端界面,提 ...