method-reference

Multiple lambda method references

跟風遠走 提交于 2019-11-28 04:58:32
问题 It's possible to chain/concatenate what is done with elements in a lambda expression like this: list.forEach(s -> { System.out.println(s.toLowerCase()); System.out.println(s.toUpperCase()); }); Is there a way to do this also with method references? Something like this: list.forEach({ System.out::println(String::toLowerCase); System.out::println(String::toCase); }); I know I could do this in four separate calls (which do also more, that is mutate the values): list.replaceAll(String:

Invoking toString via method reference in Java 8

故事扮演 提交于 2019-11-28 03:19:56
问题 What am I missing? Why do I have to use Object::toString below and not Integer::toString ? Does it have anything to do with type erasure with generics? Arrays.asList(1,2,3).stream().map(Integer::toString).forEach(System.out::println); //Won't compile Arrays.asList(1,2,3).stream().map(Object::toString).forEach(System.out::println); //Compiles and runs fine 回答1: This has nothing to do with type erasure. Look at the error message : (argument mismatch; invalid method reference reference to

Does a method reference in Java 8 have a concrete type and if so, what is it? [duplicate]

余生颓废 提交于 2019-11-28 01:47:40
问题 This question already has an answer here: How to indirectly run a method reference in Java 8? 2 answers This question is pretty closely related to another one. However, I feel like the accepted answer to that question is not quite as definitive. So, what is the type of a method reference in Java 8? Here's a little demonstration of how a method reference can be "cast" (lifted?) into a java.util.function.Function : package java8.lambda; import java.util.function.Function; public class Question

Is it possible to convert method reference to MethodHandle?

给你一囗甜甜゛ 提交于 2019-11-27 23:54:52
Is it possible to convert a method reference (e.g. SomeClass::someMethod ) to a MethodHandle instance? I want the benefits of compile-time checking (ensuring that the class and method exists) as well as the ability to introspect the method using the MethodHandle API. Use-case: I've got code that needs to execute if and only if the request was not triggered by a specific method (to avoid endless recursion). I want a compile-time check to ensure the class/method exists but a runtime check to compare the caller to the method. So to recap: Is it possible to convert a method reference to a

Is there any difference between Objects::nonNull and x -> x != null?

一曲冷凌霜 提交于 2019-11-27 22:10:45
问题 Consider the following class: import java.util.Objects; import java.util.function.Predicate; public class LambdaVsMethodRef { public static void main(String[] args) { Predicate<Object> a = Objects::nonNull; Predicate<Object> b = x -> x != null; } } The first predicate is created from a method reference and the other a lambda expression. These predicates have the same behavior ( nonNull 's body is just return obj != null; ). The lambda is two characters shorter (perhaps allowing a stream

Java8 method reference used as Function object to combine functions

喜夏-厌秋 提交于 2019-11-27 21:11:42
问题 Is there a way in Java8 to use a method reference as a Function object to use its methods, something like: Stream.of("ciao", "hola", "hello") .map(String::length.andThen(n -> n * 2)) This question is not related to the Stream , it is used just as example, I would like to have answer about the method reference 回答1: You can write a static method to do this: import java.util.function.*; class Test { public static void main(String[] args) { Function<String, Integer> function = combine(String:

How do Java 8 array constructor references work?

亡梦爱人 提交于 2019-11-27 12:43:04
问题 Say we have a variable of type IntFunction that returns an integer array: IntFunction<int[]> i; With Java 8 generics, it is possible to initialize this variable with a constructor reference like this: i = int[]::new How does the Java compiler translate this to bytecode? I know that for other types, like String::new , it can use an invokedynamic instruction that points to the String constructor java/lang/String.<init>(...) , which is just a method with a special meaning. How does this work

Use method reference with parameter

感情迁移 提交于 2019-11-27 07:34:44
I just started learning Java streams and faced a problem. Please take a look at a the following example. This is part of a Node class: private Map<String, Node> nodes; public Optional<Node> child(String name) { return Optional.<Node>ofNullable(nodes.get(name)); } private void findChildren(String name, List<Node> result) { child(name).ifPresent(result::add); nodes.values().stream() // .map(Node::findChildren(name, result)) // .forEach(Node::findChildren(name, result)) .forEach(node -> node.findChildren(name, result)); } My intent was to call #findChildren with the name and result parameters on

What does “an Arbitrary Object of a Particular Type” mean in java 8?

一个人想着一个人 提交于 2019-11-27 02:16:51
问题 In Java 8 there is "Method Reference" feature. One of its kind is "Reference to an instance method of an arbitrary object of a particular type" http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html#type Can someone explain what does "arbitrary object of particular type" mean in that context ? 回答1: It is a reference to an instance method from some type. In the case of the example, compareToIgnoreCase is a method from String . The program knows that it can invoke this method

Static context cannot access non-static in Collectors

谁说我不能喝 提交于 2019-11-27 02:11:10
问题 I have group of students. First I want to group them by the marks. Then I want to further group those sets into same name students together. Map<Integer,Map<String,List<String>>> groupping = students.stream() .collect(Collectors.groupingBy(Student::getMarks, Collectors.mapping(Student::getName,Collectors.toList()))); I am getting a error saying, Non-static method cannot be refered from a static context. Yes. I am pretty much aware that I cannot refer a non-static method without having an