Is it possible to store a method into a variable? Something like
public void store() {
SomeClass foo = ;
//...
String
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- items = new ArrayList
- ();
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
- 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);
}
}