I have a string like this "-FOREST&-JUNGLE&DESERT"
This string is split up into words in a list like so: [-FOREST, -JUNGLE ,DESERT]
List<String> pattern = Arrays.asList(s.split("\\s*&\\s*"));
Every word with a minus at the beginning must be in one new list and every word without in another like so:
List<String> allowedtags = pattern
.stream()
.filter(name -> !name.startsWith("-"))
.collect(Collectors.toList());
List<String> unwantedtags = pattern
.stream()
.filter(name -> name.startsWith("-"))
.collect(Collectors.toList());
My problem is that my unwantedtags
keeps words with a minus at the beginning, but for all my operations I need to get rid of it. If I do the example below, it will not work if "FOREST" is in taglist
as my unwantedtags
contains "-FOREST".
if (taglist.stream().anyMatch(unwantedtags::contains)) {
return !IsWhitelist;
}
So how can I fix my unwantedtags list? Maybe in java 8 stream api? Moreover I have the feeling that on the one hand it is working fine to that point, but on the other hand I get the feeling that this is a lot of code for getting just two lists. Can it be optimized?
You can map each element to strip out the leading minus sign after it has been detected:
List<String> unwantedtags = pattern
.stream()
.filter(name -> name.startsWith("-"))
.map(name -> name.substring(1))
.collect(Collectors.toList());
The code can be made more efficient by constructing and populating the lists explicitly. Then you can process pattern
in a single pass instead of two:
List<String> allowedtags = new ArrayList();
List<String> unwantedtags = new ArrayList();
pattern.stream()
.forEach(name -> {
if (name.startsWith("-")) unwantedtags.add(name.substring(1));
else allowedtags.add(name);
}
);