[Java] TreeMap and TreeSet
\tclass Solution {\n\t\tpublic List> displayTable(List> orders) {\n\t\t\tMap> tables = new TreeMap<>();\n\t\t\tSet allFoods = new TreeSet<>();\n\t\t\tfor(List o
\tclass Solution {\n\t\tpublic List<List<String>> displayTable(List<List<String>> orders) {\n\t\t\tMap<Integer, Map<String, Integer>> tables = new TreeMap<>();\n\t\t\tSet<String> allFoods = new TreeSet<>();\n\t\t\tfor(List<String> order: orders) {\n\t\t\t\tInteger table = Integer.parseInt(order.get(1));\n\t\t\t\tString food = order.get(2);\n\t\t\t\tMap<String, Integer> foods = tables.computeIfAbsent(table, key -> new HashMap<>());\n\t\t\t\tfoods.put(food, foods.getOrDefault(food, 0) + 1);\n\t\t\t\tallFoods.add(food);\n\t\t\t}\n\n\t\t\tList<List<String>> displayTable = new ArrayList<>();\n\t\t\tList<String> header = new ArrayList<>();\n\t\t\theader.add("Table");\n\t\t\theader.addAll(allFoods);\n\t\t\tdisplayTable.add(header);\n\t\t\tfor(Map.Entry<Integer, Map<String, Integer>> table: tables.entrySet()) {\n\t\t\t\tList<String> row = new ArrayList<>();\n\t\t\t\trow.add(table.getKey().toString());\n\t\t\t\tfor(int i = 1; i < header.size(); ++i) {\n\t\t\t\t\trow.add(table.getValue().getOrDefault(header.get(i), 0).toString());\n\t\t\t\t}            \n\t\t\t\tdisplayTable.add(row);\n\t\t\t}\n\t\t\treturn displayTable;\n\t\t}\n\t}
[]