I have two lists of Strings, List categories, and List choices, and my objective is to concatenate contents of these 2 lists as-
List<String> categories = Arrays.asList(new String[]{"Cat-1" , "Cat-2", "Cat-3"});
List<String> choices = Arrays.asList(new String[]{"Choice-1" , "Choice-2", "Choice-3"});
List<String> result = new ArrayList<>(categories.size() * choices.size());
for (String cat : categories) {
for (String choice: choices) {
result.add(cat + ':' + choice);
}
}
How can I achieve it using java Streams.
You could use a simple flat map here:
categories.stream()
.flatMap(left -> choices.stream().map(right -> left + ":" + right))
.collect(Collectors.toList())
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