基于Java streams的ID将数据合并在一起

基于Java streams的ID将数据合并在一起,java,java-8,java-stream,Java,Java 8,Java Stream,我目前在java应用程序中从API中提取了一组数据。返回的数据如下所示: { "id": 1, "receiptId": "123456", "selections": [ { "name": "Apple", "price": "£1" } ] }, { "id": 2, "receiptId": "678910", "selections": [ { "name": "Pear", "pric

我目前在java应用程序中从API中提取了一组数据。返回的数据如下所示:

{
  "id": 1,
  "receiptId": "123456",
  "selections": [
    {
      "name": "Apple",
      "price": "£1"
    }
  ]
},
{
  "id": 2,
  "receiptId": "678910",
  "selections": [
    {
      "name": "Pear",
      "price": "£0.5"
    }
  ]
},
{
  "id": 3,
  "receiptId": "123456",
  "selections": [
    {
      "name": "Banana",
      "price:": "£2.00"
    }
  ]
}
如您所见,两个
receiptId
是相同的,我想将任何重复的
receiptId的
数据合并成一个块。即:

{
  "id": 1,
  "receiptId": "123456",
  "selections": [
    {
      "name": "Apple",
      "price": "£1"
    },
    {
      "name": "Banana",
      "price": "£2.00"
    }
  ]
},
{
  "id": 2,
  "receiptId": "678910",
  "selections": [
    {
      "name": "Pear",
      "price": "£0.5"
    }
  ]
},
目前,我正在通过执行以下操作将数据流式传输到地图中:

List<String> data = data.getData()
                         .stream()
                         .map(this::dataToReadable)
                         .collect(Collectors.toList());
  private String dataToReadable(List data) {
    return new DataBuilder().fromData(data)
                           .buildData();
  }
public DataBuilder fromData(List data) {
    this.withId(data.getId())
    this.withSelections(data.getSelections())
    this.withReceiptId(data.getReceiptId())
    return this;
  }
fromData
执行以下操作:

List<String> data = data.getData()
                         .stream()
                         .map(this::dataToReadable)
                         .collect(Collectors.toList());
  private String dataToReadable(List data) {
    return new DataBuilder().fromData(data)
                           .buildData();
  }
public DataBuilder fromData(List data) {
    this.withId(data.getId())
    this.withSelections(data.getSelections())
    this.withReceiptId(data.getReceiptId())
    return this;
  }
看看这是否有效

 data.getData()
        .stream()
        .collect(Collectors.groupingBy(Data::getRecieptId))
        .entrySet()
        .stream()
        .map(item -> dataToReadable(item.getValue()))
        .collect(Collectors.toList());

不幸的是,我不能这样做,我需要重新流式输出数据,以便像这样收集
。即使在
收集器中
我也无法访问
的任何函数。dataToReadable函数做什么?它返回什么?我已经在上面添加了更多的细节。我已经在做
dataToReadable
如果可能的话,我想重新传输
数据
列表。对不起,我没有理解你的问题