Code: Select all
>>> def test():
print('Test')
>>> testList = []
>>> testList.append(test)
>>> testList[0]()
Test
Code: Select all
>>> def test():
print('Test')
>>> testList = []
>>> testList.append(test)
>>> testList[0]()
Test
Code: Select all
import java.util.*;
import java.util.function.*;
public class SSCCE { // simple self contained compilable example
public static void main(String... args) {
// returns the argument unchanged
UnaryOperator<String> u1 = UnaryOperator.identity();
// returns the argument changed to lower case
UnaryOperator<String> u2 = String::toLowerCase;
// returns the argument's characters reversed
UnaryOperator<String> u3 = f -> {
return new StringBuffer(f).reverse().toString();
};
// returns the argument's characters reversed and then to lower case
Function<String,String> u4 = u3.andThen(u2); // or .compose
// creates a list to hold functions
List<Function<String,String>> list = new ArrayList<>();
// add the functions to the list
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
// apply the functions to the arguments
System.out.print(list.get(0).apply("Hello"));
System.out.println(list.get(1).apply(" WORLD!"));
System.out.println(list.get(2).apply("SRETTEL EHT ESREVER"));
System.out.println(list.get(3).apply(
"ESAC REWOL OT SRETTEL EHT ESREVER"));
}
}
Code: Select all
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("test");
System.out.println(arrlist);
Code: Select all
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Object 1");
arrayList.add("Object 2");
System.out.println(arrayList);