I have a Collection of String values. If one of the values is an '*', I'd like to replace that value with 3 others, let's say "X", "Y", and "Z".
In other words, I'd like [ "A", "B", "*", "C"] to turn into ["A","B","X","Y","Z","C"]. Order does not matter, so it is simply get rid of one and add the others. These are the ways I can think of for doing it, using that example:
Collection<String> additionalValues = Arrays.asList("X","Y","Z"); // or set or whatever
if (attributes.contains("*")) {
attributes.remove("*");
attributes.addAll(additionalValues);
}
or
attributes.stream()
.flatMap(val -> "*".equals(val) ? additionalValues.stream() : Stream.of(val))
.collect(Collectors.toList());
What's the most efficient way of doing this? Again, order doesn't matter, and ideally I'd like to remove duplicates (so maybe distinct() on stream, or HashSet?).