Java 8 here. I have the following classes:
@Getter
@Setter
public class InventoryItem {
// For all instances inside the same manufacturer, this value will be the same
private String manufacturer;
private String model;
private BigDecimal price;
}
public class Inventory extends ArrayList<InventoryItem> {
}
public class InventoryProcessor {
public void processInventoriesForAllManufacturers() {
// Essentially get a List<List<InventoryItem>>, where in the
// inner list is a list of all InventoryItems for a particular
// manufacturer, and the outer list is a list of all manufacturer's
// inventories
List<Inventory> inventoriesForAllManufacturers =
inventoryService.getAllInventories();
// Now I need to sort this list based on manufacturer name, but how???
}
}
So the problem here is, that inventoryService.getAllInventories(), might return the equivalent of:
public List<Inventory> getAllInventories() {
List<Inventory> allInventories = new ArrayList<>();
List<InventoryItem> manufacturer1Inv = new ArrayList<InventoryItem>();
InventoryItem item1 = new InventoryItem(
"Lorem Ipsum Inc", "FK10", BigDecimal.valueOf(1.99));
InventoryItem item2 = new InventoryItem(
"Lorem Ipsum Inc", "FK11", BigDecimal.valueOf(9.99));
manufacturer1Inv.add(item1);
manufacturer1Inv.add(item2);
List<InventoryItem> manufacturer2Inv = new ArrayList<InventoryItem>();
InventoryItem item3 = new InventoryItem(
"Ice Station Zebra Associates", "00djd94", BigDecimal.valueOf(12.99));
InventoryItem item4 = new InventoryItem(
"Ice Station Zebra Associates", "00djd95", BigDecimal.valueOf(22.99));
List<InventoryItem> manufacturer2Inv = new ArrayList<InventoryItem>();
manufacturer2Inv.add(item3);
manufacturer2Inv.add(item4);
List<InventoryItem> manufacturer3Inv = new ArrayList<InventoryItem>();
InventoryItem item5 = new InventoryItem(
"ACME Corp", "1234A", BigDecimal.valueOf(2.99));
InventoryItem item6 = new InventoryItem(
"ACME Corp", "1234B", BigDecimal.valueOf(6.99));
List<InventoryItem> manufacturer3Inv = new ArrayList<InventoryItem>();
manufacturer3Inv.add(item5);
manufacturer3Inv.add(item6);
Inventory m1Inv = new Inventory(manufacturer1Inv);
Inventory m2Inv = new Inventory(manufacturer2Inv);
Inventory m3Inv = new Inventory(manufacturer3Inv);
allInventories.add(m1Inv);
allInventories.add(m2Inv);
allInventories.add(m3Inv);
return allInventories;
}
All InventoryItems belonging to the same manufacturer will have the same exact manufacturer value.
So, given this List<Inventory>, how could I sort it based on the unique manufacturer values of the inner InventoryItems?
Meaning, a resulting list that would contain elements in the following order (given the example above):
"ACME Corp", "1234A", BigDecimal.valueOf(2.99));
"ACME Corp", "1234B", BigDecimal.valueOf(6.99));
"Ice Station Zebra Associates", "00djd94", BigDecimal.valueOf(12.99));
"Ice Station Zebra Associates", "00djd95", BigDecimal.valueOf(22.99));
"Lorem Ipsum Inc", "FK10", BigDecimal.valueOf(1.99));
"Lorem Ipsum Inc", "FK11", BigDecimal.valueOf(9.99));
My best attempt thus far:
public void processInventoriesForAllManufacturers() {
// Essentially get a List<List<InventoryItem>>, where in the
// inner list is a list of all InventoryItems for a particular
// manufacturer, and the outer list is a list of all manufacturer's
// inventories
List<Inventory> inventoriesForAllManufacturers =
inventoryService.getAllInventories();
// Now I need to sort this list based on manufacturer name, but how???
List<Inventory> sorted = inventoriesForAllManufacturers.stream().map(inv ->
Collections::sort(in)).collect(Collectors.toList());
}
Produces the compilter error "Object is not a functional interface." Any ideas where I'm going awry?
Again, they only need to be grouped + sorted alphabetically by the manufacturer name; within each manufacturer's inventory, they should appear in order of insertion onto the list.