Is it possible to store a method into a variable? Something like
public void store() {
SomeClass foo = ;
//...
String
You can use Java 8 method references. You can use the ::
'operator' to grab a method reference from an object.
import java.util.function.IntConsumer;
class Test {
private int i;
public Test() { this.i = 0; }
public void inc(int x) { this.i += x; }
public int get() { return this.i; }
public static void main(String[] args) {
Test t = new Test();
IntConsumer c = t::inc;
c.accept(3);
System.out.println(t.get());
// prints 3
}
}
You just need a @FunctionalInterface
that matches the signature of the method you want to store. java.util.function
contains a selection of the most commonly used ones.
Yes, you can have a variable reference to any method. For simple methods it's usually enough to use java.util.function.* classes. Here's a working example:
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
final Consumer<Integer> simpleReference = Main::someMethod;
simpleReference.accept(1);
final Consumer<Integer> another = i -> System.out.println(i);
another.accept(2);
}
private static void someMethod(int value) {
System.out.println(value);
}
}
If your method does not match any of those interfaces, you can define your own. The only requirement is that is must have a single abstract method.
public class Main {
public static void main(String[] args) {
final MyInterface foo = Main::test;
final String result = foo.someMethod(1, 2, 3);
System.out.println(result);
}
private static String test(int foo, int bar, int baz) {
return "hello";
}
@FunctionalInterface // Not required, but expresses intent that this is designed
// as a lambda target
public interface MyInterface {
String someMethod(int foo, int bar, int baz);
}
}
You can use method reference like -
System.out::println
Which is equivalent to the lambda expression -
x -> System.out.println(x).
Moreover you can user reflection to store method and it will works for the earlier version of java
too.
Maybe you can try java.util.stream from Java 8
public class Item {
public Item(int id, String title, String language){
this.id=id;
this.title=title;
this.language=language;
}
private int id;
private String title;
private String language;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
The following code snippet shows how to filter the data by stream:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Item> items = new ArrayList<Item>();
items.add(new Item(1, "t1", "english"));
items.add(new Item(2, "t2", "english"));
items.add(new Item(3, "t3", "Japanese"));
// get items 1 and 2
List<Item> result1 = items.stream()
.filter(n->n.getLanguage()=="english")
.collect(Collectors.toList());
// get item 1
Item result2 = items.stream()
.filter(n->n.getId()==1)
.findFirst().orElse(null);
}
}