method-reference

How knows Stream.toArray(Book[]::new); how many elements the array has and where is the implementation of “Book[]::new”

落爺英雄遲暮 提交于 2021-02-20 04:26:27
问题 I'm triying to understand method references and I don't know how with this "Book[]::new" it can create an array with the right number of elements. Book[] arrayBook = stBooks.toArray(Book[]::new); In order to create the array when I use the second and third option I have to specify a Functional Interface that Receive an Int and return a new Array. But, in the first option I don't know where is the implementation and where you specify the number or elements that is going to have the array. I

Use of constructor reference where constructor has a non-empty parameter list

泪湿孤枕 提交于 2021-02-19 02:14:55
问题 Given.. List<Foo> copy(List<Foo> foos) { return foos .stream() .map(foo -> new Foo(foo)) .collect(Collectors.toList()); } IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference". I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here. 回答1: I'm aware of the Foo::new syntax for the no-arg constructor That's not what Foo::new does. This expression will expand to what is

Use of constructor reference where constructor has a non-empty parameter list

谁说胖子不能爱 提交于 2021-02-19 02:09:23
问题 Given.. List<Foo> copy(List<Foo> foos) { return foos .stream() .map(foo -> new Foo(foo)) .collect(Collectors.toList()); } IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference". I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here. 回答1: I'm aware of the Foo::new syntax for the no-arg constructor That's not what Foo::new does. This expression will expand to what is

Types in a LambdaMetaFactory

女生的网名这么多〃 提交于 2021-02-18 10:28:11
问题 I get an exception when I call metafactory . It says: java.lang.invoke.LambdaConversionException: Incorrect number of parameters for instance method invokeVirtual my.ExecuteTest$AProcess.step_1:()Boolean; 0 captured parameters, 0 functional interface method parameters, 0 implementation parameters I do not understand all from the documentation of LambdaMetafactory.metafactory . I have problems figuring out the correct parameters: MethodHandles.Lookup caller -- thats easy String invokedName --

Java Method reference not expected here

爷,独闯天下 提交于 2021-02-07 14:26:28
问题 How exactly do you chain method references for instances with Java 8? Example: Collections.sort(civs,Comparator.comparing(Civilization::getStrategy.getStrategLevel)); getStrategy of a Civilization instance returns a Strategy object instance which has the instance method getStrategyLevel . Why doesn't the Comparator.comparing method return a comparator with it's functional interface implemented by the lambda expression? 回答1: In that case, you should use a lambda, you can't apply a method

Method reference in Java 8

假装没事ソ 提交于 2021-02-06 15:26:11
问题 public class Car { private int maxSpeed; public Car(int maxSpeed) { this.maxSpeed = maxSpeed; } public int getMaxSpeed() { return maxSpeed; } } We can sort a list of cars by, Car carX = new Car(155); Car carY = new Car(140); List<Car> cars = new ArrayList<>(); cars.add(carX); cars.add(carY); cars.sort(Comparator.comparing(Car::getMaxSpeed)); If we see the signature of the method Comparator.comparing , the input parameter type is Function<? super T, ? extends U> In the above example, how is

Understand the compile time error with Method Reference

雨燕双飞 提交于 2021-01-04 06:41:39
问题 As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so? class Demo{ private Function<Student, Integer> p= Student::getGradeLevel; // fine

Understand the compile time error with Method Reference

扶醉桌前 提交于 2021-01-04 06:41:31
问题 As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so? class Demo{ private Function<Student, Integer> p= Student::getGradeLevel; // fine