6

I'm having problem in replacing this particular example:

Consumer consumer = new DefaultConsumer(channel) {
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
            throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    }
};

Is it possible to replace that with lambda as it uses non-default constructor for DefaultConsumer?

It's from rabbitMQ java tutorial -> LINK to whole class

4
  • It depends on DefaultConsumer. Is there more than one abstract method ? How are you using Consumer consumer ? Commented Nov 14, 2017 at 13:35
  • @AxelH rabbitmq.com/releases/rabbitmq-java-client/current-javadoc/com/… Commented Nov 14, 2017 at 13:36
  • @slim, thanks, that's answer my questions, nope it can't be used for it! Commented Nov 14, 2017 at 13:40
  • If you do this for multiple DefaultConsumers though, you could create a helper method, taking a lambda as input and returning an anonymous subclass of Defaultconsumer. Commented Nov 14, 2017 at 13:43

1 Answer 1

7

No you can't. DefaultConsumer is not a FunctionalInterface (and can't be: more info here) hence is not a lambda target.

Explanation:

Can every anonymous class be replaced by a lambda expression?

The answer is no. You can create an anonymous class for non-final classes and interfaces. Is not the same for lambda expressions. These can be only used where a SAM interface is expected, i.e. interfaces with only a Single Abstract Method (before Java 8 every interface method was abstract but since Java 8 interfaces can also have default and static methods which aren't abstract because they have implementation).

So which anonymous classes can be replaced with a lambda expression?

Only anonymous classes which are implementations of a SAM interface (like Runnable, ActionListener, Comparator, Predicate) can be replaced by a lambda expression. DefaultConsumer cannot be a lambda target because is not even an Interface.

And what about Consumer?

Even though Consumer is an Interface, it is not a SAM Interface because it has more than 1 abstract method so it can't be a lambda target either.

Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't change much on the result here but FunctionalInterface is not mandatory .
An interface can be a "functional interface" without being annotated @FunctionInterface -- but DefaultConsumer is not a functional interface for a number of reasons -- not an interface; multiple methods none of which is abstract...
@AxelH I know but it needs to be a SAM type anyway to be a lamda target
I know, that's why this "doesn't change much on the result" ;) but the explanation is a bit broad, I prefer your comment as an explanation. Because your answer don't do the difference between "is not a FunctionnalInterface" and "can not be a FunctionnalInterface" since the annotation itself is optional.
@AxelH understood. Edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.