I have the below code in which am computing Min and Max ordered items from list of Orders and works as expected. Am wondering if this can be refactored/improved any further to make it more optimal and performant to deal with thousands or orders list in prod.
I intentionally did not do Collections.min(itemFrequencyMap.values()) and Collections.max(itemFrequencyMap.values()) since it would require iteration of all values twice and then looping over itemFrequencyMap again to find entry with Min and Max value.
@Data
public class Order {
    private Long id;
    private String created_at, current_total_price, currency;
    Double total_price;     
    private List<Item> items;
}
@Data
public class Item {
private Long product_id;
String title, name, price;
}
@Data
public class ItemFrequency {
private Item item;
Long frequency;
}
public void minMaxItems(List<Order> orders) {       
    ItemFrequency minOrder = null;
    ItemFrequency maxOrder = null;
    Map<Item, Long> itemFrequencyMap = new TreeMap<>();
    orders.stream()
            .map(o -> o.getLine_items())
            .flatMap(List::stream)
            .forEach(Item -> {
                itemFrequencyMap.compute(Item, (k, v) -> v == null ? 1 : v + 1);
            });
    boolean isFirstEntry = true;
    Long max = Long.MIN_VALUE, min = Long.MAX_VALUE;
    for (Map.Entry<Item, Long> itemFrequency : itemFrequencyMap.entrySet()) {
        Item Item = itemFrequency.getKey();
        if (isFirstEntry) {
            max = itemFrequency.getValue();
            min = itemFrequency.getValue();
            isFirstEntry = false;
            continue;
        }
        if (itemFrequency.getValue() > max) {                
            max = itemFrequency.getValue();
            maxOrder = new ItemFrequency(Item,max);
        } else if (itemFrequency.getValue() < min) {                
            min = itemFrequency.getValue();
            minOrder = new ItemFrequency(Item,min);
        }
    }
    
}
 
    