Last active
August 29, 2015 13:56
-
-
Save aNNiMON/9289188 to your computer and use it in GitHub Desktop.
Java 8 examples
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
package com.app1; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.time.LocalDate; | |
import java.time.LocalTime; | |
import java.time.Month; | |
import java.time.Period; | |
import java.time.temporal.ChronoUnit; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
/** | |
* @author aNNiMON | |
*/ | |
public class Java8Examples { | |
public static void main(String[] args) { | |
/*new Thread(new Runnable() { | |
public void run() { | |
counter(); | |
} | |
}).start();*/ | |
new Thread(Java8Examples::counter).start(); | |
listDemo(); | |
timeDemo(); | |
} | |
private static void listDemo() { | |
List<Integer> list = new ArrayList<>(); | |
final Random rnd = new Random(); | |
/*for (int i = 0; i < 20; i++) { | |
list.add(rnd.nextInt(100)); | |
}*/ | |
rnd.ints(0, 100).limit(20).forEach(list::add); | |
System.out.println("Sorted list"); | |
list.stream().sorted().forEach(p -> System.out.print(p + " ")); | |
System.out.println(); | |
int min = list.stream().min((Integer o1, Integer o2) -> o1.compareTo(o2)).get(); | |
System.out.println("Min: " + min); | |
int max = list.stream().max((Integer o1, Integer o2) -> o1.compareTo(o2)).get(); | |
System.out.println("Max: " + max); | |
int median = (max + min) / 2; | |
System.out.println("List with values above " + median); | |
list.stream().filter(p -> p >= median).forEach(p -> System.out.print(p + " ")); | |
System.out.println(); | |
} | |
private static void timeDemo() { | |
System.out.println(); | |
System.out.println(Instant.now()); | |
System.out.println(LocalTime.now()); | |
System.out.println(LocalDate.now().plus(5, ChronoUnit.WEEKS)); | |
LocalDate date2000 = LocalDate.of(2000, Month.JANUARY, 1); | |
System.out.println(Period.between(LocalDate.now(), date2000)); | |
System.out.println(); | |
} | |
private static void counter() { | |
Instant now = Instant.now(); | |
int counter = 0; | |
while (counter <= 10) { | |
System.out.println(counter); | |
counter++; | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException ex) { } | |
} | |
System.out.println("Elapsed time: " + Duration.between(Instant.now(), now)); | |
} | |
private static void multiplicationTable() { | |
// Prints "4" "6" | |
/*IntStream.range(1, 10) | |
.flatMap(i -> IntStream.range(1, 10).map(j -> i * j)) | |
.forEach(System.out::println);*/ | |
// Prints "2 * 2 = 4" "2 * 3 = 6" | |
IntStream.range(1, 10) | |
.mapToObj(String::valueOf) | |
.flatMap(i -> IntStream.range(1, 10) | |
.mapToObj(j -> String.format("%s * %d = %d", i, j, Integer.parseInt(i) * j) + (j == 9 ? "\n" : ""))) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment