Drill

ProblemsJava › orders

Split an order across warehouses

mediumordersHash mapsArraysJava

Fulfilment needs the order regrouped by the warehouse each line ships from, so it can raise one pick list per site.

splitByWarehouse(rows: list<Row>) → map<string, list<string>>

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

Map<String, List<String>> splitByWarehouse(List<Row> rows) {
    
}

Worked examples

CallResult
splitByWarehouse(Main.<Row>ls(new Row("A1", "IST"), new Row("B2", "ANK"), new Row("C3", "IST")))Main.<String, List<String>>mp("IST", Main.<String>ls("A1", "C3"), "ANK", Main.<String>ls("B2"))
splitByWarehouse(Main.<Row>ls(new Row("A1", "IST")))Main.<String, List<String>>mp("IST", Main.<String>ls("A1"))
splitByWarehouse(Main.<Row>ls())Main.<String, List<String>>mp()
splitByWarehouse(Main.<Row>ls(new Row("X", "IZM"), new Row("X", "IZM")))Main.<String, List<String>>mp("IZM", Main.<String>ls("X", "X"))

Hint

A map from code to a growing list. Create the list the first time you meet a code.

Reference solution in Java
Map<String, List<String>> splitByWarehouse(List<Row> rows) {
    Map<String, List<String>> result = new LinkedHashMap<>();
    for (Row r : rows) {
        result.computeIfAbsent(r.warehouse, k -> new ArrayList<>()).add(r.sku);
    }
    return result;
}

The same problem in another language

More orders problems in Java