Menu
  • HOME
  • TAGS

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

java,java-8,functional-interface

There's no such syntax construction you're asking for. However you can create a static constant where you explicitly assign the method reference to your interface: class LongHashes { private static final LongHasher XOR_HASH = LongHashes::xorHash; private static final LongHasher CONTINUING_HASH = LongHashes::continuingHash; private static final LongHasher RANDOM_HASH = LongHashes::randomHash; static...

how to pass lambda expression with arguments as parameters in Java 8?

lambda,java-8,functional-interface

Your handleOperation method takes an object that implements Function, Operation::add (a method reference) doesn't qualify. Also, for two arguments, you'll need to use BiFunction instead. Here's an example that should work: public class LambdaExample { public static Integer handleOperation(Integer x, Integer y, BiFunction<Integer, Integer, Integer> converter){ return converter.apply(x,y); } public...

Is lambda expression actually implementing java interface under the hood?

lambda,java-8,functional-interface

Yes, Comparator is a functional interface

Using streams with custom functional interfaces

java,lambda,java-8,java-stream,functional-interface

List<Person> list = roster.parallelStream().filter((p) -> p.getAge() > 18).collect(Collectors.toList()); what if I want to reuse the filter logic, e.g. having a method "isAdult" in Person? List<Person> list = roster.parallelStream().filter(Person::isAdult).collect(Collectors.toList()); or List<Person> list = roster.parallelStream().filter(p -> p.isAdult()).collect(Collectors.toList()); I would not be able to create a filter which accepts and additional int parameter...

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

java,functional-programming,functional-interface

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...

Generic FunctionalInterface and Method Reference messed up by Type Erasure

java,generics,java-8,type-erasure,functional-interface

FooList (without a type argument) is called a raw type. 4.8. Raw Types says this: The superclasses (respectively, superinterfaces) of a raw type are the erasures of the superclasses (superinterfaces) of any of the parameterizations of the generic type. This means that a raw FooList is just a raw ArrayList...

Functional Interface Inheritance Quirk

java,lambda,java-8,default-method,functional-interface

As stated in the comments, it compiles fine with the oracle compiler. It is an eclipse bug. Awaiting for a bug fix, personally i will remove the annotation @FunctionalInterface (your 3rd variation): public interface Function<T, R> extends java.util.function.Function<T, R>, com.google.common.base.Function<T, R> { R call(T input); @Override default R apply(T input)...

Can you call the parent interface's default method from an interface that subclasses that interface? [duplicate]

java,java-8,default-method,functional-interface

You can invoke an inherited interface default method using InterfaceName.super. The rules are the same as for other super method invocations: You can invoke the inherited method that you have overridden, but you can’t directly invoke the method which the inherited method might have overridden. E.g. interface A { void...

Why it isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?

java,functional-programming,java-8,functional-interface

Well, an annotation documenting an intention would be useless if you assume that there is always that intention given. You named the example AutoCloseable which is obviously not intended to be implemented as a function as there’s Runnable which is much more convenient for a function with a ()->void signature....

java 8 fuction invocation

java,lambda,functional-interface

If you're a long time Java coder, the new lambda stuff can be confusing -- it was(is?) for me. Lambdas are a contextual construct that the compiler interprets based on the context where it is used, just like a lot of other scripting languages. For most cases, you never need...

Java 8 Supplier with arguments in the constructor

java,lambda,functional-programming,java-8,functional-interface

That's just a limitation of the method reference syntax -- that you can't pass in any of the arguments. It's just how the syntax works.

Use method reference with parameter

java,lambda,java-stream,method-reference,functional-interface

You can’t use method references for this purpose. You have to resort to lambda expressions. The reason why the bind2 method of the linked question doesn’t work is that you are actually trying to bind two parameters to convert a three-arg function into a one-arg function. There is no similarly...

Compose Java-8-Functional-Interface

java,functional-programming,java-8,functional-interface

There is no reason why the compose method has to come from the Function interface. For your case the Function interface is not appropriate as Function has a return value (rather than void) and it’s compose method is intended to feed the result of one function into the next. Just...

Is there a way to turn an existing interface into a functional interface?

java,lambda,java-8,functional-interface

In order to reuse an existing interface that is not a functional interface in a lambda expression, you must also use the new Java8 feature, default methods. In this case, if you wanted to use a lambda expression in place of the anonymous class, you would have to do the...

Forward call on a functional interface using method handles

java,reflection,methodhandle,functional-interface

While in principle, you can use MethodHandles to construct combining handles delegating to multiple targets, there is no way to provide it in form of an efficient implementation of the functional interface. LambdaMetafactory creates efficient implementation but currently supports direct method handles only, thus you can’t use a combining handle...

Functional Interfaces in Java 8 (Method execution time logger)

java,java-8,functional-interface

Instead of Supplier<Void> you should use a plain Runnable, and instead of insisting on method references you'll need an explicit lambda which contains the method invocation, capturing any arguments it needs. For example: logMethodExecTime(() -> m2(17, 37)); Note that the lambda is not necessarily just a single method invocation. This...