Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Object in Java

Tags:

java

I wanna implement a javascript like method in java , is this possible ?

Say , I have a Person class :

public class Person {
 private String name ;
 private int age ;
 // constructor ,accessors are omitted
}

And a list with Person objects:

Person p1 = new Person("Jenny",20);
Person p2 = new Person("Kate",22);
List<Person> pList = Arrays.asList(new Person[] {p1,p2});

I wanna implement a method like this:

modList(pList,new Operation (Person p) {
  incrementAge(Person p) { p.setAge(p.getAge() + 1)};
});

modList receives two params , one is a list , the other is the "Function object", it loops the list ,and apply this function to every element in the list. In functional programming language,this is easy , I don't know how java do this? Maybe could be done through dynamic proxy, does that have a performance trade off compares to native for loop ?

like image 554
Sawyer Avatar asked Apr 08 '10 12:04

Sawyer


People also ask

What is function identity () in Java?

identity. static <T> Function<T,T> identity() Returns a function that always returns its input argument. Type Parameters: T - the type of the input and output objects to the function Returns: a function that always returns its input argument.

What is the use of function object?

A function object, or functor, is any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator. The C++ Standard Library uses function objects primarily as sorting criteria for containers and in algorithms.

Is object and function same?

An object is a collection of functions and data. A function is a collection of commands and data. When a bunch of functions work together to perform a certain task we may call this community of functionality an object.

What is type of function in Java?

There are two types of methods in Java: Predefined Method. User-defined Method.


2 Answers

You can do it with an interface and an anonymous inner class implementing it, like

Person p1 = new Person("Jenny",20);
Person p2 = new Person("Kate",22);
List<Person> pList = Arrays.asList(p1, p2);

interface Operation {
  abstract void execute(Person p);
}

public void modList(List<Person> list, Operation op) {
  for (Person p : list)
    op.execute(p);
}

modList(pList, new Operation {
  public void execute(Person p) { p.setAge(p.getAge() + 1)};
});

Note that with varargs in Java5, the call to Arrays.asList can be simplified as shown above.

Update: A generified version of the above:

interface Operation<E> {
  abstract void execute(E elem);
}

public <E> void modList(List<? extends E> list, Operation<E> op) {
  for (E elem : list)
    op.execute(elem);
}

modList(pList, new Operation<Person>() {
    public void execute(Person p) { p.setAge(p.getAge() + 1); }
});

Note that with the above definition of modList, you can execute an Operation<Person> on e.g. a List<Student> too (provided Student is a subclass of Person). A plain List<E> parameter type would not allow this.

like image 98
Péter Török Avatar answered Oct 11 '22 05:10

Péter Török


Take a look at the lambdaj project. Here is an example from the project home page:

List<Person> personInFamily = asList(new Person("Domenico"), new Person("Mario"), new Person("Irma"));
forEach(personInFamily).setLastName("Fusco");
like image 42
Andrea Polci Avatar answered Oct 11 '22 05:10

Andrea Polci