【问题标题】:Sorting and group two nested lists in Java based on a field in the inner list基于内部列表中的字段对Java中的两个嵌套列表进行排序和分组
【发布时间】: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&lt;Inventory&gt;,我如何根据内部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


    【解决方案1】:

    首先,Inventory 不应该扩展 ArrayList。它应该一个List&lt;InventoryItem&gt;

    按制造商排序应该像

    一样简单
    list.sort(Comparator.comparing(Inventory::getManufacturer));
    

    你只需要在 Inventory 中有一个 getManufacturer() 方法即可。由于 Inventory 的所有项目都具有相同的制造商,因此您需要从此方法返回第一个项目的制造商。

    您当然应该确定库存中没有任何物品是否合法,并决定如何在您的订单中放置此类库存。

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      相关资源
      最近更新 更多