Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用多个参数调用方法_Java_Java Stream - Fatal编程技术网

Java 使用多个参数调用方法

Java 使用多个参数调用方法,java,java-stream,Java,Java Stream,我已经使用Java8流有一段时间了。我遇到了一种情况,我需要通过一个列表流式传递每个元素给一个非静态类的方法 List<String> emps = new ArrayList<>(); emps.add("ABC"); emps.add("DEF"); emps.add("GHI"); 我试过这个,但不起作用 emps.stream().map(e-> generator.start(e)); ma

我已经使用Java8流有一段时间了。我遇到了一种情况,我需要通过一个列表流式传递每个元素给一个非静态类的方法

List<String> emps = new ArrayList<>();
emps.add("ABC");
emps.add("DEF");
emps.add("GHI");
我试过这个,但不起作用

emps.stream().map(e-> generator.start(e));


map
必须接受输入并将其转换为某种东西。
start
方法无效

这里不需要溪流。一个简单的
forEach
就可以了

emps.forEach(e-> generator.start(e)); 


有没有办法在这里使用stream?我知道没有必要,但我只是好奇上面案例的流实现
emp.stream().forEach(..)
。任何好的IDE都会警告您,此处不需要使用stream()。一个可接受的用法是,start方法返回一些东西,您可以使用它执行一些操作,如
emps.stream().map(生成器:start).collect(Collectors.toList())
public class EmpDataGenerator {

    // Used to signal a graceful shutdown
    private volatile boolean stop = false;
    private final ExecutorService executor;

    public EmpDataGenerator(ExecutorService executor) {
        this.executor = executor;
    }

    public void start(String name ) {
        Runnable generator = () -> {
            try {
                while (!stop) {
                    //do some processing
                 }
                System.out.println("Broke while loop, stop " + stop);
            } catch (Exception e) {
                System.out.println("EmpDataGenerator thread caught an exception and halted!");
                throw e;
            }
        };
        executor.execute(generator);
    }

    public void stop() {
        stop = true;

        // The shutdown the executor (after waiting a bit to be nice)
        try {
            executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            // Purposely ignore any InterruptedException
            Thread.currentThread().interrupt();
        }
        executor.shutdownNow();
    }

}
emps.forEach(e-> generator.start(e)); 
emps.forEach(generator::start);