method-reference

Why Double::compareTo can be used as an argument of Stream.max(Comparator<? super T> comparator)

◇◆丶佛笑我妖孽 提交于 2020-06-25 04:42:13
问题 The api for Stream.max requires an argument of type Comparator<? super T> , and for Comparator , the only abstract method is int compare(T o1, T o2) but Double::compareTo , the compareTo api is public int compareTo(Double anotherDouble) why just provide one argument, so why can Double::compareTo use as argument of Stream Optional<T> max(Comparator<? super T> comparator) 回答1: The below MyComparator implement a Comparator . It takes two arguments. It's the same as the lambda expression (d1,d2)

Are method References as method parameters thread safe in Java

心不动则不痛 提交于 2020-05-16 08:58:08
问题 i have the following scenario: interface ValueBinding<T> { public void setValue(T input); } public enum FacesBinding { VALUE; public void bindString(ValueBinding<String> fcn, HttpServletRequest req, String param){ try { String val = req.getParameter(param); if( val != null ) fcn.setValue(val); } catch (Exception e) { } } public void bindBoolean(ValueBinding<Boolean> fcn, HttpServletRequest req, String param){ try { fcn.setValue(req.getParameter(param) != null); } catch (Exception e) { } }

Are method References as method parameters thread safe in Java

*爱你&永不变心* 提交于 2020-05-16 08:58:07
问题 i have the following scenario: interface ValueBinding<T> { public void setValue(T input); } public enum FacesBinding { VALUE; public void bindString(ValueBinding<String> fcn, HttpServletRequest req, String param){ try { String val = req.getParameter(param); if( val != null ) fcn.setValue(val); } catch (Exception e) { } } public void bindBoolean(ValueBinding<Boolean> fcn, HttpServletRequest req, String param){ try { fcn.setValue(req.getParameter(param) != null); } catch (Exception e) { } }

Are method References as method parameters thread safe in Java

不羁岁月 提交于 2020-05-16 08:57:46
问题 i have the following scenario: interface ValueBinding<T> { public void setValue(T input); } public enum FacesBinding { VALUE; public void bindString(ValueBinding<String> fcn, HttpServletRequest req, String param){ try { String val = req.getParameter(param); if( val != null ) fcn.setValue(val); } catch (Exception e) { } } public void bindBoolean(ValueBinding<Boolean> fcn, HttpServletRequest req, String param){ try { fcn.setValue(req.getParameter(param) != null); } catch (Exception e) { } }

Create BiConsumer from LambdaMetafactory

只谈情不闲聊 提交于 2020-05-12 11:10:23
问题 I'm trying to dynamically create a method reference of type BiConsumer through LambdaMetafactory. I was trying to apply two approaches found on https://www.cuba-platform.com/blog/think-twice-before-using-reflection/ - createVoidHandlerLambda and here Create BiConsumer as Field setter without reflection the Holger's answer. However in both cases I'm having below error: Exception in thread "main" java.lang.AbstractMethodError: Receiver class org.home.ref.App$$Lambda$15/0x0000000800066040 does

method reference vs lambda expression

冷暖自知 提交于 2020-01-31 18:13:19
问题 I want to replace lambda expression by method reference in the below example : public class Example { public static void main(String[] args) { List<String> words = Arrays.asList("toto.", "titi.", "other"); //lambda expression in the filter (predicate) words.stream().filter(s -> s.endsWith(".")).forEach(System.out::println); } } I want to write a something like this : words.stream().filter(s::endsWith(".")).forEach(System.out::println); is it possible to transform any lambda expression to

method reference vs lambda expression

好久不见. 提交于 2020-01-31 18:11:01
问题 I want to replace lambda expression by method reference in the below example : public class Example { public static void main(String[] args) { List<String> words = Arrays.asList("toto.", "titi.", "other"); //lambda expression in the filter (predicate) words.stream().filter(s -> s.endsWith(".")).forEach(System.out::println); } } I want to write a something like this : words.stream().filter(s::endsWith(".")).forEach(System.out::println); is it possible to transform any lambda expression to

Java 8 method reference to class instance method NPE

依然范特西╮ 提交于 2020-01-30 13:27:12
问题 import java.util.function.Function; public class Playground { public static void main (String[] args) { Object o = null; System.out.println(o); Function<Object, String> toStringFunc = Object::toString; String s = toStringFunc.apply(o); System.out.println(s); } } This code will result in a NullPointerException being thrown, reported at the line containing toStringFunc.apply(o) . This is a trivial example, so it's easy to see that o == null , but how in general can we understand why this line

SONAR: Replace this lambda with a method reference

自作多情 提交于 2020-01-23 04:32:20
问题 Sonar tells me "Replace this lambda with a method reference" public class MyClass { private List<SomeValue> createSomeValues(List<Anything> anyList) { return anyList // .stream() // .map(anything -> createSomeValue(anything)) // .collect(Collectors.toList()); } private SomeValue createSomeValue(Anything anything) { StatusId statusId = statusId.fromId(anything.getStatus().getStatusId()); return new SomeValue(anything.getExternId(), statusId); } } Is this possible here? I tried several things,

Java - Use predicate without lambda expressions

二次信任 提交于 2020-01-21 08:28:21
问题 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