Loading...
Loading...
Loading...
A catalog lists products with their category. Group the products by category. Print the categories in alphabetical order, and the products of each category in the order they were listed.
<category> <product> (no spaces inside a word).<category>: <product> <product> ..., sorted by category.Example
Input
6 stationery pen electronics mouse stationery notebook electronics cable furniture chair stationery marker
Output
electronics: mouse cable furniture: chair stationery: pen notebook marker
Categories are in A-Z order; the products keep the order of the input.
Try the problem first. Open one hint at a time.
The value of the map is a list: Map<String, List<String>>.
computeIfAbsent(category, k -> new ArrayList<>()).add(product) creates the list when needed.
String.join(" ", list) builds the line of products.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor