functional-interface

Is there a way to use Java 8 functional interfaces on Android API below 24?

北慕城南 提交于 2019-12-05 00:00:58
I can use retrolambda to enable lambdas with Android API level <24. So this works myButton.setOnClickListener(view -> Timber.d("Lambdas work!")); This also works Runnable runLater = () -> Timber.d("Lambdas work!"); runLater.run(); But this one does not Consumer<Integer> runLaterWithInt = (Integer i) -> Timber.d("i = " + i); runLaterWithInt.accept(3); The last one works on Android API Level 24, but on other devices this code causes a crash java.lang.NoClassDefFoundError: com.retrolambdatry.MainActivity$$Lambda$1 Instead of using retrolambda I tried to enable Java 8. First two code examples

Java8 lambda for checking two conditions

 ̄綄美尐妖づ 提交于 2019-12-04 16:58:22
I have the following code snippet which I would try to change to the lambda function. if(catList != null && catList.size() > 0) { animalType = AnimalType.CAT; HttpEntity<List<?>> request = new HttpEntity<>(catList, headers); response = restTemplate.postForObject(apiUrl, request, String.class); } else if(dogList != null && dogList.size() > 0) { animalType = AnimalType.DOG; } else { return; } Somehow I have written like as shown below, but don't know to incorporate the dogList checking the condition Optional.of(catList) .map(catList -> { .... }) .orElse(return); //<------ THIS IS NOT POSSIBLE

reference to method is ambiguous when migrating from java8 to java9

吃可爱长大的小学妹 提交于 2019-12-04 09:17:37
问题 I'm migrating a project from JAVA 8 to JAVA 9 and I'm having some trouble getting the code to work. All work in JAVA 8 but in 9 I'm having the following errors: Error java: reference to ok is ambiguous both method <T>ok(java.util.function.Supplier<T>) and method ok(web.Procedure) match here is the code when I'm calling the method: public ResponseEntity<List<MailTemplateDto>> mailTemplateFindAll() { return ok(() -> mailTemplateService.findAll()); } and here is the implementation : public <T>

Why doesn't Java 8's ToIntFunction<T> extend Function<T, Integer>

白昼怎懂夜的黑 提交于 2019-12-04 03:22:52
问题 If I wrote the ToIntFunction interface, i'd want to encode in the interface the fact that it's just a function that returns a primitive int, like this: @FunctionalInterface public interface ToIntFunction<T> extends Function<T, Integer> { int applyAsInt(T value); @Override default Integer apply(T value) { return Integer.valueOf(applyAsInt(value)); } } I was wondering, is there a compelling reason Java 8 API designers chose to keep the primitive alternatives completely separate from Function?

How to ensure at Java 8 compile time that a method signature “implements” a functional interface

给你一囗甜甜゛ 提交于 2019-12-03 17:20:30
问题 Is there in Java 8 any analogue for implements keyword for methods? Let's say I have a functional interface: @FunctionalInterface interface LongHasher { int hash(long x); } And a library of 3 static methods "implementing" this functional interface: class LongHashes { static int xorHash(long x) { return (int)(x ^ (x >>> 32)); } static int continuingHash(long x) { return (int)(x + (x >>> 32)); } static int randomHash(long x) { return xorHash(x * 0x5DEECE66DL + 0xBL); } } In the future I want to

Why does Comparator declare equals?

浪子不回头ぞ 提交于 2019-12-03 10:45:15
问题 The Comparator interface has its own equals() method. Any class will get equals() by default through Object class. What is the need to have equals() method inside an interface? 回答1: Comparator refines the contract of Object.equals : It has to satisfy the constraints set out by Object.equals and then some . Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn

Do you have a list of Java 8 Functional interfaces (not the ones listed in java.util.function)?

走远了吗. 提交于 2019-12-03 05:41:14
I'm trying to see if there is any way to get a list of all the interfaces in Java 8 that are functional interfaces. I'm not talking about the list on this page: https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html Rather, I'm talking about interfaces like Comparator, FileFilter, and Runnable - interfaces that the API document shows are functional like this: @FunctionalInterface public interface Runnable Is there a full list of these somewhere? Thank you! Holger There is a list of all interfaces being annotated with @FunctionalInterface available in the API

Why does a lambda change overloads when it throws a runtime exception?

别说谁变了你拦得住时间么 提交于 2019-12-03 02:59:11
问题 Bear with me, the introduction is a bit long-winded but this is an interesting puzzle. I have this code: public class Testcase { public static void main(String[] args){ EventQueue queue = new EventQueue(); queue.add(() -> System.out.println("case1")); queue.add(() -> { System.out.println("case2"); throw new IllegalArgumentException("case2-exception");}); queue.runNextTask(); queue.add(() -> System.out.println("case3-never-runs")); } private static class EventQueue { private final Queue

reference to method is ambiguous when migrating from java8 to java9

[亡魂溺海] 提交于 2019-12-03 02:41:46
I'm migrating a project from JAVA 8 to JAVA 9 and I'm having some trouble getting the code to work. All work in JAVA 8 but in 9 I'm having the following errors: Error java: reference to ok is ambiguous both method <T>ok(java.util.function.Supplier<T>) and method ok(web.Procedure) match here is the code when I'm calling the method: public ResponseEntity<List<MailTemplateDto>> mailTemplateFindAll() { return ok(() -> mailTemplateService.findAll()); } and here is the implementation : public <T> ResponseEntity<T> ok(Supplier<T> action) { return this.body(HttpStatus.OK, action); } public <T>

Why does Comparator declare equals?

心不动则不痛 提交于 2019-12-03 02:18:15
The Comparator interface has its own equals() method. Any class will get equals() by default through Object class. What is the need to have equals() method inside an interface? Comparator refines the contract of Object.equals : It has to satisfy the constraints set out by Object.equals and then some . Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2 . Declaring an equals