functional-interface

Why Functional Interfaces in Java 8 have one Abstract Method?

人盡茶涼 提交于 2019-12-17 07:33:30
问题 As we know in Java 8, the concept of functional interfaces are introduced. A Functional Interface has one abstract method and several default or static methods are possible. But why should a Functional interface have only one abstract method? If Interface has more then one abstract method, why is this not a Functional Interface? 回答1: The functional interface also known as Single Abstract Method Interface was introduced to facilitate Lambda functions. Since a lambda function can only provide

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

What purpose is served by the `SerializableFunction` interface defined in Vaadin 8 if we can simply pass a regular Lambda expression in its place?

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:47:56
问题 Vaadin 8 defines a functional interface, SerializableFunction. This interface appears in various places. For example, when defining a Converter for displaying non-textual types in a TextField such as a UUID. Binder.BindingBuilder<BEAN,TARGET>::withConverter(SerializableFunction<TARGET,NEWTARGET> toModel, SerializableFunction<NEWTARGET,TARGET> toPresentation) See class documentation. Example usage: binder .forField( this.idField ) .withConverter( text -> UUID.fromString( text ) , uuid -> uuid

How to configure the bindings of a function in Spring Cloud Stream to bind its input to a web endpoint and its output to a Kafka topic

烂漫一生 提交于 2019-12-12 11:35:02
问题 I have a regular java Function ; which I am trying to bind: Its input to a web endpoint Its output to a kafka topic. When I use my function in the context of the web, it always returns the resulting value of the Function back to the web client alone. Can I do something like this?: spring.cloud.stream.bindings.input.binder=web spring.cloud.stream.bindings.output.binder=kafka I'm currently even trying to split the Function into 2: One with its input bound to the web client and its output

Why doesn't for-each method in java not throw an exception when a Function type argument is passed instead of Consumer? [duplicate]

烈酒焚心 提交于 2019-12-10 19:44:53
问题 This question already has answers here : Why do Consumers accept lambdas with statement bodies but not expression bodies? (3 answers) Lambda 'special void-compatibility rule' - statement expression (3 answers) Why does a Java method reference with return type match the Consumer interface? (2 answers) Closed last year . Why doesn't forEach method in java not show a compiler error when a Function type argument is passed instead of Consumer? Here both of the lines are returning a boolean value

Converting array iteration to lambda function using Java8

谁说胖子不能爱 提交于 2019-12-10 10:22:13
问题 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)

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

不羁岁月 提交于 2019-12-09 05:16:39
问题 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! 回答1:

Java 8 streams, why does this compile part 2… Or what is a method reference, really?

自闭症网瘾萝莉.ら 提交于 2019-12-09 04:32:50
问题 OK, the first question in this "series" was this one. Now, here is another case: Arrays.asList("hello", "world").stream().forEach(System.out::println); This compiles, and works... OK, in the last question, static methods from a class were used. But now this is different: System.out is a static field of System , yes; it is also a PrintStream , and a PrintStream has a println() method which happens to match the signature of a Consumer in this case, and a Consumer is what forEach() expects. So I

How to overload a method by method with parameters list that contains parameters of the exact same type but parametrized with other types

限于喜欢 提交于 2019-12-09 02:11:59
问题 I have a methods: public List<Integer> convertBy(Function<String, List<String>> flines, Function<List<String>, String> join, Function<String, List<Integer>> collectInts) { return collectInts.apply(join.apply(flines.apply((String) value))); }//first method public Integer convertBy(Function<List<String>, String> join, Function<String, List<Integer>> collectInts, Function<List<Integer>, Integer> sum) { return sum.apply(collectInts.apply(join.apply((List<String>) value))); }//second method

Lambda Expression is not working, getting terminated

末鹿安然 提交于 2019-12-07 05:43:56
问题 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");