method-reference

Unable to figure out behaviour : Method reference with lambda

心已入冬 提交于 2019-12-11 10:57:36
问题 Consider below code, class DemoStatic { public static Runnable testStatic() { return () -> { System.out.println("Run"); }; } public void runTest () { Runnable r = DemoStatic::testStatic; r.run(); } } public class MethodReferenceDemo { public static void main(String[] args) { DemoStatic demo = new DemoStatic(); demo.runTest(); } } run() method of Runnable instance that is being return by testStatic method was supposed to be invoked. And output on console should be "Run". But this code is not

Nulling variable does not invalidate method reference [duplicate]

这一生的挚爱 提交于 2019-12-10 16:07:08
问题 This question already has answers here : Function pointer to String method in Java (3 answers) Closed 6 months ago . Why does the code not throw a NullPointerException when I use a method reference tied to a variable dog which I later assigned null to? I am using Java 8. import java.util.function.Function; class Dog { private int food = 10; public int eat(int num) { System.out.println("eat " + num); this.food -= num; return this.food; } } public class MethodRefrenceDemo { public static void

Why class/object name must be explicitly specified for method references?

半城伤御伤魂 提交于 2019-12-10 10:43:31
问题 When I want to refer to the method in the current scope I still need to specify class name (for static methods) or this before :: operator. For example, I need to write: import java.util.stream.Stream; public class StreamTest { public static int trimmedLength(String s) { return s.trim().length(); } public static void main(String[] args) { System.out.println(Stream.of(" aaa ", " bb ", " c ") .mapToInt(StreamTest::trimmedLength).sum()); } } It's not so big problem for this , but sometimes look

Type Inference in Method Reference

我的未来我决定 提交于 2019-12-10 10:38:29
问题 I recently put my hands on Java 8 and tried using Method References. I was trying different kinds of Method references and got stuck in the type "Reference to an Instance Method of an Arbitrary Object of a Particular Type". String[] arr = {"First", "Second", "Third", "Fourth"}; Arrays.sort(arr, String::compareToIgnoreCase); This works perfectly well. But when I try to refer a method of a user defined class through its type : Demo2[] arr = {a, b}; Arrays.sort(arr, Demo2::compare); This

Java method reference to a method with generic parameter

我们两清 提交于 2019-12-10 10:00:42
问题 I'm trying to make a method reference to a method which have a generic parameter specified in a class declaration. So I have: public interface IExecutable<P extends IParameter> { void execute(P parameter); } public class Parameter implements IParameter { public void childSpecific() { ... } } public class TestClass { ... //somewhere in the code public void foo(Parameter parameter) { parameter.childSpecific(); } public void test() { IExecutable<?> executable = this::foo; //compilation error //

Java Lambda Expression

南楼画角 提交于 2019-12-10 00:35:20
问题 I am currently learning lambda expressions on JDK 1.8. I have come across some code I have found that I do not understand. Here is the code: import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.lang.Comparable; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { List<String> list = Arrays.asList("a", "b", "c"); sort(list, Comparable::<String>compareTo); } interface MyComparable { public <T extends

Limits of static method references in Java 8

ぃ、小莉子 提交于 2019-12-09 16:58:57
问题 I'm trying to use method references to capture method invocations and am hitting some limitations. This works fine: <T> void capture(Function<T, ?> in) { } private interface Foo { String getBar(); } capture(Foo::getBar); But if I change the signature of Foo.setBar to something like this: private interface Foo { void setBar(String bar); } capture(Foo::setBar); I get an error: Cannot make a static reference to the non-static method setBar(String) from the type MyTest.Foo It's not clear to me

Java 8 method references with javafx

巧了我就是萌 提交于 2019-12-08 14:14:27
问题 I just developed a JavaFX applications this twenty different pages. Each page has a table and I wanted to place a context menu on each table. Basically its always the same code for placing the context menu to the table but I am hoping that method references can help here a little bit. This is the actual code snippet: resultTable.setRowFactory(new Callback<TableView<InterfaceModel>, TableRow<InterfaceModel>>() { @Override public TableRow<InterfaceModel> call(TableView<InterfaceModel> tableView

Using method references

有些话、适合烂在心里 提交于 2019-12-08 10:44:19
问题 I've got a JButton called saveButton and want it to call the save method when it is clicked. Of course we can do it using the old approach: saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { save(); } }); But today I want to use new Java 8 features like method references. Why does saveButton.addActionListener(this::save); not work? How is it done using method references? 回答1: Method actionPerformed(ActionEvent e) requires single

How can I create a list of method references?

大城市里の小女人 提交于 2019-12-07 08:07:18
问题 I have a need to work through a list and for each item call a different method on a target object. It seems elegant that I could just create a list of method references to do this, so for each index in the list, I could call the appropriate method reference that corresponds to it. private final static List<Consumer<String>> METHODS = (List<Consumer<String>>) Arrays.asList( TargetClass::setValue1, TargetClass::setValue2, TargetClass::setValue3, TargetClass::setValue4, TargetClass::setValue5);