Thanks to helpful people all over the web I get a good taste of how things work now.
Here is little runnable example.
import java.util.Arrays; import java.util.Comparator; import java.util.List; public class TestingLambda { public static void main(String arg[]) { String[] nameArray = {"Johnny","Ken","Alan"}; ListLook, besides defining lambda function itself, there is now this forEach thing (no, not the same for loop thing as in Microsoft's languages). That arrow notation is gosh straight from mathematics. Look! that double colon! You may have seen that in C++. Nope not same use. So lambda let you define function on the fly, and stuff it onto sorting, for example. There is more to lambda than this little example.names = Arrays.asList(nameArray); // Using lambda expression and functional operations names.forEach((x) -> System.out.println("lambda thing: "+x)); System.out.println("-----"); System.out.println("The double colon"); // Using double colon operator in Java 8 names.forEach(System.out::println); Comparator byNameLength = (String o1, String o2) -> ( new Integer(o1.length()).compareTo( new Integer(o2.length()))); names.sort(byNameLength); System.out.println("-----"); System.out.println("sorted by name length"); // a good old way to list for (String name: names) { System.out.println(name); } } }
People just love define lots of things on the fly and stuff everything into 1 line.
No comments:
Post a Comment