This article brings you a summary (code examples) of commonly used built-in functions in Java 8. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Commonly used function interfaces are recorded for easy reference later
Interface | Parameters | Return type | Description |
---|---|---|---|
T | boolean | Input a certain value and output a boolean value, which is used to determine a certain value | |
T | void | Enter a certain value, no output. Used to consume a certain value | |
T | R | Input a certain type of value and output another A type value, used for type conversion, etc. | |
None | T | No input, output a certain Value, used to produce a certain value | |
T | T | Input a certain type of value and output the same Type value, used for same-type conversion of values, such as performing four arithmetic operations on values, etc. | |
(T,T) | T | Input two values of a certain type and output a value of the same type, used for merging two values, etc. |
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
Function<String, Integer> toInteger = Integer::valueOf; Function<String, String> backToString = toInteger.andThen(String::valueOf); backToString.apply("123"); // "123"
Suppliers
Supplier<Person> personSupplier = Person::new; personSupplier.get(); // new Person
Consumers
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); greeter.accept(new Person("Luke", "Skywalker"));
Comparators
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
Common methods of Stream
Stream<Integer> s = Stream.of(1, 2, 3, 4, 5); Stream<Integer> s = Arrays.stream(arr); Stream<Integer> s = aList.stream();
// 这种方法通常表示无限序列 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)) ...
<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
The flatMap method is a one-to-many mapping. Each element is mapped to a Stream, and then the elements of this child Stream are mapped to the parent collection:
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 method
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 and skip
sorted
distinct
findFirst
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
max can find the maximum value of the integer stream and return OptionalInt.
These two methods are end operations and can only be called once.
allMatch, anyMatch, noneMatch
anyMatch: As long as there is one element in the Stream that matches the incoming predicate, it returns true
noneMatch: None of the elements in the Stream matches the incoming predicate and returns true
reduce method
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);
The above is the detailed content of Summary of commonly used built-in functions in java8 (code examples). For more information, please follow other related articles on the PHP Chinese website!