Quick actions

cmd+k|ctrl+k

Navigation

Languages

Map assessment

Snippet info

Language

Java

Visibility

public

Author

hospino11

Created

2026-01-21T02:24:48.949373Z

Updated

2026-01-21T02:24:48.949373Z

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Comparator;
import java.util.Arrays;
import java.util.stream.Collectors;

class Main {
    
    public static void main(String[] args) {
        List<Item> items = Arrays.asList(
            new Item("A", "X", 100.0),
            new Item("B", "X", 30.0),
            new Item("C", "X", 80.0),
            new Item("D", "Y", 60.0),
            new Item("E", "Y", 40.0)
        );
        
        // Expected: {X=[100.0, 80.0], Y=[60.0]}
        Map<String, List<Double>> itemExpensiveItemsByCategory = expensiveItemsByCategory(items);
        System.out.println(itemExpensiveItemsByCategory);
    }
    
    /**
     * Map<String, List<Double>> where key is category, value is list of prices over 50, sorted descending
     */
    public static Map<String, List<Double>> expensiveItemsByCategory(List<Item> items) {
        return items.stream()
                .collect(Collectors.groupingBy(Item::getCategory, TreeMap::new, Collectors.filtering(
                    item -> item.getPrice() > 50.0, 
                    Collectors.mapping(
                        item -> item.getPrice(),
                        Collectors.collectingAndThen(
                            Collectors.toList(),
                            list -> {
                                list.sort(Comparator.reverseOrder());
                                
                                return list;
                            }
                        )
                    )) )
                );
    }
}

class Item {
    private String name;
    private String category;
    private double price;
    
    public Item(String name, String category, double price) {
        this.name = name;
        this.category = category;
        this.price = price;
    }
    
    public String getName() { return name; }
    public String getCategory() { return category; }
    public double getPrice() { return price; }
}
INFO