Android

데이터 조작 : flatMap 활용하기

그란. 2021. 8. 9. 20:32
"allBlocks": [
    {
      "id": 58,
      "userId": 332,
      "startDate": "2021-07-10T00:00:00.000Z",
      "endDate": "2021-07-10T00:00:00.000Z",
      "type": "block",
      "createdAt": "2021-07-06T05:18:07.000Z"
    },
    {
      "id": 55,
      "userId": 332,
      "startDate": "2021-02-02T00:00:00.000Z",
      "endDate": "2021-02-02T00:00:00.000Z",
      "type": "block",
      "createdAt": "2021-02-02T00:14:17.000Z"
    },
    {
      "id": 53,
      "userId": 332,
      "startDate": "2020-12-01T00:00:00.000Z",
      "endDate": "2020-12-02T00:00:00.000Z",
      "type": "block",
      "createdAt": "2020-09-21T02:46:50.000Z"
    },
    {
      "id": 52,
      "userId": 332,
      "startDate": "2020-12-30T00:00:00.000Z",
      "endDate": "2020-12-31T00:00:00.000Z",
      "type": "block",
      "createdAt": "2020-09-21T02:21:11.000Z"
    }
  ]
 rentalsWithBlocks = product.rentals.asSequence()
                    .flatMap { rental ->
                        (rental.startDate betweenDays rental.endDate).asSequence()
                    }
                    .distinct()
                    .toList()
                        merge product.blocks.asSequence()
                    .flatMap { block ->
                        (block.startDate betweenDays block.endDate).asSequence()
                    }
                    .distinct()
                    .toList()
                        merge product.allBlocks.asSequence()
                    .flatMap { allBlock ->
                        (allBlock.startDate betweenDays allBlock.endDate).asSequence()
                    }
                    .distinct()
                    .toList(),