Asked  6 Months ago    Answers:  5   Viewed   22 times

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions.

Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.

For example:

@FunctionalInterface
interface MathOperation {
    int operation(int a, int b);
}

How useful it is in Java 8 other than just working with lambda expressions?

(The question here is different from the one I asked. It is asking why we need functional interfaces while working with lambda expressions. My question is: What are the other uses of functional interfaces besides use with lambda expressions?)

 Answers

10

@FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.

But you can use lambdas without this annotation as well as you can override methods without @Override annotation.

From docs

a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere

This can be used in lambda expression:

public interface Foo {
  public void doSomething();
}

This cannot be used in lambda expression:

public interface Foo {
  public void doSomething();
  public void doSomethingElse();
}

But this will give compilation error:

@FunctionalInterface
public interface Foo {
  public void doSomething();
  public void doSomethingElse();
}

Invalid '@FunctionalInterface' annotation; Foo is not a functional interface

Tuesday, June 1, 2021
 
millenomi
answered 6 Months ago
28
Stream.of("AAA","BBB","CCC").parallel().forEach(s->System.out.println("Output:"+s));
Stream.of("AAA","BBB","CCC").parallel().forEachOrdered(s->System.out.println("Output:"+s));

The second line will always output

Output:AAA
Output:BBB
Output:CCC

whereas the first one is not guaranted since the order is not kept. forEachOrdered will processes the elements of the stream in the order specified by its source, regardless of whether the stream is sequential or parallel.

Quoting from forEach Javadoc:

The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism.

When the forEachOrdered Javadoc states (emphasis mine):

Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.

Wednesday, June 2, 2021
 
Pegues
answered 6 Months ago
69

It doesn't mean anything special.

But because $ is allowed in identifier names, many Javascript libraries have taken to using $ as the "central" interface to them, or at least as a shortcut for accessing their functionality.

For example, if you're using jQuery and you say $("div"), this is a call to the $ function with argument "div". When you say $.post(), it's calling the post method on the $ object (Javascript is nice in that functions are first-class objects).

Wednesday, July 14, 2021
 
Floris
answered 5 Months ago
44

Both Stream#filter and Stream#map are intermediate operations, which means that they are evaluated lazily. According to the documentation:

Intermediate operations return a new stream. They are always lazy; executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.

In any case, you should be using the appropriate methods to avoid errors like this; forEach should be used instead of map here as Stream#map is used to convert the stream to the result of calling the mapping function on each element, while Stream#forEach is used to iterate over it.

Demo: https://ideone.com/ZQhLJC

strings
  .stream()
  .filter(x -> !distinct.add(x))
  .forEach(extras::add);

Another possible workaround is to perform a terminal operation like .collect to force the filter and map to be applied.

strings
  .stream()
  .filter(x -> !distinct.add(x))
  .map(extra -> extras.add(extra)).collect(Collectors.toList());

If you are going to use .collect, you might as well use the collected list as extras to avoid wasting time and space.

List<String> extras = strings
  .stream()
  .filter(x -> !distinct.add(x)).collect(Collectors.toList());
Monday, August 23, 2021
 
motanelu
answered 3 Months ago
65

This is because you're not calling the BiConsumer's accept method. call it as follows:

  bc.accept(list1, list2);

Further, note that it's not necessary to call stream just to call forEach instead call forEach directly on the list:

 lista.forEach(System.out::print);

Another thing is that your BiConsumer doesn't use the second list, this may be because you haven't finished implementing the entire logic yet in which case it's understandable.

Complete code:

BiConsumer<List<String>, List<String>> bc = (lista, listb) -> {
    lista.forEach(System.out::print);
    // listb.forEach(System.out::print);
};
bc.accept(list1, list2);
Wednesday, November 10, 2021
 
George Armhold
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share