[Java] Straightforward Solution using TreeMap!!!
For this problem, we can imagine that we are going to fill a chart/2D List as the final output. The key idea of my solution is: we can maintain 2 Lists: one cal
For this problem, we can imagine that we are going to fill a **chart/2D List** as the final output. The key idea of my solution is: we can maintain 2 Lists: one called *foodSorted*, the other called *tablesSorted*. These 2 Lists are sorted by the natual order.\n\nThen we can construct a TreeMap, the trick here is, I constructed an **orderKey** as the key of this TreeMap so that keep track of what food was ordered by which table. \n\nFinally, we can fill the chart by checking the key of this TreeMap and put the value of the key into this chart (For the first row with "Table" string, I just treated it separately).\n\nPlease let know if you have other better solutions!\n\n```\n    public List<List<String>> displayTable(List<List<String>> orders) {\n        List<List<String>> res = new ArrayList<>();\n        \n        Set<String> food = new HashSet<>(); //For collecting all the food without duplication\n        Set<Integer> tables = new HashSet<>(); //For collecting all the table without duplication\n        \n        TreeMap<String, Integer> map = new TreeMap<>();\n        \n        for (List<String> order : orders) {\n            food.add(order.get(2));\n            tables.add(Integer.parseInt(order.get(1)));\n            String orderKey = order.get(1) + order.get(2); // Use the Table number and Food as the TreeMap\'s key! \n            map.put(orderKey, map.getOrDefault(orderKey, 0) + 1);\n        }\n        List<String> foodSorted = new ArrayList<>();\n        List<Integer> tablesSorted = new ArrayList<>();\n        foodSorted.addAll(food);\n        tablesSorted.addAll(tables);\n        // Sorting the food and table number by natural order.\n        foodSorted.sort(Comparator.naturalOrder());\n        tablesSorted.sort(Comparator.naturalOrder());\n        // Precessing the first row of the output\n        List<String> res1 = new ArrayList<>();\n        res1.add("Table");\n        for (String s : foodSorted) {\n            res1.add(s);\n        }\n        res.add(res1);\n        // Precessing other rows of the output\n        for (int tableNum : tablesSorted) {\n            List<String> res2 = new ArrayList<>();\n            res2.add(String.valueOf(tableNum));\n            for (int i = 1; i < res1.size(); i++) {\n                String key = tableNum + res1.get(i);\n                res2.add(String.valueOf(map.getOrDefault(key, 0)));\n            }\n            res.add(res2);\n        }\n              \n        return res;\n    }\n```
[]