method-reference

External argument to method reference in Java 8

旧时模样 提交于 2019-12-04 01:40:35
问题 I am looking to pass an external parameter to a method reference: String prefix = "The number is :"; numbers.forEach(Main::printWithPrefix); private static void printWithPrefix(Integer number) { System.out.println(number); } I am no idea on how to do it. I am able to do it with a lambda: String prefix = "The number is :"; numbers.forEach(number -> { System.out.println(prefix + number); }); Is it possible to pass an external parameter to a method reference? 回答1: No, you cannot pass a parameter

Reference to an instance method of a particular object

蓝咒 提交于 2019-12-03 19:31:07
问题 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

Method Reference. Cannot make a static reference to the non-static method

余生长醉 提交于 2019-12-03 10:20:36
问题 Can someone explain to me, why passing a non-static method-reference to method File::isHidden is ok, but passing method reference to a non-static method MyCass::mymethod - gives me a "Cannot make a static reference to the non-static method" ? public static void main(String[] args) { File[] files = new File("C:").listFiles(File::isHidden); // OK test(MyCass::mymethod); // Cannot make a static reference to the non-static method } static interface FunctionalInterface{ boolean function(String

Invalid constructor reference when using local class?

耗尽温柔 提交于 2019-12-03 07:32:41
问题 Given the following code: package com.gmail.oksandum.test; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { } public void foo() { class LocalFoo { LocalFoo(String in) { //Some logic } } List<String> ls = new ArrayList<>(); ls.stream().map(LocalFoo::new); //Line 21 } } my IDE gives me no errors. That is, until I try to build the project and run it. When I do that it gives me a compiler error that looks like this: Error:(21, 24)

The type org.eclipse.jdt.annotation.NonNull cannot be resolved. It is indirectly referenced from required .class files

强颜欢笑 提交于 2019-12-03 05:40:56
问题 When I use the Java 8 method reference double colon operator ( :: ) with new operator (e.g. MyType::new ), I get this error in Eclipse of Spring Tool suite (STS): The type org.eclipse.jdt.annotation.NonNull cannot be resolved. It is indirectly referenced from required .class files How to get rid of this error? 回答1: Error description is provided in Stephan Herrmann's comment. There is open Eclipse issue to make this issue more user friendly. Solution is to include following dependency:

Why does invokeLater execute in the main thread?

拜拜、爱过 提交于 2019-12-02 23:47:47
I just encountered this "bug", but I'm not sure if this is intended: Code: public static Object someMethod(){ assert SwingUtilities.isEventDispatchThread(); return new Object(); } public static void main(String[] args){ SwingUtilities.invokeLater(() -> someMethod().toString());//First Example SwingUtilities.invokeLater(someMethod()::toString);//Second Example } In the first example someMethod is being executed on the swing Thread, but in the second example it is not, although it should be in my opinion. Is this a bug or is this intended? Pelocho To me it seems like a misunderstanding on your

Syntax for specifying a method reference to a generic method

不羁的心 提交于 2019-12-01 16:27:25
I read the following code in "Java - The beginner's guide" interface SomeTest <T> { boolean test(T n, T m); } class MyClass { static <T> boolean myGenMeth(T x, T y) { boolean result = false; // ... return result; } } The following statement is valid SomeTest <Integer> mRef = MyClass :: <Integer> myGenMeth; Two points were made regarding the explanation of the above code 1 - When a generic method is specified as a method reference, its type argument comes after the :: and before the method name. 2 - In case in which a generic class is specified, the type argument follows the class name and

Return method reference

北城余情 提交于 2019-12-01 12:07:27
I am playing around in Java 8. How can I return a method reference? I am able to return a lambda but not the method reference. My attempts: public Supplier<?> forEachChild(){ return new ArrayList<?>::forEach; } OR public Function<?> forEachChild(){ return new ArrayList<?>::forEach; } You have a small mis-understanding of how method-references work. First of all, you cannot new a method-reference. Then, let's reason through what you want to do. You want the method forEachChild to be able to return something that would accept a List and a Consumer . The List would be on which object to invoke

Return method reference

我怕爱的太早我们不能终老 提交于 2019-12-01 11:05:40
问题 I am playing around in Java 8. How can I return a method reference? I am able to return a lambda but not the method reference. My attempts: public Supplier<?> forEachChild(){ return new ArrayList<?>::forEach; } OR public Function<?> forEachChild(){ return new ArrayList<?>::forEach; } 回答1: You have a small mis-understanding of how method-references work. First of all, you cannot new a method-reference. Then, let's reason through what you want to do. You want the method forEachChild to be able

Java - Use predicate without lambda expressions

倾然丶 夕夏残阳落幕 提交于 2019-12-01 09:13:12
I've below requirement:- Employee.java public boolean isAdult(Integer age) { if(age >= 18) { return true; } return false; } Predicate.java private Integer age; Predicate<Integer> isAdult; public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) { this.age = age; System.out.println("Test result is "+ isAdult(age)); } public void testPredicate() { System.out.println("Calling the method defined in the manager class"); } Now My goal is to test whether the age which i pass to Predicate is adult or not using the method defined in Employee class , for which i am passing the method