import java.util.Comparator;
import java.util.function.BinaryOperator; public class BinaryOperatorTest { public static void main(String[] args) {
BinaryOperatorTest binaryOperatorTest = new BinaryOperatorTest(); System.out.println(binaryOperatorTest.compute(1, 2, (a, b) -> a + b));
System.out.println(binaryOperatorTest.compute(1, 2, (a, b) -> a - b)); System.out.println("----------"); System.out.println(binaryOperatorTest.getMax("hello123", "world", (a, b) -> a.length() - b.length()));
System.out.println(binaryOperatorTest.getMax("hello123", "world", (a, b) -> a.charAt(0) - b.charAt(0))); } public int compute(int a, int b, BinaryOperator<Integer> binaryOperator) {
return binaryOperator.apply(a, b);
} public String getMax(String a, String b, Comparator<String> comparator) {
return BinaryOperator.maxBy(comparator).apply(a, b);
}
}
import java.util.List;

public class Company {

    private String name;

    private List<Employee> employees;

    public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<Employee> getEmployees() {
return employees;
} public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
public class Employee {

    private String name;

    public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
import java.util.function.Function;

public class FunctionTest {

    public static void main(String[] args) {
FunctionTest test = new FunctionTest(); System.out.println(test.compute(1, value -> {
return 2 * value;
})); System.out.println(test.compute(2, value -> 5 + value));
System.out.println(test.compute(3, value -> value * value)); System.out.println(test.convert(5, value -> String.valueOf(value + " hello world"))); System.out.println(test.method1(2)); Function<Integer, Integer> function = value -> value * 2; System.out.println(test.compute(4, function));
} public int compute(int a, Function<Integer, Integer> function) {
int result = function.apply(a); return result;
} public String convert(int a, Function<Integer, String> function) {
return function.apply(a);
} public int method1(int a) {
return 2 * a;
}
}
import java.util.function.BiFunction;
import java.util.function.Function; public class FunctionTest2 { public static void main(String[] args) {
FunctionTest2 test = new FunctionTest2(); System.out.println(test.compute(2, value -> value * 3, value -> value * value)); //
System.out.println(test.compute2(2, value -> value * 3, value -> value * value)); // System.out.println(test.compute3(1, 2, (value1, value2) -> value1 + value2));
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 - value2));
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 * value2));
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 / value2)); System.out.println(test.compute4(2, 3, (value1, value2) -> value1 + value2, value -> value * value)); //
} public int compute(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).apply(a);
} public int compute2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.andThen(function2).apply(a);
} public int compute3(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(a, b);
} public int compute4(int a, int b, BiFunction<Integer, Integer, Integer> biFunction,
Function<Integer, Integer> function) {
return biFunction.andThen(function).apply(a, b);
}
}
import java.util.Optional;

public class OptionalTest {

    public static void main(String[] args) {
Optional<String> optional = Optional.ofNullable("hello"); // if(optional.isPresent()) {
// System.out.println(optional.get());
// } // optional.ifPresent(item -> System.out.println(item)); //推荐的Optional使用方式
// System.out.println("-------"); optional = Optional.empty(); // System.out.println(optional.orElse("world"));
// System.out.println("---------"); optional = Optional.empty(); System.out.println(optional.orElseGet(() -> "nihao"));
}
}
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional; public class OptionalTest2 { public static void main(String[] args) {
Employee employee = new Employee();
employee.setName("zhangsan"); Employee employee2 = new Employee();
employee2.setName("lisi"); Company company = new Company();
company.setName("company1"); List<Employee> employees = Arrays.asList(employee, employee2);
// company.setEmployees(employees); Optional<Company> optional = Optional.ofNullable(company); System.out.println(optional.map(theCompany -> theCompany.getEmployees()).
orElse(Collections.emptyList()));
}
}
import java.util.function.Predicate;

public class PredicateTest {

    public static void main(String[] args) {

        Predicate<String> predicate = p -> p.length() > 5;

        System.out.println(predicate.test("hello1"));
}
}
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate; public class PredicateTest2 { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); PredicateTest2 predicateTest2 = new PredicateTest2(); predicateTest2.conditionFilter(list, item -> item % 2 == 0);
System.out.println("---------"); predicateTest2.conditionFilter(list, item -> item % 2 != 0);
System.out.println("---------"); predicateTest2.conditionFilter(list, item -> item > 5);
System.out.println("---------"); predicateTest2.conditionFilter(list, item -> item < 3);
System.out.println("---------"); predicateTest2.conditionFilter(list, item -> true);
System.out.println("---------"); predicateTest2.conditionFilter(list, item -> false);
System.out.println("---------"); predicateTest2.conditionFilter2(list, item -> item > 5, item -> item % 2 == 0);
System.out.println("---------"); Date d = new Date(); System.out.println(predicateTest2.isEqual(d).test(d));
} public void conditionFilter(List<Integer> list, Predicate<Integer> predicate) {
for(Integer integer : list) {
if(predicate.test(integer)) {
System.out.println(integer);
}
}
} public void conditionFilter2(List<Integer> list, Predicate<Integer> predicate,
Predicate<Integer> predicate2) {
for(Integer integer : list) {
if(predicate.and(predicate2).negate().test(integer)) {
System.out.println(integer);
}
}
} public Predicate<Date> isEqual(Object object) {
return Predicate.isEqual(object);
} // 寻找所有偶数,传统方式
public void findAllEvens(List<Integer> list) {
for(Integer integer : list) {
if(integer % 2 == 0) {
System.out.println(integer);
}
}
}
}
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class StringComparator { public static void main(String[] args) { List<String> names = Arrays.asList("zhangsan", "lisi", "wangwu", "zhaoliu"); // Collections.sort(names, new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// return o2.compareTo(o1);
// }
// });
//
// System.out.println(names); // expression o2.compareTo(o1)
// statement {return o2.compareTo(o1);} // Collections.sort(names, (o1, o2) -> { return o2.compareTo(o1); }); Collections.sort(names, (o1, o2) -> o2.compareTo(o1)); System.out.println(names);
}
}
public class Student {

    private String name = "zhangsan";

    private int age = 20;

    public Student() {

    }

    public Student(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
import java.util.function.Supplier;

public class StudentTest {

    public static void main(String[] args) {
// Supplier<Student> supplier = () -> new Student();
// System.out.println(supplier.get().getName());
//
// System.out.println("-------"); Supplier<Student> supplier2 = Student::new;
System.out.println(supplier2.get().getName());
}
}
import java.util.function.Supplier;

public class SupplierTest {

    public static void main(String[] args) {
Supplier<String> supplier = () -> "hello world";
System.out.println(supplier.get());
}
}
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer; public class Test1 { public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
} System.out.println("---------------"); for(Integer i : list) {
System.out.println(i);
} System.out.println("---------------"); for(Iterator<Integer> iter = list.iterator(); iter.hasNext();) {
System.out.println(iter.next());
} System.out.println("---------------"); list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(integer);
}
}); System.out.println("---------------"); list.forEach((Integer i) -> System.out.println(i)); System.out.println("---------------"); list.forEach(i -> System.out.println(i)); System.out.println("---------------"); list.forEach(System.out::println); Consumer<Integer> consumer = i -> System.out.println(i); Runnable r = () -> {}; new Thread(() -> {
System.out.println("hello world");
}).start(); new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello world2");
}
}).start();
}
}
@FunctionalInterface
interface MyInterface { void test(); // 取消如下方法注释,该接口是否还是函数式接口
// String toString();
} public class Test2 { public void myTest(MyInterface myInterface) {
System.out.println(1);
myInterface.test();
System.out.println(2);
} public static void main(String[] args) {
Test2 test2 = new Test2(); test2.myTest(() -> {
System.out.println("mytest");
}); System.out.println("----------"); MyInterface myInterface = () -> {
System.out.println("hello");
}; System.out.println(myInterface.getClass());
System.out.println(myInterface.getClass().getSuperclass());
System.out.println(myInterface.getClass().getInterfaces()[0]);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function; public class Test3 { public static void main(String[] args) { // TheInterface i1 = () -> {};
// System.out.println(i1.getClass().getInterfaces()[0]);
//
// TheInterface2 i2 = () -> {};
// System.out.println(i2.getClass().getInterfaces()[0]); // new Thread(() -> System.out.println("hello world")).start();
//
List<String> list = Arrays.asList("hello", "world", "hello world"); // list.forEach(item -> System.out.println(item.toUpperCase()));
//
List<String> list2 = new ArrayList(); // list.forEach(item -> list2.add(item.toUpperCase()));
// list2.forEach(item -> System.out.println(item));
//
// list.stream().map(item -> item.toUpperCase()).forEach(item -> System.out.println(item)); // list.stream().map(String::toUpperCase).forEach(item -> System.out.println(item));
//
Function<String, String> function = String::toUpperCase;
System.out.println(function.getClass().getInterfaces()[0]);
}
} @FunctionalInterface
interface TheInterface { void myMethod();
} @FunctionalInterface
interface TheInterface2 { void myMethod2();
}

stream4的更多相关文章

  1. snort使用

    http://jingyan.baidu.com/article/d8072ac45a626fec95cefd85.html 接上篇,如果编译安装snort并指定了prefix,那么须指定一个软链接, ...

  2. Java 8怎么了之二:函数和原语

    [编者按]本文作者为专注于自然语言处理多年的 Pierre-Yves Saumont,Pierre-Yves 著有30多本主讲 Java 软件开发的书籍,自2008开始供职于 Alcatel-Luce ...

  3. 复合文档的二进制存储格式研究[ole存储结构](word,xls,ppt...)[转]

    复合文档文件格式研究   前 言 复合文档(Compound Document) 是一种不仅包含文本而且包括图形.电子表格数据.声音.视频图象以及其它信息的文档.可以把复合文档想象成一个所有者,它装着 ...

  4. RFC2889转发性能測试用例设计和自己主动化脚本实现

    一.203_TC_FrameRate-1.tcl set chassisAddr 10.132.238.190 set islot 1 set portList {9 10} ;#端口的排列顺序是po ...

  5. 流API--流的基础知识

    流接口--BaseStream接口 流API定义了几个流接口,这些接口包含在java.util.stream中.BaseStream是基础接口,它定义了所有流都可以使用的基本功能.我们来看一下源码: ...

  6. 流API--初体验

    在JDK8新增的许多功能中,有2个功能最重要,一个是Lambda表达式,一个是流API.Lambda表达式前面我已经整理过了,现在开始整理流API.首先应该如何定义流API中的"流" ...

  7. java8完全解读二

    继续着上次的java完全解读一 继续着上次的java完全解读一1.强大的Stream API1.1什么是Stream1.2 Stream操作的三大步骤1.2.1 创建Stream1.2.2 Strea ...

  8. QUIC协议原理分析(转)

    之前深入了解了一下HTTP1.1.2.0.SPDY等协议,发现HTTP层怎么优化,始终要面对TCP本身的问题.于是了解到了QUIC,这里分享一篇之前找到的有意义的文章. 原创地址:https://mp ...

  9. Java 8 Stream介绍及使用1

    (原) stream的内容比较多,先简单看一下它的说明: A sequence of elements supporting sequential and parallel aggregate * o ...

随机推荐

  1. 编写高质量代码改善C#程序的157个建议——建议33:避免在泛型类型中声明静态成员

    建议33:避免在泛型类型中声明静态成员 在上一建议中,已经理解了应该将MyList<int>和MyList<string>视作两个完全不同的类型,所以,不应该将MyList&l ...

  2. (转)Linux环境进程间通信系列(五):共享内存

    原文地址:http://www.cppblog.com/mydriverc/articles/29741.html 共享内存可以说是最有用的进程间通信方式,也是最快的 IPC 形式.两个不同进程 A ...

  3. React学习笔记4

    遇到的问题 目前模板是自己任意定义的,样式不好控制 在组件设计时,可以把页面数据显示的地方,分割父子组件嵌套的结构,比如,商品数据显示列表,把组外层容器看成是父组件,里面是数据显示的渲染模板,看成是子 ...

  4. 做ETL的时候用到的数据同步更新代码

    这里是用的从一个库同步到另一个库,代码如下 private void IncrementalSyncUpdate(string fromConn, string toConn, Dictionary& ...

  5. MSSQL中数据库对象类型解释

    public string GetObjectTypeName(object oType) { switch (oType+"") { case "U": re ...

  6. SOA IN Real World

    微软发布了一个名为“真实世界里的面向服务架构(SOA)”的电子书.这本书表达了微软对面向服务架构的观点,并包括了数个展示如何用微软产品和技术实现SOA的真实案例.书中解释到,SOA的功能型架构本身是松 ...

  7. arp欺骗进行流量截获-1

    这边博文主要讲一下怎么使用arp欺骗进行流量截获,主要用于已经攻入内网以后,进行流量监听以及修改. 一.什么是arp     arp协议是以太网的基础工作协议,其主要作用是是一种将IP地址转化成物理地 ...

  8. 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

    Problem surface 戳我 Meaning 坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 这道题就是一道凸包的模板题啊,只要求出凸包后在计算就好了,给出几个注意点 记得检查是否有吧 ...

  9. Selenium辅助工具

    下载Firefox39.0版本浏览器,安装firebug和FirePath.最新版的Firefox在扩展组件中无法找到firebug,可以使用旧的版本的Firefox浏览器. FirePath插件的使 ...

  10. (转)VS2010实用快捷键

    1,Visual Studio 2008自带的1000多个 Windows 系统使用的各种图标.光标和动画文件在Visual Studio 2008的安装目录下,/Microsoft Visual S ...