Skip to content

Instantly share code, notes, and snippets.

@dimitardanailov
Last active December 25, 2016 16:05
Show Gist options
  • Save dimitardanailov/fca222d7b536fbe7c2e5f42ff5e7c101 to your computer and use it in GitHub Desktop.
Save dimitardanailov/fca222d7b536fbe7c2e5f42ff5e7c101 to your computer and use it in GitHub Desktop.
JDK 8 MOOC: Lambdas and Streams Introduction, 2016
// 1.1
() -> System.out.println("Hello Lambda")

// 1.2
x -> x + 10

// 1.3
(int x, int y) -> { return x + y; }

// 1.4
(String x, String y) -> x.length() - y.length()

// 1.5
(String x) -> {
  listA.add(x);
  listB.remove(x);
  
  return listB.size();
}

// 1.6
static T process(List<T> l, Comparator<T> c) // Example method defination

// Use the Method
List<String> list = getList();
process(list, (String x, String y) -> x.length() - y.length()); 

// Compiler is now smarter
String r = process(list, (x, y) -> x.length() - y.length())
// Consumer<T>
// Operation that takes a single value and return returns no result

String s -> System.out.println(s)
// Supplier
// The opposite of a Consumer

() -> createLogMessage()
// Function<T,R>
// A Function that accepts one argument and returns a result

Student s -> s.getName()
// UnaryOperator<T>
// specialised form of Function

String s -> s.toLowerCase()
// BinaryOperator<T>
// Specialised form of BiFunction

(String x, String y) -> {
  if (x.length() > y.length())
    return x;
  return y;
}
// Predicate
// A Boolean Valued Function of One Argument

Student s -> s.graduationYear() == 2011

// BiPredicate
Files.find(start, maxDepth,
  (path, attr) -> String.valueOf(path).endsWith(".js") && 
    attr.size() > 1024,
   FileVisitOption.FOLLOW_LINKS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment