Java example using Supplier to get input from Scanner

Java example using java.util.function.Supplier to get input from java.util.Scanner.

JavaSupplier.java

package javasupplier;

import java.util.Scanner;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class JavaSupplier {

public static void main(String[] args) {
Supplier<String> msg = ()-> "http://java-buddy.blogspot.com/";
System.out.println(msg.get());
System.out.println();

Scanner scanner = new Scanner(System.in);
Supplier<String> scannerNext = () -> scanner.next();
System.out.println("Enter something, 'q' to quit");

Stream.generate(scannerNext)
.map(s -> {
System.out.println(s);
return s;
})
.allMatch(s -> !"q".equals(s));
}

}


Source: http://java-buddy.blogspot.com/

Be the first to comment

Leave a Reply

Your email address will not be published.


*