[Java] Simple Map, Set and List operations
Understand the problem\n1. We need to calculate orders placed on a table in a Restaurant\n2. Orders need to be sorted on table number \n\t [Intuition: Use TreeM
**Understand the problem**\n1. We need to calculate orders placed on a table in a Restaurant\n2. Orders need to be sorted on table number \n\t* [Intuition: Use TreeMap but also there are only [1, 500] tables in the restaurant]\n\t* I preferred to not use TreeMap and keep sorting the map on each insertion.\n\t* Traversing 1-500 is O(n)\n3. Rest is formatting the data for generating the output.\n\n```\npublic List<List<String>> displayTable(List<List<String>> orders) {\n        // We need food items in sorted order\n        Set<String> foods = new TreeSet<>();\n        List<String> items = new ArrayList<>();\n        \n        // Creating a list of TableNo and a Map of <Item, Count>\n        Map<String, Map<String, Integer>> map = new HashMap<>();\n        \n        for(List order: orders) {\n            String tNo = (String)order.get((Integer)1);\n            String item = (String)order.get((Integer)2);\n            map.putIfAbsent(tNo, new HashMap<String, Integer>());\n            Map<String, Integer> tOrder = map.get(tNo);\n            tOrder.put(item, tOrder.getOrDefault(item, 0) + 1);\n            foods.add(item);\n        }\n        List<List<String>> result = new ArrayList<>();\n        items.add(0, "Table");\n        for (String food:foods) {\n            items.add(food);\n        }\n        // Added headers in the result\n        result.add(items);\n        List<String> dummy =  Collections.nCopies(items.size() , "0");\n        for (int i = 1; i <= 500; i++) {\n            List<String> temp = new ArrayList<>(dummy);\n            String tNo = String.valueOf(i);\n            if (map.containsKey(tNo)) {\n                Map<String, Integer> tOrder = map.get(tNo);\n                temp.set(0, tNo);\n                \n                for (String key: tOrder.keySet()) {\n                    int q = tOrder.get(key);\n                    int iItem = items.indexOf(key);\n                    temp.set(iItem, String.valueOf(q));\n                }\n                result.add(temp);\n            }\n        }\n        return result;\n    }\n```
[]