From an object and through Java 8 stream/collector functionality, I want to create a String made of 2 different separators. "/" to separate the options of the same category, and to separate the different categories.
Expected output result :
AJOUT option1 test / AJOUT option3 test </br>
SUPPRESSION option2 test </br>
MODIFICATION option4 test / MODIFICATION option5 test
Notes :
The uppercase words (AJOUT, SUPPRESSION, MODIFICATION) are the categories and are retrieved with method
option.getAction().getLabel()The rest of the words stands for the option (ex: "option1 test" is an option name) and are retrieved through method
option.getLabel()
Here is my try:
1- Separate all options with "/"
String description = orderCapture.getSelectedOptions().stream()
.map(option -> String.format("%s %s", option.getAction().getLabel(), option.getLabel()))
.collect(Collectors.joining(" / "));
Result
AJOUT option1 test / SUPPRESSION option2 test / AJOUT option3 test / MODIFICATION option4 test / MODIFICATION option5 test
2- Separate all options with "<br/>" (</br>)
String description = orderCapture.getSelectedOptions().stream()
.map(option -> String.format("%s %s", option.getAction().getLabel(), option.getLabel()))
.collect(Collectors.joining(" <br/> "));
Result
AJOUT option1 test </br> SUPPRESSION option2 test </br> AJOUT option3 test </br> MODIFICATION option4 test </br> MODIFICATION option5 test
3- My last try is to separate categories with the br tag but it is not working
String description = orderCapture.getSelectedOptions().stream()
.map(option -> String.format("%s %s", option.getAction().getLabel(), option.getLabel()))
.collect(Collectors.groupingBy(option -> option.getAction().getLabel()),
Collectors.joining(" <br/> "));
Result
Eclipse error:
Type mismatch: cannot convert from Collector>> to Supplier
The method collect(Supplier, BiConsumer, BiConsumer) in the type Stream is not applicable for the arguments (Collector>>, Collector)
Any help would be appreciated, thanks.