Skip to content
Advertisement

Return maximum element

I wrote this program which can count the number of pizzas ordered.

I can’t find how to return the maximum element of my list (the pizza with maximum orders).

I tried this but it doesn’t work

return order.stream().max(Comparator.comparing(norder::pizza)).get();

public class PizzaCount {
 
    private ArrayList<String> order;
    private String[] menu;
 
    public PizzaCount(String[] pizzas) {
        this.menu = pizzas;
        this.order = new ArrayList<>();
    }
 
    public int norder(String pizza) {
        int number=0;
        for(String pizzaOrdered : this.order){
            if(pizza(pizzaOrdered)){
                number++;
            }
        }
        return number;
    }
     
    public String pizzamax() {
        /*stuck here*/
    }
 
    public static void main(String[] args) {
            String [] menu = {"romana", "funghi"};
            PizzaCount n = new PizzaCount(menu);
            n.add("romana");
            n.add("romana");
            n.add("funghi");
            System.out.println("number of romana: "+ n.norder("romana"));
            System.out.println("number of funghi: "+ n.norder("funghi"));
            System.out.println("the pizza with the maximum order is: "+ n.pizzamax());
        }
}

Advertisement

Answer

If I understand you correctly, you can try this

order.stream().max(Comparator.comparing(this::norder)).get();

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement