method-reference

Why is lambda return type not checked at compile time?

。_饼干妹妹 提交于 2019-12-18 12:05:15
问题 The used method reference has return type Integer . But an incompatible String is allowed in the following example. How to fix the method with declaration to get the method reference type safe without manually casting? import java.util.function.Function; public class MinimalExample { static public class Builder<T> { final Class<T> clazz; Builder(Class<T> clazz) { this.clazz = clazz; } static <T> Builder<T> of(Class<T> clazz) { return new Builder<T>(clazz); } <R> Builder<T> with(Function<T, R>

Why Comparator.comparing doesn't work with String::toLowerCase method reference?

泪湿孤枕 提交于 2019-12-18 05:47:05
问题 I am trying to sort an array of Strings by reverse order (ignoring case), without modifying it, and just printing it. So I am using Java8 stream. But I can't manage to do it. Here is my attempt : package experimentations.chapter02; import java.util.Arrays; import java.util.Comparator; import java.util.stream.Collectors; public class StringStream { public static void main(String[] args) { sortStrings(); } public static void sortStrings(){ String[] stringsArray = "The quick brown fox has a

Method Reference - passing Function to method with Consumer argument

陌路散爱 提交于 2019-12-18 05:12:07
问题 I'm learning about Method References from Java 8 and I have difficulties understanding why does this work? class Holder { private String holded; public Holder(String holded) { this.holded = holded; } public String getHolded() { return holded; } } private void run() { Function<Holder, String> getHolded = Holder::getHolded; consume(Holder::getHolded); //This is correct... consume(getHolded); //...but this is not } private void consume(Consumer<Holder> consumer) { consumer.accept(null); } As you

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

假如想象 提交于 2019-12-17 17:31:47
问题 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

Use method reference with parameter

烂漫一生 提交于 2019-12-17 06:39:09
问题 I just started learning Java streams and faced a problem. Please take a look at a the following example. This is part of a Node class: private Map<String, Node> nodes; public Optional<Node> child(String name) { return Optional.<Node>ofNullable(nodes.get(name)); } private void findChildren(String name, List<Node> result) { child(name).ifPresent(result::add); nodes.values().stream() // .map(Node::findChildren(name, result)) // .forEach(Node::findChildren(name, result)) .forEach(node -> node

Instance Method Reference and Lambda Parameters

拟墨画扇 提交于 2019-12-17 05:11:12
问题 I am having trouble understanding the syntax for a method reference, where there are two parameters a and b , and the reference is to a method of a on b . For example I understand how Arrays.sort(personArray, comparators::compareByName); is equivalent to Arrays.sort(personArray, (o1, o2) -> comparators.compareByName(o1, o2)); because in that case the lambda parameters match the method call parameters (o1, o2) . Howevever for this lambda stream.sorted((o1, o2) -> o1.compareToIgnoreCase(o2));

java.lang.NullPointerException is thrown using a method-reference but not a lambda expression

随声附和 提交于 2019-12-17 05:01:17
问题 I've noticed something weird about unhandled exceptions using Java 8 method reference. This is my code, using the lambda expression () -> s.toLowerCase() : public class Test { public static void main(String[] args) { testNPE(null); } private static void testNPE(String s) { Thread t = new Thread(() -> s.toLowerCase()); // Thread t = new Thread(s::toLowerCase); t.setUncaughtExceptionHandler((t1, e) -> System.out.println("Exception!")); t.start(); } } It prints "Exception", so it works fine. But

What is the equivalent lambda expression for System.out::println

删除回忆录丶 提交于 2019-12-16 22:50:07
问题 I stumbled upon the following Java code which is using a method reference for System.out.println class SomeClass{ public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9); numbers.forEach(System.out::println); } } } What is the equivalent lambda expression for System.out::println ? 回答1: The method reference System.out::println will evaluate System.out first, then create the equivalent of a lambda expression which captures the evaluated value. Usually,

Is method reference caching a good idea in Java 8?

北慕城南 提交于 2019-12-16 22:19:29
问题 Consider I have code like the following: class Foo { Y func(X x) {...} void doSomethingWithAFunc(Function<X,Y> f){...} void hotFunction(){ doSomethingWithAFunc(this::func); } } Suppose that hotFunction is called very often. Would it then be advisable to cache this::func , maybe like this: class Foo { Function<X,Y> f = this::func; ... void hotFunction(){ doSomethingWithAFunc(f); } } As far as my understanding of java method references goes, the Virtual Machine creates an object of an anonymous

Comparator.reversed() does not compile using lambda

别说谁变了你拦得住时间么 提交于 2019-12-16 22:10:26
问题 I have a list with some User objects and i'm trying to sort the list, but only works using method reference, with lambda expression the compiler gives an error: List<User> userList = Arrays.asList(u1, u2, u3); userList.sort(Comparator.comparing(u -> u.getName())); // works userList.sort(Comparator.comparing(User::getName).reversed()); // works userList.sort(Comparator.comparing(u -> u.getName()).reversed()); // Compiler error Error: com\java8\collectionapi\CollectionTest.java:35: error: