functional-interface

Which FunctionalInterface should I use?

 ̄綄美尐妖づ 提交于 2019-12-07 02:21:39
问题 I was learning to write some lambda representation as FunctionalInterface. So, to add two integers I used: BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b; System.out.println(biFunction.apply(10, 60)); Gives me the output 70 . But if I write it as this BinaryOperator<Integer, Integer, Integer> binaryOperator = (a, b) -> a + b; I get an error saying Wrong number of type arguments: 3; required: 1 Isn't BinaryOperator a child of BinaryFunction ? How do I improve it? 回答1:

Java8 lambda for checking two conditions

删除回忆录丶 提交于 2019-12-06 10:53:17
问题 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

Converting array iteration to lambda function using Java8

坚强是说给别人听的谎言 提交于 2019-12-05 20:22:34
I am trying to convert to Lambda function So far I am able to convert the above code to lambda function like as shown below Stream.of(acceptedDetails, rejectedDetails) .filter(list -> !isNull(list) && list.length > 0) .forEach(new Consumer<Object>() { public void accept(Object acceptedOrRejected) { String id; if(acceptedOrRejected instanceof EmployeeValidationAccepted) { id = ((EmployeeValidationAccepted) acceptedOrRejected).getId(); } else { id = ((EmployeeValidationRejected) acceptedOrRejected).getAd().getId(); } if(acceptedOrRejected instanceof EmployeeValidationAccepted) { dates1.add(new

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

橙三吉。 提交于 2019-12-05 09:40:47
问题 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

Lambda Expression is not working, getting terminated

雨燕双飞 提交于 2019-12-05 09:17:54
wrote java8 program with lambda expression, its not getting executed instead its getting terminated at the lambda expression, no exceptions import java.util.ArrayList; import java.util.List; import java.util.function.BiConsumer; public class BiConsumerTest { public static void main(String[] args) { try{ List<String> list1 = new ArrayList<String>(); list1.add("A"); list1.add("B"); list1.add("V"); List<String> list2 = new ArrayList<String>(); list2.add("J"); list2.add("G"); list2.add("P"); BiConsumer<List<String> , List<String>> bc = (lista, listb) ->{ lista.stream().forEach( System.out::print);

Definition of Functional Interface in Java 8 [duplicate]

青春壹個敷衍的年華 提交于 2019-12-05 05:40:52
This question already has an answer here: Precise definition of “functional interface” in Java 8 6 answers The definition of a functional Interface in Java 8 says: A functional interface is defined as any interface that has exactly one explicitly declared abstract method. (The qualification is necessary because an interface may have non-abstract default methods.) This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is still sometimes seen. So how come we have this: List<Double> temperature = new ArrayList<Double>(Arrays.asList(new Double[] {

Which FunctionalInterface should I use?

被刻印的时光 ゝ 提交于 2019-12-05 05:35:59
I was learning to write some lambda representation as FunctionalInterface . So, to add two integers I used: BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b; System.out.println(biFunction.apply(10, 60)); Gives me the output 70 . But if I write it as this BinaryOperator<Integer, Integer, Integer> binaryOperator = (a, b) -> a + b; I get an error saying Wrong number of type arguments: 3; required: 1 Isn't BinaryOperator a child of BinaryFunction ? How do I improve it? BinaryOperator Since BinaryOperator works on a single type of operands and result . i.e. BinaryOperator<T> . Isn

Equality of instance of functional interface in java [duplicate]

偶尔善良 提交于 2019-12-05 03:32:21
This question already has answers here : Is there a way to compare lambdas? (3 answers) Closed 4 years ago . I am not sure how I can be sure about equality/immutability of functional interface. I guess there might be no way to assure equality when I use this syntactic sugar in java 8, please let me know any hint if you have any. I made a short code snippet for my question. public interface Element { void doSomething(int a); } and I've tried to add instance of this interface in functional way public class FunctionSet { public void doubleUp(int a) { System.out.println(a*2); } public void square

Create custom Predicate with Set<String> and String as parameter

房东的猫 提交于 2019-12-05 02:11:12
I have a String as "ishant" and a Set<String> as ["Ishant", "Gaurav", "sdnj"] . I need to write the Predicate for this. I have tried as below code, but it is not working Predicate<Set<String>,String> checkIfCurrencyPresent = (currencyList,currency) -> currencyList.contains(currency); How can I create a Predicate which will take Set<String> and String as a parameter and can give the result? A Predicate<T> which you're currently using represents a predicate (boolean-valued function) of one argument . You're looking for a BiPredicate<T,U> which essentially represents a predicate (boolean-valued

A summary of the parameters and return type of functional interfaces in the package java.util.function

陌路散爱 提交于 2019-12-05 01:43:08
问题 I'm looking for a table of parameters and return type of the single abstract method (SAM) of all interfaces in java.util.function . 回答1: Here's a table of all 43 interfaces in the package, plus some other notable interfaces. This arrangement should make it easy to see the naming patterns in the package. The table is designed to work as a comment in a .java class file. Open the file in eclipse (or any other IDE that can resolve class names in comments). You should be able to hover over the