I have a scenario like below:
I have done all this individually and is perfectly working. But I would like to do this with CompletableFuture as Step 1 and Step 2 are independent and can run parallelly. Step 3 can be chained or attached to the result of Step 1 and Step 2.
I just don't know how this can be done. I request for help in here.
Note: I have commented the code that I tried for CompletableFuture. Please help me complete it. Thanks in advance.
`import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ProjectWork {
public static List<Integer> getKey(Path path) throws IOException {
return Files
.readAllLines(path)
.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
}
public static Map<Integer, String> getMap(Path path) throws IOException {
var userInput = new HashMap<Integer, String>();
var allLines = Files.readAllLines(path);
for(String line : allLines) {
var values = line.split(",");
userInput.put(Integer.valueOf(values[0]), values[1]);
}
return userInput;
}
public static Path getResult(Map<Integer, String> map, List<Integer> list, Path path) throws IOException {
map
.keySet()
.removeAll(list);
var text = map
.entrySet()
.stream()
.map(e -> e.getKey()+","+e.getValue())
.collect(Collectors.joining("\n"));
var filePath = Files.write(path, text.getBytes());
return filePath;
}
public static void main(String[] args) throws IOException {
Path mapPath = Path.of("C:\\Users\\ra\\Documents\\myMapFile.txt");
Path keyPath = Path.of("C:\\Users\\ra\\Documents\\myKeyFile.txt");
Path outputPath = Path.of("C:\\Users\\ra\\Documents\\myResultFile.txt");
var map = getMap(mapPath);
var list = getKey(keyPath);
var path = getResult(map, list, outputPath);
System.out.println(map);
System.out.println(list);
System.out.println(path);
/*
CompletableFuture<Map<Integer, String>> cfGetMap = CompletableFuture
.supplyAsync(() -> {
Map<Integer, String> input = null;
try {
input = getMap(mapPath);
} catch (IOException ex) {
System.out.println("Exception: "+ex);
}
return input;
})
.exceptionally(ex -> {
System.out.println("Exception: "+ex);
return Map.of(0, "");
});
CompletableFuture<List<Integer>> cfGetList = CompletableFuture
.supplyAsync(() -> {
List<Integer> input = null;
try {
input = getKey(keyPath);
} catch (IOException ex) {
System.out.println("Exception: "+ex);
}
return input;
})
.exceptionally(ex -> {
System.out.println("Exception: "+ex);
return List.of(0);
});
CompletableFuture.allOf(cfGetMap, cfGetList).join();
*/
}
}`
I am expecting it to be done by CompletableFuture.
Use thenCombine
instead of allOf
:
CompletableFuture<Path> cfGetResult = cfGetMap.thenCombine(cfGetList, (map, list) -> getResult(map, list, path))
Note that I omitted the necessary try-catch and error handling for brevity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.