Created
September 23, 2017 12:20
-
-
Save orende/9c95eef2c94bdac419c4e4ba17159b63 to your computer and use it in GitHub Desktop.
De mest intressanta nyheterna i Java 9 sammanfattade i en Java-klass
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 se.citerus; | |
import java.util.Optional; | |
import java.util.Scanner; | |
import java.util.concurrent.Flow; | |
import java.util.concurrent.SubmissionPublisher; | |
import java.util.stream.IntStream; | |
public class Java9Features { | |
private static class MySubscriber<String> implements Flow.Subscriber<String> { | |
private Flow.Subscription subscription; | |
@Override | |
public void onSubscribe(Flow.Subscription subscription) { | |
this.subscription = subscription; | |
subscription.request(1); | |
} | |
@Override | |
public void onNext(String item) { | |
System.out.println("Got: " + item); | |
subscription.request(1); | |
} | |
@Override | |
public void onError(Throwable t) { | |
t.printStackTrace(); | |
} | |
@Override | |
public void onComplete() { | |
System.out.println("Done"); | |
} | |
} | |
public static void main(String[] args) { | |
IntStream.rangeClosed(0, 10).takeWhile(value -> value <= 5).forEach(System.out::print); | |
System.out.println(); | |
IntStream.rangeClosed(0, 10).dropWhile(value -> value <= 5).forEach(System.out::print); | |
System.out.println(); | |
IntStream.iterate(0, value -> value <= 10, operand -> operand + 2).forEach(System.out::print); | |
System.out.println(); | |
Optional.ofNullable(null).ifPresentOrElse(System.out::println, () -> System.out.println("Nothing to see here")); | |
Optional.of("aaa").stream().map(String::length).forEach(System.out::println); | |
SubmissionPublisher<String> publisher = new SubmissionPublisher<>(); | |
MySubscriber<String> subscriber = new MySubscriber<>(); | |
publisher.subscribe(subscriber); | |
Scanner scan = new Scanner(System.in); | |
String input; | |
do { | |
input = scan.next(); | |
publisher.submit(input); | |
} while (!input.contains("quit")); | |
subscriber.subscription.cancel(); | |
publisher.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment