【发布时间】:2019-11-22 20:23:24
【问题描述】:
这里是 Java 8。我有以下课程:
@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???
}
}
所以这里的问题是,inventoryService.getAllInventories() 可能会返回相当于:
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;
}
属于同一制造商的所有InventoryItems 将具有完全相同的manufacturer 值。
那么,鉴于这个List<Inventory>,我如何根据内部InventoryItems 的唯一manufacturer 值对其进行排序?
含义,一个包含以下顺序的元素的结果列表(给定上面的示例):
"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));
迄今为止我最好的尝试:
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());
}
产生编译器错误“对象不是功能接口。”有什么想法我会出错吗?
同样,它们只需要按制造商名称的字母顺序进行分组+排序;在每个制造商的库存中,它们应按插入列表的顺序出现。
【问题讨论】:
标签: sorting collections java-8 java-stream