Tuesday, June 7, 2016

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));
    }
    
}



No comments:

Post a Comment