This is my first article on getting started

I. Topic Description:

Give you the orders an array, said clients completed orders in the restaurant, specifically, the orders [I] = [customerNamei tableNumberi, foodItemi], including customerNamei is the customer’s name, TableNumberi is the number of the table at which the customer is sitting, and foodItemi is the name of the order.

Please return to the menu of the restaurant. In this Table, the first action in the Table is the title, with the first column being the Table number “Table”, and each column following it is the alphabetical name of the meal. The items in the next row represent the number of items ordered for each table, with the table number in the first column followed by the number of items ordered.

Note: Customer names are not part of the order table. In addition, the data rows in the table should be arranged in ascending order by table number.

Example:

Input: the orders = [[” Laura “, “2”, “Bean Burrito”], [” Jhon “, “2”, “Beef Burrito”], [” Melissa “, “2”, “Soda”]] output: [[“Table”,”Bean Burrito”,”Beef Burrito”,”Soda”],[“2″,”1″,”1″,”1”]]

Ii. Thinking analysis:

Implementation approach

To analyze the topic, it is necessary to carry out statistics on the order information

  1. Customer name is useless data, ignore it
  2. Group data by table, using HashMap, table as key, and food name and quantity as value
  3. In the first row of the result, you need to sort the food in lexicographical order, using HashSet to record the type of food
  4. Data rows in a table should be arranged in ascending order by table number

Problems encountered

The collections.sort is used to sort tables in lexicographical order, which does not meet the requirements of the question (table number in ascending order). Instead, the Integer collection stores tables

Three, AC code

public List<List<String>> displayTable(List<List<String>> orders) {
    if (orders == null || orders.size() == 0) {
        return new ArrayList<>();
    }
    // <table : <food : count>>
    Map<Integer, Map<String, Integer>> map = new HashMap<>();

    Set<String> foodSet = new HashSet<>();
    List<List<String>> res = new ArrayList<>();

    for (List<String> list : orders) {
        int table = Integer.parseInt(list.get(1));
        String food = list.get(2);
        foodSet.add(food);
        Map<String, Integer> foodMap = map.getOrDefault(table, new HashMap<>());
        int count = foodMap.getOrDefault(food, 0);
        foodMap.put(food, ++count);
        map.put(table, foodMap);
    }
    
    / / sorting
    List<Integer> tables = new ArrayList<>(map.keySet());
    Collections.sort(tables);
    List<String> foodList = new ArrayList<>(foodSet);
    Collections.sort(foodList);

    // The first row of data
    List<String> temp = new ArrayList<>();
    temp.add("Table");
    temp.addAll(foodList);
    res.add(new ArrayList<>(temp));
    
    // The number of foods for each table
    for (Integer table : tables) {
        temp.clear();
        temp.add(String.valueOf(table));

        Map<String, Integer> foodMap = map.getOrDefault(table, new HashMap<>());
        for (String food : foodList) {
            int count = foodMap.getOrDefault(food, 0);
            temp.add(String.valueOf(count));
        }
        res.add(new ArrayList<>(temp));
    }

    return res;
}
Copy the code