Questions tagged [functional-interface]

A functional interface in the Java language refers to an interface with a single abstract method. @FunctionalInterface is an annotation which requires a particular interface declaration to conform to this specification. The target type of a lambda expression or method reference must be a functional interface. Functional interfaces are part of the Java 8 feature set.

A functional interface in the language refers to an interface with a single abstract method. @FunctionalInterface is an annotation which requires the interface to conform to this specification. The target type of a lambda expression or method reference must be a functional interface. Functional interfaces are part of the feature set.

The following is a simple functional interface:

@FunctionalInterface
interface StringFunction<T> {
    String applyToString(T obj);
}

The single abstract method declaration allows it to be the target of a lambda expression:

StringFunction<Integer> toHexStringFn =
    (Integer n) -> Integer.toHexString(n);

If the interface has more than one abstract method, it is not longer a functional interface and can no longer be the target of a lambda.

Functional interfaces should be annotated with the @FunctionalInterface annotation, which requires the interface to conform to the functional interface specification. An interface annotated with @FunctionalInterface will cause a compilation error if the interface has e.g. more than one abstract method. (This is similar to the behavior of the @Override annotation, which will cause a compilation error if a method override is incorrect.)

See also:

571 questions
-2
votes
1 answer

FunctionalInterfaces does not count Object class Method

@FunctionalInterface interface Superman { //public methods in java.lang.Object class String toString(); int hashCode(); boolean equals(Object obj); //Protected method in java.lang.Object class //Object clone() throws…
Ng Sharma
  • 2,072
  • 8
  • 27
  • 49
-2
votes
2 answers

Execute functional interface as lambda

I'm not sure if the title really suits what I want to convey. Let me explain it with an example: public static void test() { Consumer c = System.out::println; c.accept("hello world"); } I want to know if there is a more…
beni0888
  • 1,050
  • 1
  • 12
  • 40
-2
votes
1 answer

How to pass a function through parameters using generics

Given the following code: static List map(Iterable l, Function f) { return null; } I need to pass an arraylist in the first parameter and a hash function in the second which takes a string and outputs a type int. I'm trying…
-2
votes
1 answer

List of String to Map Java8

I have a list of String in my application. Every String is actually an output of 2 different strings concatenated by ".". e.g. Stack.Overflow Now, I am trying to convert this list to a Map with key being the first part of the string and value being…
user3262365
  • 75
  • 1
  • 10
-2
votes
1 answer

Is there a solution for the match-mapping-group pattern in python by Functional programming?

For example, I have a list such as [2, 4, 7, 9, 6, 12, 38]. I want to firstly recognize each number by whether it is odd, then add 100 to each odd and 101 to each even,finally get two lists. There are 3 steps: 1. Number matching that it is odd or…
-2
votes
1 answer

Repeated overriding of functional interface's abstract method?

I have a functional interface in Java 8: public interface IFuncLambda1 { public int someInt(); } in main: IFuncLambda1 iFuncL1 = () -> 5; System.out.println("\niFuncL1.someInt: " + iFuncL1.someInt()); iFuncL1 = () ->…
Ade
  • 5
  • 1
-3
votes
1 answer

Correct way to apply different fucntions based on condition in stream

What would be the correct way to implement this functional logic with streams? Stream through list of items Check if condition 1 passes, if so apply function 1 to the item. Check if condition 2 passes, if so apply function 2 to the item. If both…
ems19
  • 23
  • 6
-3
votes
2 answers

How to write a generic Java functional interface that covers as many use cases as possible?

There are many Java core functional interfaces. I often need a slight variation with different arguments, e.g. for a custom listener with a custom event type, and none of the core classes fit. Is there a generic way of writing a functional interface…
Reto Höhener
  • 5,419
  • 4
  • 39
  • 79
-3
votes
2 answers

incompatible types: bad return type in lambda expression | void is not a functional interface

I am going through Java 8 Feature and using Jshell to explore it. I tried to run the below command: Consumer consumer = (str)->System.out::println; But it failed with the below error: Error: | incompatible types: bad return type in lambda…
Manish
  • 1,274
  • 3
  • 22
  • 59
-3
votes
1 answer

What does it mean? Java

something().orElseGet(() -> (a, b, c) -> {}) // ^----this part------^ where (a, b, c) is a method with a, b and c parameters. e.g: Method(a, b, c) which returns something. My question is in practice what does this functional…
pandita
  • 43
  • 1
  • 5
-3
votes
1 answer

Access object with functional interfaces

The following situation: I have three classes A, B and C. A is some sort of parent which means A has a reference to the instance of B as well as C. B does not have a reference to the instance of C and C has no reference to the instance of B. B has…
PeterPan
  • 684
  • 1
  • 10
  • 27
-3
votes
2 answers

whats is alternate solution of fluent wait wait.until() method in selenium 3.x.x?

whats is alternate solution of fluent wait wait.until() method in selenium 3.x.x ? It is giving some functional reference for util method. Please guide.
-4
votes
1 answer

Java: How to replace multiple nested if-else statements with a more maintainable design

I need to execute a different action doAction1(), doAction2() ... depending on the value and/or type of some variables and return value of some functions. All combinations could be possible, in principle. The quick solution is to test all…
-4
votes
2 answers

Why we do need to write "implements InterfaceName" while implementing a Functional Interface?

Why we do need to write "implements FI" while implementing a FunctionalInterface? @FunctionalInterface interface FI { void sayHello(String name); } public class Hello implements FI { public static void main(String[] args) { // TODO…
-5
votes
1 answer

Which functional interface to use if method don't accept anything but return object of the class in java

I have one existing Java 7 method which accepts nothing but return class instance. I want to change it in java 8 using any existing functional interface but don't know what i can use here. public NotificationPage infraSelection() { ...... return…
1 2 3
38
39