List<Stu> list = new ArrayList<>(); list.stream() .map(it -> it.getName()) .map(it->new StringBuilder(it)) .map(it->it.append("TTF").toString()) .forEach(it-> System.out.println(it));
优化后:
1 2 3 4 5 6
List<Stu> list = new ArrayList<>(); list.stream() .map(Stu::getName) .map(StringBuilder::new) .map(it->it.append("TTF").toString()) .forEach(System.out::println);
JAVA8新增函数式接口
Java8新增的java.util.function包定义的函数式接口。
Supplier(生产)
指定接口的泛型是什么类型,那么接口的get方法就会生产什么类型的数据。
1 2 3 4 5 6 7 8 9 10
@FunctionalInterface publicinterfaceSupplier<T> {
/** * Gets a result. * * @return a result */ T get(); // 用来获取一个泛型参数指定类型的对象数据 }
classStu{ String name; int age; long weight; double money; }
List<Stu> stus = new ArrayList<>(); Stu s1 = new Stu("bbbbb", 23, 24, 23.24); Stu s2 = new Stu("ddddd", 11, 124, 133.124); Stu s3 = new Stu("aaaaa", 43, 34, 13.14); Stu s4 = new Stu("eeeee", 13, 34, 13.14); Stu s5 = new Stu("ccccc", 33, 34, 13.14); stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); stus.add(s5);
// 最大值 Stu max = getM(()->{ return stus.stream().max((o1, o2) -> o1.getAge()-o2.getAge()).get(); }); System.out.println(max); // 最小值 Stu min = getM(()->{ return stus.stream().min((o1, o2) -> o1.getAge()-o2.getAge()).get(); }); System.out.println(min);
publicstatic Stu getM(Supplier<Stu> supplier){ return supplier.get(); }
/** * Performs this operation on the given argument. * * @param t the input argument */ voidaccept(T t);
/** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */ default Consumer<T> andThen(Consumer<? super T> after){ Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } }
accept:对给定的参数执行此操作。
andThen:返回一个组合的 Consumer ,按顺序执行该操作,然后执行 after操作。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
String[] str = {"Andy,13", "Mini,7.6", "Ak,23"}; for (String it : str) { fix(it, c1 -> { String name = it.split(",")[0]; System.out.println("姓名:" + name); }, c2 -> { String age = it.split(",")[1]; System.out.println("年龄:" + age); }); }
List<Stu> stus = new ArrayList<>(); Stu s1 = new Stu("bbbbb", 23, 24, 23.24); Stu s2 = new Stu("ddddd", 11, 124, 133.124); Stu s3 = new Stu("aaaaa", 43, 34, 13.14); Stu s4 = new Stu("eeeee", 13, 34, 13.14); Stu s5 = new Stu("ccccc", 33, 34, 13.14); stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); stus.add(s5);
for (Stu it : stus) { System.out.println(it); fix(it, c1 -> { it.setName("qqqqqqq"); }, c2 -> { it.setAge(232323); }); System.out.println(it); }
List<Stu> stus = new ArrayList<>(); Stu s1 = new Stu("bbbbb", 23, 24, 23.24); Stu s2 = new Stu("ddddd", 11, 124, 133.124); Stu s3 = new Stu("aaaaa", 43, 34, 13.14); Stu s4 = new Stu("eeeee", 13, 34, 13.14); Stu s5 = new Stu("ccccc", 33, 34, 13.14); stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); stus.add(s5);
List<Stu> list = new ArrayList<>(); for (Stu it : stus) { list.add(check(it, p -> p.getAge() < 20)); } list.forEach(System.out::println);
publicstatic Stu check(Stu stu, Predicate<Stu> p1){ List<Stu> list = new ArrayList<>(); if (p1.test(stu)) { return stu; } returnnull; }