method-reference

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

一个人想着一个人 提交于 2019-11-29 08:01:43
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 { public static final class Greeter { private final String salutation; public Greeter(final String

Method Reference - passing Function to method with Consumer argument

人走茶凉 提交于 2019-11-29 07:50:31
I'm learning about Method References from Java 8 and I have difficulties understanding why does this work? class Holder { private String holded; public Holder(String holded) { this.holded = holded; } public String getHolded() { return holded; } } private void run() { Function<Holder, String> getHolded = Holder::getHolded; consume(Holder::getHolded); //This is correct... consume(getHolded); //...but this is not } private void consume(Consumer<Holder> consumer) { consumer.accept(null); } As you can see in run method - Holder::getHolded returns unbound method reference which you can invoke by

Why did Java 8 introduce a new “::” operator for method references?

北慕城南 提交于 2019-11-29 06:23:15
问题 In Java 8 method references are done using the :: operator. For Example // Class that provides the functionality via it's static method public class AddableUtil { public static int addThemUp(int i1, int i2){ return i1+i2; } } // Test class public class AddableTest { // Lambda expression using static method on a separate class IAddable addableViaMethodReference = AddableUtil::addThemUp; ... } You can see that the addableViaMethodReference now acts like an alias to AddableUtil::addThemUp . So

Java compiler: How can two methods with the same name and different signatures match a method call?

浪子不回头ぞ 提交于 2019-11-29 04:01:41
I have this class called Container : public class Container { private final Map<String, Object> map = new HashMap<>(); public void put(String name, Object value) { map.put(name, value); } public Container with(String name, Object value) { put(name, value); return this; } public Object get(String name) { return map.get(name); } public <R> R get(String name, Function<Object, R> mapper) { Object value = get(name); if (null == value) { return null; } return mapper .apply(value); } public <R> R get(String name, Class<R> type) { Object value = get(name); if (null == value) { return null; } if (type

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

£可爱£侵袭症+ 提交于 2019-11-29 03:49:19
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 pipeline to fit on one line). Other than code style, is there any difference between Objects::nonNull and x

Reference to methods with different parameters in Java8

*爱你&永不变心* 提交于 2019-11-29 03:40:11
I'm wondering how does all this stuff with method references and functional interfaces works on lower level. The easiest example is where we have some List List<String> list = new ArrayList<>(); list.add("b"); list.add("a"); list.add("c"): Now we want to sort it with Collections class, so we can call: Collections.sort(list, String::compareToIgnoreCase); But if we define custom comparator it could be something like: Comparator<String> customComp = new MyCustomOrderComparator<>(); Collections.sort(list, customComp::compare); The problem is that Collections.sort takes two parameters: List and

Java8 method reference used as Function object to combine functions

让人想犯罪 __ 提交于 2019-11-28 22:30:06
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 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::length, n -> n * 2); System.out.println(function.apply("foo")); } public static <T1, T2, T3> Function<T1, T3>

How do Java 8 array constructor references work?

拈花ヽ惹草 提交于 2019-11-28 20:04:46
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 with arrays, seeing that there are special instructions for constructing arrays? You can find out yourself

What are the uses of constructor reference in java 8

血红的双手。 提交于 2019-11-28 12:37:18
I was reading about Java 8 features, which lead me to this article and I was wondering about the actual uses of constructor reference, I mean why not just use new Obj ? P.S, I tried googling, but I failed to find something meaningful, if someone has a code example, link or tut it will be great Holger First of all, you should understand that constructor references are just a special form of method references. The point about method references is that they do not invoke the referenced method but provide a way to define a function which will invoke the method when being evaluated. The linked

Static context cannot access non-static in Collectors

本小妞迷上赌 提交于 2019-11-28 08:09:48
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 instance. But with all these stream operations, I'm bit confused what has gone wrong really. Rather than