Last active
August 29, 2015 14:11
-
-
Save madhub/4022b6b8478e0cae2338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package demo; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Random; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
/** | |
* | |
* @author madhub | |
*/ | |
public class Java8Samples { | |
public static void main(String[] args) { | |
List<String> names = Arrays.asList("Rachel Green", "Monica Geller", "Phoebe Buffay", | |
"Joey Tribbiani", " Chandler Bing", " Ross Geller "); | |
// create list of string | |
List<String> nCopies = Collections.nCopies(10, "Google") | |
.forEach(value -> System.out.println(value)); | |
// on any collection , you can use foreach | |
names.forEach(value -> System.out.println(value)); | |
// or | |
names.forEach(System.out::println); | |
// or | |
// Removes all of the elements of this collection that satisfy the given predicate | |
//names.removeIf(name -> name.contains("ll")); | |
// Replaces each element of this list with the result of applying the operator to that element | |
names.replaceAll(name -> name + ","); | |
Map<String, Integer> authorBooks = new HashMap<String, Integer>(); | |
authorBooks.put("Robert Ludlum", 27); | |
authorBooks.put("Clive Cussler", 50); | |
authorBooks.put("Tom Clancy", 17); | |
authorBooks.forEach((a, b) -> System.out.println(a + " wrote " + b + " books")); | |
Integer orDefault = authorBooks.getOrDefault("AuthorA", 0); | |
Stream<Double> generate = Stream.generate(() -> Math.random()); | |
generate.limit(5).forEach(System.out::println); | |
System.out.println(""); | |
} | |
public static void moreLambdaSamples() { | |
Arrays.asList("a1", "a2", "a3") | |
.stream() | |
.findFirst() | |
.ifPresent(System.out::println); // a1 | |
// Stream.of() to create a stream from a bunch of object references. | |
Stream.of("a1", "a2", "a3") | |
.findFirst() | |
.ifPresent(System.out::println); // a1 | |
// Java 8 ships with special kinds of streams for working with the primitive data types int, long and double | |
IntStream.range(1, 4) | |
.forEach(System.out::println); | |
Arrays.stream(new int[]{1, 2, 3}) | |
.map(n -> 2 * n + 1) | |
.average() | |
.ifPresent(System.out::println); // 5.0 | |
// transform a regular object stream to a primitive stream or vice versa. | |
// For that purpose object streams support the special mapping operations mapToInt(), mapToLong() and mapToDouble: | |
Stream.of("a1", "a2", "a3") | |
.map(s -> s.substring(1)) | |
.mapToInt(Integer::parseInt) | |
.max() | |
.ifPresent(System.out::println); // 3 | |
Stream.of("d2", "a2", "b1", "b3", "c") | |
.sorted((s1, s2) -> { | |
System.out.printf("sort: %s; %s\n", s1, s2); | |
return s1.compareTo(s2); | |
}) | |
.filter(s -> { | |
System.out.println("filter: " + s); | |
return s.startsWith("a"); | |
}) | |
.map(s -> { | |
System.out.println("map: " + s); | |
return s.toUpperCase(); | |
}) | |
.forEach(s -> System.out.println("forEach: " + s)); | |
parallelStreamDemo(); | |
} | |
public static void parallelStreamDemo() { | |
Arrays.asList("a1", "a2", "b1", "c2", "c1") | |
.parallelStream() | |
.filter(s -> { | |
System.out.format("filter: %s [%s]\n", | |
s, Thread.currentThread().getName()); | |
return true; | |
}) | |
.map(s -> { | |
System.out.format("map: %s [%s]\n", | |
s, Thread.currentThread().getName()); | |
return s.toUpperCase(); | |
}) | |
.forEach(s -> System.out.format("forEach: %s [%s]\n", | |
s, Thread.currentThread().getName())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment