0

I have an array of objects in java that I return from a SQL server and I want to convert them and post them to a react Application. The goal is to fill a 2d table into react. I do not want to make the conversion into client. I want the data to be served ready.

Here is the array of objects I want to convert :

[
  {
    "tentId": 34,
    "timeslot": "2020-03-01T01:00:00"
  },
  {
    "tentId": 34,
    "timeslot": "2020-03-02T01:00:00"
  },
  {
    "tentId": 34,
    "timeslot": "2020-03-03T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-01T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-02T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-03T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-04T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-05T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-06T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-07T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-08T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-09T01:00:00"
  },
  {
    "tentId": 32,
    "timeslot": "2020-05-10T01:00:00"
  },
  {
    "tentId": 35,
    "timeslot": "2020-05-29T01:00:00"
  },
  {
    "tentId": 35,
    "timeslot": "2020-05-30T01:00:00"
  },
  {
    "tentId": 35,
    "timeslot": "2020-05-31T01:00:00"
  }
 ]

This is the results I want.

 [
  {
    "tentId": 34,[
    {"timeslot": "2020-03-01T01:00:00"},
    {"timeslot": "2020-03-02T01:00:00"},
    {"timeslot": "2020-03-03T01:00:00"}
    ]},
  {
    "tentId": 32,[
  { "timeslot": "2020-05-01T01:00:00"},
  { "timeslot": "2020-05-02T01:00:00"},
  {"timeslot": "2020-05-03T01:00:00"},
  { "timeslot": "2020-05-04T01:00:00"},
  {"timeslot": "2020-05-05T01:00:00"},
  {"timeslot": "2020-05-06T01:00:00"},
  {"timeslot": "2020-05-07T01:00:00"},
  {"timeslot": "2020-05-08T01:00:00"},
  {"timeslot": "2020-05-09T01:00:00"},
  {"timeslot": "2020-05-10T01:00:00"},
  {"timeslot": "2020-05-29T01:00:00"}
  ]},
  {
    "tentId": 35,[
  { "timeslot": "2020-05-30T01:00:00"},
  { "timeslot": "2020-05-31T01:00:00"}
  ]}
 ]

these are my dto objects

public class FinalTentBookingListDTO {
    Long tentId;

    List<LocalDateTime> timeslots;

    public Long getTentId() {
        return tentId;
    }

    public void setTentId(Long tentId) {
        this.tentId = tentId;
    }

    public List<LocalDateTime> getTimeslots() {
        return timeslots;
    }

    public void setTimeslots(List<LocalDateTime> timeslots) {
        this.timeslots = timeslots;
    }


    @Override
    public String toString() {
        return "FinalTentBookingListDTO{" +
                "tentId=" + tentId +
                ", timeslots=" + timeslots +
                '}';
    }
}


public class TentWithAllBookedTimeslotsDTO {
    Long tentId;
    LocalDateTime timeslot;

    public Long getTentId() {
        return tentId;
    }

    public void setTentId(Long tentId) {
        this.tentId = tentId;
    }

    public LocalDateTime getTimeslot() {
        return timeslot;
    }

    public void setTimeslot(LocalDateTime timeslot) {
        this.timeslot = timeslot;
    }

    @Override
    public String toString() {
        return "TentWithAllBookedTimeslotsDTO{" +
                "tentID=" + tentId +
                ", timeslot=" + timeslot +
                '}';
    }
}

I have tried with many methods including flatmap but I had no success. Thank you very much.

2
  • so you want to convert a List<TentWithAllBookedTimeslotsDTO> into a JSON String with a given format? Commented Oct 27, 2020 at 16:39
  • 1
    So you want to "group" by tentId? Did you research hpow to group data using Java Streams? Commented Oct 27, 2020 at 16:39

3 Answers 3

1

You can do it using Java Stream API. Example solution to your problem:

        final Map<Long, List<TentWithAllBookedTimeslotsDTO>> map = tents.stream().collect(Collectors.groupingBy(TentWithAllBookedTimeslotsDTO::getTentId));
        final List<FinalTentBookingListDTO> dtos = map.keySet().stream()
                .map((final Long tentId) -> {
                    final List<LocalDateTime> dateTimes = map.get(tentId).stream().map(TentWithAllBookedTimeslotsDTO::getTimeslot).collect(Collectors.toList());
                    return new FinalTentBookingListDTO(tentId, dateTimes);
                }).collect(Collectors.toList());

In your code there are no contructors, but I assume that you are able to code them on your own.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much this worked. Had to implement constructors of course!
0

My answer is you can directly return the data from database if the DB is returning the JSON content as a response. Else only you need to convert the db format to DTO and using jackson or gson you can convert it to json format.

Just set the response content type as "application/json" and send the db conent if it is Json else do conversion and then use libs to convert dtos to json.

Comments

0

You can do this conversion in Java 8 using one line assuming you add required constructors to your DTOs

List<FinalTentBookingListDTO> finalList = tentWithAllBookedTimeslotsDTOOList.stream()
                                                        .collect(groupingBy(TentWithAllBookedTimeslotsDTO::getTentId,
                                                                mapping(TentWithAllBookedTimeslotsDTO::getTimeslot, Collectors.toList())))
                                                        .entrySet()
                                                        .stream()
                                                        .map(entry -> new FinalTentBookingListDTO(entry.getKey(), entry.getValue()))
                                                        .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.