I have a String of text lines.
Some of the lines have a format of "key: value". Others should be ignored.
I have a fixed (pre-defined) list of keys that I needs to extract values for and put into a HashMap.
So, I'm doing something like this:
BufferedReader reader = new BufferedReader(new StringReader(memoText));
reader.lines().forEach(line->{
if(line.startsWith("prefix1")){
// Some code is required here to get the value1
}
else if(line.startsWith("prefix2")){
// Some code is required here to get the value2
}
...
}
Is there a better way of implementing the parsing in Java 8?
As per you current problem statement. You can try below code which..
You may want to change it as per your needs:
import static java.util.stream.Collectors.toMap;
//skipped
Pattern pattern = Pattern.compile("([a-zA-Z]+)\\s*:\\s*(.*)");
try (Stream<String> stream = Files.lines(Paths.get("<PATH_TO_FILE>"))) {
Map<String, String> results =
stream.map(pattern::matcher)
.filter(Matcher::find)
.collect(toMap(a -> a.group(1), a -> a.group(2)));
}
Let me know, if this is not what you are looking for
// define your fixed keys in a list
List<String> keys = Arrays.asList("key1", "key2");
reader.lines()
// use filter instead of if-else
.filter(line -> line.indexOf(":")>-1 && keys.contains(line.substring(0, line.indexOf(":"))))
// collect in to a map
.collect(Collectos.toMap(line -> {
return line.substring(0, line.indexOf(":"));
}, line -> {
return line.substring(line.indexOf(":") + 1);
}))
But you must make sure every line has the different key. Or it will throw java.lang.IllegalStateException: Duplicate key
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With