method-reference

How does method reference casting work?

空扰寡人 提交于 2019-12-01 06:18:10
public class Main { interface Capitalizer { public String capitalize(String name); } public String toUpperCase() { return "ALLCAPS"; } public static void main(String[] args) { Capitalizer c = String::toUpperCase; //This works c = Main::toUpperCase; //Compile error } } Both are instance methods with same signature. Why does one work and the other doesn't? Signature of String::toUpperCase : String toUpperCase(); gniewkos There are 3 constructs to reference a method: object::instanceMethod Class::staticMethod Class::instanceMethod The line: Capitalizer c = String::toUpperCase; //This works use 3

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

强颜欢笑 提交于 2019-11-30 19:14:25
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 sBuilder::append the output will be: 2 Have you any ideas how to explain this? This isn't the same things?

Is there a way to use method references for top-level functions in jshell?

倖福魔咒の 提交于 2019-11-30 17:48:30
Suppose I do this in jshell: jshell> void printIsEven(int i) { ...> System.out.println(i % 2 == 0); ...> } | created method printIsEven(int) jshell> List<Integer> l = Arrays.asList(7,5,4,8,5,9); l ==> [7, 5, 4, 8, 5, 9] jshell> l.forEach(/* ??? */); // is it possible to use a method reference here? In a normal program I could write l.forEach(this::printIsEven) in a non-static context or l.forEach(MyClass::printIsEven) in the static context of a class named MyClass . Using this::printIsEven in jshell doesn't work because jshell executes statements in a static context, but you can't use a static

Is there a way to use method references for top-level functions in jshell?

浪尽此生 提交于 2019-11-30 16:45:53
问题 Suppose I do this in jshell: jshell> void printIsEven(int i) { ...> System.out.println(i % 2 == 0); ...> } | created method printIsEven(int) jshell> List<Integer> l = Arrays.asList(7,5,4,8,5,9); l ==> [7, 5, 4, 8, 5, 9] jshell> l.forEach(/* ??? */); // is it possible to use a method reference here? In a normal program I could write l.forEach(this::printIsEven) in a non-static context or l.forEach(MyClass::printIsEven) in the static context of a class named MyClass . Using this::printIsEven in

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

最后都变了- 提交于 2019-11-30 15:39:42
This question already has an answer here: Instance Method Reference and Lambda Parameters 2 answers 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 seems more like: "Reference to the first parameter of the abstract method of the Functional Interface, assuming it is of type

How does Java 8 know which String::compareTo method reference to use when sorting?

a 夏天 提交于 2019-11-30 13:45:22
How does Java know which String::compareTo method reference to use when calling Collections.sort(someListOfStrings, String::compareTo); ? compareTo is not static and it needs to know the value of the "left hand side" of the comparison. Suppose that you use method reference for Comparator interface: Comparator<String> cmp = String::compareTo; When you call the cmp.compare(left, right) (which is "single abstract method" or "SAM" of Comparator interface), the magic occurs: int result = cmp.compare(left, right); | | /------------------------/ | | /---------------/ | | left.compareTo(right);

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

99封情书 提交于 2019-11-30 13:25:45
问题 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? 回答1: String[]::new means size -> new String[size] . When Stream#toArray(IntFunction<A[]> generator) is ready to produce an array, it calls

Why is this Java method call considered ambiguous?

青春壹個敷衍的年華 提交于 2019-11-30 11:22:33
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 notAmbiguous(int x, int y) { return "string"; } public static int strangelyAmbiguous() { return 4; }

Java 8 pass method as parameter

微笑、不失礼 提交于 2019-11-30 10:32:16
问题 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>

Reference to an instance method of a particular object

狂风中的少年 提交于 2019-11-30 09:15:21
In the following code, it works when passing the method reference variable with the class name, but when passing the reference variable with a user object there is an error. public class User { private String name; public User(String name) { this.name = name; } public void printName() { System.out.println(name); } } public class Main { public static void main(String[] args) { User u1 = new User("AAA"); User u2 = new User("BBB"); User u3 = new User("ZZZ"); List<User> userList = Arrays.asList(u1, u2, u3); userList.forEach(User::printName); // works userList.forEach(u1::printName); // compile