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();
}

最新文章

  1. 【JavaScript】--重点解析之跨域请求
  2. Ubuntu设置环境变量 16.04
  3. cron表达式详解
  4. 【leetcode】Copy List with Random Pointer (hard)
  5. 【Android开发日记】Popupwindow 完美demo
  6. 更新jar包中的MANIFEST.MF
  7. IOS 应用生命周期
  8. Android Application Project 工程目录下各个文件的意思
  9. pycharm的使用技巧
  10. NodeAsp——像开发NodeJS应用一样玩转ASP
  11. [转]eclipse下编写android程序突然不会自动生成R.java文件和包的解决办法
  12. Maven搭建springMVC+spring+hibernate环境
  13. Python系列之内置函数
  14. Java Web开发中路径问题小结
  15. postgresql 触发器 更新操作
  16. Microsoft.AspNet.Identity: UserID用整型数据表示, 而不是GUID
  17. Spring 学习笔记(十)渲染 Web 视图 (Apache Tilesa 和 Thymeleaf)
  18. MySQL 聚簇索引&amp;&amp;二级索引&amp;&amp;辅助索引
  19. Intellij下Jquery中文乱码
  20. powerdesiner技巧

热门文章

  1. 很好的QSqlDatabase问题说明,关于连接错误(转)
  2. C# 操作Excel基础篇(读取Excel、写入Excel)
  3. URAL 1204. Idempotents (扩展欧几里得)
  4. 命令(Command)模式
  5. 15分XX秒后订单自动关闭(倒计时)
  6. 用JS实现表格的高亮显示
  7. Sharepoint2013搜索学习笔记之自定义结果显示模板(九)
  8. 禁用 C# 编译器对某段代码的警告
  9. C#开源网络通信库PESocket的使用
  10. Ext.Net安装和应用