简化编程模型-函数式接口
函数式接口
什么是函数式接口?
只有一个方法的接口
@FunctionalInterface public interface Runnable { public abstract void run(); }
函数型接口,传入一个值,返回一个值
Function接口源码:
@FunctionalInterface public interface Function<T, R> { R apply(T t); default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } static <T> Function<T, T> identity() { return t -> t; } }
测试:
public class FunctionTest { public static void main(String[] args) { // 匿名内部类 Function<String, String> function = new Function<String, String>() { @Override public String apply(String s) { return s; } }; // lambda表达式,简化 Function<String, String> function1 = (str)-> { return str; }; System.out.println(function.apply("asgd")); System.out.println(function1.apply("asddf")); } }
判定型接口,传入一个值,返回一个布尔值
Predictate源码:
@FunctionalInterface public interface Predicate<T> { boolean test(T t); default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } default Predicate<T> negate() { return (t) -> !test(t); } default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
测试:
public class PredicateTest { public static void main(String[] args) { // 匿名内部类 Predicate<Integer> predicate = new Predicate<Integer>() { @Override public boolean test(Integer integer) { return integer == 0; } }; // lambda改写 Predicate<Integer> predicate1 = (integer)-> { return integer == 0; }; System.out.println(predicate.test(1)); System.out.println(predicate1.test(0)); } }
消费型接口,只有参数,没有返回值
Consumer接口源码:
@FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } }
测试:
public class ConsumerTest { public static void main(String[] args) { // 匿名内布类 Consumer<String> consumer = new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } }; // lambda表达式 Consumer<String> consumer1 = (str)-> { System.out.println(str); }; consumer.accept("23322"); consumer1.accept("ssdgsd"); } }
供给型接口,只有返回值,没有参数
Supplier接口源码:
@FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); }
测试:
public class SupplierTest { public static void main(String[] args) { Supplier<Integer> supplier = new Supplier<Integer>() { @Override public Integer get() { return 1024; } }; Supplier<Integer> supplier1 = ()-> { return 1024; }; System.out.println(supplier.get()); System.out.println(supplier1.get()); } }
Stream流式计算
存储交给集合
计算交给Stream流
示例:
@Data @AllArgsConstructor public class User { private int id; private String name; private int age; }
public class Test { public static void main(String[] args) { User user1 = new User(1, "a", 21); User user2 = new User(2, "b", 22); User user3 = new User(3, "c", 23); User user4 = new User(4, "d", 24); User user5 = new User(6, "e", 25); List<User> list = Arrays.asList(user1, user2, user3, user4, user5); // 把list变为流 list.stream().filter((u)->{ return u.getId() % 2 == 0;}) .filter(u->{return u.getAge() > 23;}) .map(u->{return u.getName().toUpperCase();}) .sorted((uu1, uu2)-> {return uu1.compareTo(uu2);}) .limit(1) .forEach(System.out::println); } }
内容来自狂神JUC视频
#JUC#