经常运用的函数接口纪录轻易今后翻吧
接口 | 参数 | 返回范例 | 申明 |
---|---|---|---|
Predicate<T> | T | boolean | 输入某个值,输出boolean 值,用于对某值举行剖断 |
Consumer<T> | T | void | 输入某值,无输出。用于消耗某值 |
Function<T,R> | T | R | 输入某范例值,输出另种范例值,用于范例转化等 |
Supplier<T> | None | T | 无输入,输出某值,用于生产某值 |
UnaryOperator<T> | T | T | 输入某范例值,输出同范例值,用于值的同范例转化,如对值举行四则运算等 |
BinaryOperator<T> | (T,T) | T | 输入两个某范例值,输出一个同范例值,用于两值兼并等 |
Predicates
Predicates是包含一个参数的布尔值接口。其包含一些缺省要领,组合他们运用能够完成庞杂的营业逻辑(如:and, or, negate)。示例代码以下:
Predicate<String> predicate = (s) -> s.length() > 0; predicate.test("foo"); // true predicate.negate().test("foo"); // false Predicate<Boolean> nonNull = Objects::nonNull; Predicate<Boolean> isNull = Objects::isNull; Predicate<String> isEmpty = String::isEmpty; Predicate<String> isNotEmpty = isEmpty.negate();
Functions
Functions接口吸收一个参数并发生一个效果。其缺省要领一般被用来链接多个功用一同运用 (compose, andThen)。
Function<String, Integer> toInteger = Integer::valueOf; Function<String, String> backToString = toInteger.andThen(String::valueOf); backToString.apply("123"); // "123"
Suppliers
Suppliers接口生成一个给定范例效果。和Functions差别,其没有吸收参数。
Supplier<Person> personSupplier = Person::new; personSupplier.get(); // new Person
Consumers
Consumers表现实行带有单个输入参数的操纵。
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); greeter.accept(new Person("Luke", "Skywalker"));
Comparators
Comparators是从java旧版本升级并增加了一些缺省要领。
Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); Person p1 = new Person("John", "Doe"); Person p2 = new Person("Alice", "Wonderland"); comparator.compare(p1, p2); // > 0 comparator.reversed().compare(p1, p2); // < 0
Stream 经常运用要领
建立Stream
- 将现有数据结构转化成Stream
Stream<Integer> s = Stream.of(1, 2, 3, 4, 5); Stream<Integer> s = Arrays.stream(arr); Stream<Integer> s = aList.stream();
- 经由过程Stream.generate()要领:
// 这类要领一般示意无穷序列 Stream<T> s = Stream.generate(SuppLier<T> s); // 建立悉数自然数的Stream class NatualSupplier implements Supplier<BigInteger> { BigInteger next = BigInteger.ZERO; @Override public BigInteger get() { next = next.add(BigInteger.ONE); return next; } }
- 经由过程其他要领返回
Stream<String> lines = Files.lines(Path.get(filename)) ...
map要领
把一种操纵运算映照到Stream的每个元素上,从而完成一个Stream到另一个Stream的转换
map要领接收的对象是Function接口,这个接口是一个函数式接口:
<R> Stream<R> map(Function<? super T, ? extends R> mapper); @FunctionalInterface public interface Function<T, R> { // 将T转换为R R apply(T t); }
运用:
// 猎取Stream里每个数的平方的鸠合 Stream<Integer> ns = Stream.of(1, 2, 3, 4, 5); ns.map(n -> n * n).forEach(System.out::println);
flatMap
map要领是一个一对一的映照,每输入一个数据也只会输出一个值。
flatMap要领是一对多的映照,对每个元素映照出来的仍旧是一个Stream,然后会将这个子Stream的元素映照到父鸠合中:
Stream<List<Integer>> inputStream = Stream.of(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6)); List<Integer> integerList = inputStream.flatMap((childList) -> childList.stream()).collect(Collectors.toList()); //将一个“二维数组”flat为“一维数组” integerList.forEach(System.out::println);
filter要领
filter要领用于过滤Stream中的元素,并用相符前提的元素生成一个新的Stream。
filter要领接收的参数是Predicate接口对象,这个接口是一个函数式接口:
Stream<T> filter(Predicate<? super T>) predicate; @FunctionInterface public interface Predicate<T> { // 推断元素是不是相符前提 boolean test(T t); }
运用
// 猎取当前Stream一切偶数的序列 Stream<Integer> ns = Stream.of(1, 2, 3, 4, 5); ns.filter(n -> n % 2 == 0).forEach(System.out::println);
limit、skip
limit用于限定猎取多少个效果,与数据库中的limit作用相似,skip用于消除前多少个效果。
sorted
sorted函数须要传入一个完成Comparator函数式接口的对象,该接口的笼统要领compare吸收两个参数并返回一个整型值,作用就是排序,与其他罕见排序要领一致。
distinct
distinct用于剔除反复,与数据库中的distinct用法一致。
findFirst
findFirst要领老是返回第一个元素,假如没有则返回空,它的返回值范例是Optional<T>范例,打仗过swift的同砚应当晓得,这是一个可选范例,假如有第一个元素则Optional范例中保留的有值,假如没有第一个元素则该范例为空。
Stream<User> stream = users.stream(); Optional<String> userID = stream.filter(User::isVip).sorted((t1, t2) -> t2.getBalance() - t1.getBalance()).limit(3).map(User::getUserID).findFirst(); userID.ifPresent(uid -> System.out.println("Exists"));
min、max
min能够对整型流求最小值,返回OptionalInt。
max能够对整型流求最大值,返回OptionalInt。
这两个要领是完毕操纵,只能挪用一次。
allMatch、anyMatch、noneMatch
allMatch:Stream中悉数元素相符传入的predicate返回 true
anyMatch:Stream中只需有一个元素相符传入的predicate返回 true
noneMatch:Stream中没有一个元素相符传入的predicate返回 true
reduce要领
reduce要领将一个Stream的每个元素一次作用于BiFunction,并将效果兼并。
reduce要领接收的要领是BinaryOperator接口对象。
Optional<T> reduce(BinaryOperator<T> accumulator); @FuncationalInterface public interface BinaryOperator<T> extends BiFunction<T, T, T> { // Bi操纵,两个输入,一个输出 T apply(T t, T u); }
运用:
// 求当前Stream累乘的效果 Stream<Integer> ns = Stream.of(1, 2, 3, 4, 5); int r = ns.reduce( (x, y) -> x * y ).get(); System.out.println(r);
以上就是java8经常运用内置函数的总结(代码示例)的细致内容,更多请关注ki4网别的相干文章!