method-reference

Java 8 Method Reference to non-static method

淺唱寂寞╮ 提交于 2019-11-30 08:32:19
Why this doesn't work? I get compiler error "Cannot make static reference to the non static method print..." public class Chapter3 { public void print(String s) { System.out.println(s); } public static void main(String[] args) { Arrays.asList("a", "b", "c").forEach(Chapter3::print); } } Regardless of whether you use method references, lambda expressions or ordinary method calls, an instance method requires an appropriate instance for the invocation. The instance may be supplied by the function invocation, e.g. if forEach expected a BiConsumer<Chapter3,String> it worked. But since forEach

What does “String[]::new” mean?

别等时光非礼了梦想. 提交于 2019-11-30 07:34:21
I'm learning how to use stream, and I get a problem with this method. public static String[] inArray(String[] array1, String[] array2) { return Arrays.stream(array1) .filter(str -> Arrays.stream(array2).anyMatch(s -> s.contains(str))) .distinct().sorted().toArray(**String[]::new**); } I'm so confused about String[]::new , could you give me a hint? String[]::new means size -> new String[size] . When Stream#toArray(IntFunction<A[]> generator) is ready to produce an array, it calls generator and passes ( generator.apply ) the size of the inner collection to get a collection to fill it up. String[

Why do equivalent lambda expression and method reference behave differently when capturing static field value?

不打扰是莪最后的温柔 提交于 2019-11-30 03:27:53
问题 I'm a little bit confused about Java lambdas and method references behaviour. For ex., we have this code: import java.util.function.Consumer; public class Main { private static StringBuilder sBuilder = new StringBuilder("1"); public static void main(String[] args) { Consumer<String> consumer = s -> sBuilder.append(s); sBuilder = new StringBuilder("2"); consumer.accept("3"); System.out.println(sBuilder); } } Output: 23 This works as expected, but if we replace s -> sBuilder.append(s) with

How does a method “reference to an instance method of an arbitrary object of a particular type” resolve the arbitrary object? [duplicate]

我的梦境 提交于 2019-11-29 22:31:22
问题 This question already has answers here : Instance Method Reference and Lambda Parameters (2 answers) Closed 4 years ago . The oracle Java 8 documentation defines 4 types of method references you can use instead of Lambda Expressions. What I am trying to understand is the kind of method reference described as: "Reference to an instance method of an arbitrary object of a particular type " which is written as ContainingType::methodName . I am not sure if I am missing something, but to me it

Java 8 pass method as parameter

大憨熊 提交于 2019-11-29 21:17:45
Currently getting into Java 8 lambda expressions and method references. I want to pass a method with no args and no return value as argument to another method. This is how I am doing it: public void one() { System.out.println("one()"); } public void pass() { run(this::one); } public void run(final Function function) { function.call(); } @FunctionalInterface interface Function { void call(); } I know there is a set of predefined functional interfaces in java.util.function such as Function<T,R> but I didn't find one with no arguments and not producing a result. It really does not matter;

Horrendous performance & large heap footprint of Java 8 constructor reference?

99封情书 提交于 2019-11-29 20:24:45
I just had a rather unpleasant experience in our production environment, causing OutOfMemoryErrors: heapspace.. I traced the issue to my use of ArrayList::new in a function. To verify that this is actually performing worse than normal creation via a declared constructor ( t -> new ArrayList<>() ), I wrote the following small method: public class TestMain { public static void main(String[] args) { boolean newMethod = false; Map<Integer,List<Integer>> map = new HashMap<>(); int index = 0; while(true){ if (newMethod) { map.computeIfAbsent(index, ArrayList::new).add(index); } else { map

Why is this Java method call considered ambiguous?

懵懂的女人 提交于 2019-11-29 17:02:54
问题 I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String s); } public static void load(Supplier s) {} public static void load(Processor p) {} public static int genuinelyAmbiguous() { return 4; } public static String genuinelyAmbiguous(String s) { return "string"; } public static int notAmbiguous() { return 4; } public static String

Java 8 Method Reference to non-static method

岁酱吖の 提交于 2019-11-29 11:58:30
问题 Why this doesn't work? I get compiler error "Cannot make static reference to the non static method print..." public class Chapter3 { public void print(String s) { System.out.println(s); } public static void main(String[] args) { Arrays.asList("a", "b", "c").forEach(Chapter3::print); } } 回答1: Regardless of whether you use method references, lambda expressions or ordinary method calls, an instance method requires an appropriate instance for the invocation. The instance may be supplied by the

Multiple lambda method references

喜你入骨 提交于 2019-11-29 11:34:33
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::toLowerCase); list.forEach(System.out::println); list.replaceAll(String::toUpperCase); list.forEach(System.out:

Invoking toString via method reference in Java 8

我是研究僧i 提交于 2019-11-29 10:08:05
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 This has nothing to do with type erasure. Look at the error message : (argument mismatch; invalid method reference reference to toString is ambiguous both method toString(int) in Integer and method toString() in Integer match) The Integer