Fava is a functional library for java, it's an extention for Java collection framework.
The two essential class is FList and FArrayList. FList extends JDK's List, FArrayList extends ArrayList
Create from exist list
List originalList = .... //JDK list
FList fList = new FArrayList(originalList);Because FArrayList extends ArrayList, you also can create it directly
List<String> fList = new FArrayList<String>();
fList.add("one"); //use jdk's functionSuppose you have a FList contains [1,2,3,4,5]
FList<Integer> ints = FLists.create(1,2,3,4,5);
Each operation
int.each(new Consumer<Integer>(){
public void apply(Integer e) {
System.out.print(e);
}
});Find the first element match the Predicate
int result = int.find(new Predicate<Integer>(){
public boolean apply(Integer e){
return e > 3;
}
});Find all elements match the Predicate
FList<Integer> results = ints.findAll(new Predicate<Integer>(){
public boolean apply(Integer e){
return e % 2 == 0; //the int value is even
}
}); Convert list to another different type list, use map() or collect()
FList<String> results = ints.map(new Function<Integer,String>(){
public String apply(Integer e){
return String.valueOf(e);
}
});Test is there any element match Predicate
boolean hasAnyBiggerThanThree = ints.any(new Predicate<Integer>(){
public boolean apply(Integer e) {
return e > 3;
}
});Test all elements match Predicate
boolean allBiggerThanThree = ints.all(new Predicate<Integer>(){
public boolean apply(Integer e) {
return e > 3;
}
});Fold operation, such as sum list
int result = ints.fold(0, new FoldFunction<Integer, Integer>(){
public Integer apply(Integer element, Integer init) {
return element + init;
}
});There are more functions such as reject, deleteIf, please see Fava' java doc.
There are three basic function shape in fava.
- Consumer - operate on object and not return
- Predicate - operate on object and return boolean value
- Function - operate on object and return value with other type
