列为流(如果存在)还是不更新列表?

时间:2019-06-17 14:44:08

标签: java javafx collections mapreduce java-stream

更新:我已经解决了这个问题

Optional<Collection<Pair<String, Integer>>> collResult = shufflingResult.stream().filter(c -> c.contains(uniquePair)).findAny();
        if(collResult.isPresent()) {
            collResult.ifPresent(c -> {
                for(int i = 0; i < pFrequencyCount; i++)
                    c.add(uniquePair);
            });
        }
        else {
            Collection<Pair<String, Integer>> newColl = new ArrayList<>();
            for(int i = 0; i < pFrequencyCount; i++)
                newColl.add(uniquePair);
            shufflingResult.add(newColl);
        }

但是我仍然想知道为什么ifPresentOrElse不更新列表,而ifPresent却更新列表。

我正在尝试实现类似MapReduce的框架,在改组阶段,我正在做如下所示的操作,但是当我通过collection消费者时 到ifPresentOrElse方法,该集合值不会在列表中更新

public class ExecutingController implements Initializable {

@FXML
Button startBtn;

private List<String> fileLines;
ExecutorService executor;
List<Collection<Pair<String, Integer>>> shufflingResult;

public ExecutingController() {
    fileLines = new ArrayList<>();
    shufflingResult = new ArrayList<>();
    executor = Executors.newCachedThreadPool();
}

private void init() {
    startBtn.setOnAction(e -> {
        fileLines = getInputFileLines();
        try {
            startMappingPhase();
        } catch (Exception ex) {
            ex.printStackTrace();
            Utils.showFatalError(ex.toString());
        }
    });
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    init();
}

private void startMappingPhase() throws Exception {
    for (String line : fileLines) {
        Future future = new ExecutingController().mapping(line);
        while (!future.isDone()) {
            //
        }
        shuffle((Collection<Pair<String, Integer>>) future.get());
    }
}

private void shuffle(Collection<Pair<String, Integer>> pairs) {
    // Get a unique set of pairs
    Set<Pair<String, Integer>> uniquePairs = new LinkedHashSet<>(pairs);

    // Loop through set
    for (Pair<String, Integer> uniquePair : uniquePairs) {

        // Get a unique pair frequency count
        int pFrequencyCount = Collections.frequency(pairs, uniquePair);

        // Get a stream of result list and check if it's collections has that unique pair if so append it
        // else make a new collection then append it
        shufflingResult.stream().filter(c -> c.contains(uniquePair)).findAny().ifPresentOrElse(new Consumer<Collection<Pair<String, Integer>>>() {
            @Override
            public void accept(Collection<Pair<String, Integer>> ps) {
                for (int i = 0; i < pFrequencyCount; i++)
                    ps.add(uniquePair); /* My problem here, I don't get the collection(ps) updated in the list(shufflingResult). */
            }
        }, new Runnable() {
            @Override
            public void run() {
                Collection<Pair<String, Integer>> newColl = new ArrayList<>();
                for (int i = 0; i < pFrequencyCount; i++)
                    newColl.add(uniquePair);
                shufflingResult.add(newColl);
            }
        });

    }
}

private Future<Collection<Pair<String, Integer>>> mapping(String segment) throws Exception {
    Path clsPath = Paths.get(Utils.getAppTempFolder(), "Mapping.class");
    Invoker invoker = new Invoker(clsPath, "Mapping", "mapper");
    return executor.submit(() -> {
        return (Collection<Pair<String, Integer>>) invoker.invoke(segment);
    });
}

private List<String> getInputFileLines() {
    return Utils.getFileLines(Paths.get(Utils.getAppTempFolder(), "input.txt").toFile());
}

}

但是,如果我只调用带有lambda集合的ifPresent,则会在列表中进行更新。

shufflingResult.stream().filter(c -> c.contains(uniquePair)).findAny().ifPresent(c -> {
            for(int i = 0; i < pFrequencyCount; i++)
                c.add(uniquePair);
        });

我的问题是为什么集合ifPresentOrElse没有更新列表中的集合?

或者我正在尝试做类似的事情吗?

An example image

0 个答案:

没有答案