将对象数组转换为具有不同格式的数组

huangapple go评论72阅读模式
英文:

Convert an array of Objects to an array with different format

问题

import java.util.*;
import java.util.stream.Collectors;

public class YourClassName {

    public static void main(String[] args) {
        List<TentWithAllBookedTimeslotsDTO> inputList = new ArrayList<>();
        // Initialize inputList with your data
        
        Map<Long, List<LocalDateTime>> resultMap = inputList.stream()
            .collect(Collectors.groupingBy(
                TentWithAllBookedTimeslotsDTO::getTentId,
                Collectors.mapping(TentWithAllBookedTimeslotsDTO::getTimeslot, Collectors.toList())
            ));

        List<FinalTentBookingListDTO> finalList = resultMap.entrySet().stream()
            .map(entry -> {
                FinalTentBookingListDTO dto = new FinalTentBookingListDTO();
                dto.setTentId(entry.getKey());
                dto.setTimeslots(entry.getValue());
                return dto;
            })
            .collect(Collectors.toList());

        // Now 'finalList' contains the desired result
    }
}

请注意,这只是一个演示代码,假设你已经正确初始化了 inputList。你需要根据你的实际环境和数据进行适当的调整。这段代码使用了 Java 8 的流式操作,根据 tentId 对对象进行分组,并将对应的时间戳集合收集到一个新的 DTO 对象中。最终结果将保存在 finalList 中。

英文:

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 :

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

This is the results I want.

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

these are my dto objects

public class FinalTentBookingListDTO {
    Long tentId;

    List&lt;LocalDateTime&gt; timeslots;

    public Long getTentId() {
        return tentId;
    }

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

    public List&lt;LocalDateTime&gt; getTimeslots() {
        return timeslots;
    }

    public void setTimeslots(List&lt;LocalDateTime&gt; timeslots) {
        this.timeslots = timeslots;
    }


    @Override
    public String toString() {
        return &quot;FinalTentBookingListDTO{&quot; +
                &quot;tentId=&quot; + tentId +
                &quot;, timeslots=&quot; + timeslots +
                &#39;}&#39;;
    }
}


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 &quot;TentWithAllBookedTimeslotsDTO{&quot; +
                &quot;tentID=&quot; + tentId +
                &quot;, timeslot=&quot; + timeslot +
                &#39;}&#39;;
    }
}

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

答案1

得分: 1

你可以使用Java Stream API 来完成。以下是针对你问题的示例解决方案:

        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());

在你的代码中没有构造函数,但我假设你能够自己编写它们。

英文:

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

        final Map&lt;Long, List&lt;TentWithAllBookedTimeslotsDTO&gt;&gt; map = tents.stream().collect(Collectors.groupingBy(TentWithAllBookedTimeslotsDTO::getTentId));
        final List&lt;FinalTentBookingListDTO&gt; dtos = map.keySet().stream()
                .map((final Long tentId) -&gt; {
                    final List&lt;LocalDateTime&gt; 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.

答案2

得分: 0

我的回答是,如果数据库将JSON内容作为响应返回,您可以直接从数据库返回数据。否则,您只需要将数据库格式转换为DTO,然后使用Jackson或Gson将其转换为JSON格式。

只需将响应内容类型设置为"application/json",并且如果数据库内容为JSON,则发送数据库内容;否则进行转换,然后使用库将DTO转换为JSON。

英文:

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.

答案3

得分: 0

你可以在Java 8中使用一行代码进行此转换,假设您为DTO添加了所需的构造函数。

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());
英文:

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

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

huangapple
  • 本文由 发表于 2020年10月28日 00:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64558952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定