Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's equivalents of Func and Action

Tags:

java

What are Java's equivalents of Func and Action?

I mean, instead of writing this on my own:

public interface Func<TInput, TResult> {     TResult call(TInput target) throws Exception; } public interface Action<T> {     void call(T target) throws Exception; } 
like image 814
ripper234 Avatar asked Jul 26 '09 12:07

ripper234


People also ask

What is the equivalent of functions in Java?

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

What is a Functional interface?

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.


2 Answers

In Java 8, the equivalents are the java.util.function.Function<T, R> and java.util.function.Consumer<T> interfaces respectively. Similarly, java.util.function.Predicate<T> is equivalent to System.Predicate<T>. As mentioned elsewhere, these are interfaces instead of delegates.

Related aside: I'm currently leaning heavily on the following utility class to do LINQ-like extension method stuff:

abstract class IterableUtil {   public static <T> Iterable<T> where(Iterable<T> items, Predicate<T> predicate) {     ArrayList<T> result = new ArrayList<T>();     for (T item : items) {       if (predicate.test(item)) {         result.add(item);       }     }     return result;   }    public static <T, R> Iterable<R> select(Iterable<T> items, Function<T, R> func) {     ArrayList<R> result = new ArrayList<R>();     for (T item : items) {       result.add(func.apply(item));     }     return result;   } } 

Unlike System.Linq.Enumerable.Where<TSource> and System.Linq.Enumerable.Select<TSource, TResult> the LINQ-like methods I present here are not lazy and fully traverse the source collections before returning the result collections to the caller. Still, I find them useful for purely syntactic purposes and could be made lazy if necessary. Given

class Widget {   public String name() { /* ... */ } } 

One can do the following:

List<Widget> widgets = /* ... */; Iterable<Widget> filteredWidgets = IterableUtil.where(widgets, w -> w.name().startsWith("some-prefix")); 

Which I prefer to the following:

List<Widget> widgets = /* ... */; List<Widget> filteredWidgets = new ArrayList<Widget>(); for (Widget w : widgets) {   if (w.name().startsWith("some-prefix")) {     filteredWidgets.add(w);   } } 
like image 54
Richard Cook Avatar answered Sep 28 '22 06:09

Richard Cook


Callable interface is similar to Func.

Runnable interface is similar to Action.

In general, Java uses anonymous inner classes as a replacement for C# delegates. For example this is how you add code to react to button press in GUI:

button.addActionListener(new ActionListener() {       public void actionPerformed(ActionEvent e) {            ...//code that reacts to the action...        } }); 
like image 27
Gregory Mostizky Avatar answered Sep 28 '22 05:09

Gregory Mostizky