管理java对象列表中的一个对象
我有一个对象列表,对象列表多次显示同一个对象,但所有重复对象的颜色字段都不同。我想改变反应 如果有人对此有解决方案,请创建通用方法并传递此列表,然后根据我的预期响应转换为 1.实际反应管理java对象列表中的一个对象,java,arrays,json,spring-boot,Java,Arrays,Json,Spring Boot,我有一个对象列表,对象列表多次显示同一个对象,但所有重复对象的颜色字段都不同。我想改变反应 如果有人对此有解决方案,请创建通用方法并传递此列表,然后根据我的预期响应转换为 1.实际反应 [ { "id": 21, "startDate": "2020-07-21", "startTime": "10:28:34+05:30", "e
[
{
"id": 21,
"startDate": "2020-07-21",
"startTime": "10:28:34+05:30",
"endDate": "2020-07-21",
"endTime": "19:28:34+05:30",
"eventName": "Stretegic Planing for Business Success",
"location": "America Phoenix",
"city": "San Jose de Gracia",
"eventImg": "Image Url",
"passport": false,
"passportDiscountPercent": 5,
"featured": true,
"myFavourite": true,
"color": "Yellow"
},
{
"id": 21,
"startDate": "2020-07-21",
"startTime": "10:28:34+05:30",
"endDate": "2020-07-21",
"endTime": "19:28:34+05:30",
"eventName": "Stretegic Planing for Business Success",
"location": "America Phoenix",
"city": "San Jose de Gracia",
"eventImg": "Image Url",
"passport": false,
"passportDiscountPercent": 5,
"featured": true,
"myFavourite": true,
"color": "Green"
},
{
"id": 76,
"startDate": "2020-07-21",
"startTime": "10:28:34+05:30",
"endDate": "2020-07-21",
"endTime": "19:28:34+05:30",
"eventName": "Stretegic Planing for Business Success",
"location": "string",
"city": "Villa Juarez",
"eventImg": "Imgage url",
"passport": false,
"passportDiscountPercent": 5,
"featured": false,
"myFavourite": true,
"color": "Red"
},
{
"id": 76,
"startDate": "2020-07-21",
"startTime": "10:28:34+05:30",
"endDate": "2020-07-21",
"endTime": "19:28:34+05:30",
"eventName": "Stretegic Planing for Business Success",
"location": "string",
"city": "Villa Juarez",
"eventImg": "Imgage url",
"passport": false,
"passportDiscountPercent": 5,
"featured": false,
"myFavourite": true,
"color": "Black"
}
]
2.期待回应
[
{
"id": 21,
"startDate": "2020-07-21",
"startTime": "10:28:34+05:30",
"endDate": "2020-07-21",
"endTime": "19:28:34+05:30",
"eventName": "Stretegic Planing for Business Success",
"location": "America Phoenix",
"city": "San Jose de Gracia",
"eventImg": "Image Url",
"passport": false,
"passportDiscountPercent": 5,
"featured": true,
"myFavourite": true,
"eventType": [
{
"color": "Yellow"
},
{
"color": "Green"
}
]
},
{
"id": 76,
"startDate": "2020-07-21",
"startTime": "10:28:34+05:30",
"endDate": "2020-07-21",
"endTime": "19:28:34+05:30",
"eventName": "Stretegic Planing for Business Success",
"location": "string",
"city": "Villa Juarez",
"eventImg": "Imgage url",
"passport": false,
"passportDiscountPercent": 5,
"featured": false,
"myFavourite": true,
"eventType": [
{
"color": "Red"
},
{
"color": "Black"
}
]
}
]
步骤1:创建架构类 初始响应模型:
public class InitialModel {
String id;
String color;
}
public class Model {
String id;
List<Color> eventType = new ArrayList<>();
class Color {
String color;
public Color(String color) {
this.color = color;
}
}
}
最终/转换响应模型:
public class InitialModel {
String id;
String color;
}
public class Model {
String id;
List<Color> eventType = new ArrayList<>();
class Color {
String color;
public Color(String color) {
this.color = color;
}
}
}
1.我已添加了您的代码,但出现了一些错误
private List<EventCustomDTO> commonListView(List<EventDtoClass> eventDtoClassList) {
List<EventCustomDTO> eventCustomDTOList = new ArrayList<>();
List<EventDtoClass> eventDtoClasses = new Gson().fromJson(eventDtoClassList.toString(), new TypeToken<List<EventCustomDTO>>(){}.getType());
for (EventDtoClass eventDtoClass : eventDtoClasses) {
EventCustomDTO eventCustomDTO = null;
for(EventCustomDTO model : eventCustomDTOList){
if(eventDtoClass.getId().equals(model.getId())){
eventCustomDTO = model;
break;
}
}
if(eventCustomDTO == null) {
eventCustomDTO = new EventCustomDTO();
eventCustomDTO.getId() = eventDtoClass.getId();
eventCustomDTOList.add(eventCustomDTO);
}
eventCustomDTO.getEventType().add(new TypesOfEvents(eventDtoClass.getEvenTypeId(),eventDtoClass.getColor()));
}
return eventCustomDTOList;
}
private List commonListView(List eventDtoClassList){
List eventCustomDTOList=new ArrayList();
List eventDtoClasses=new Gson().fromJson(eventDtoClassList.toString(),new TypeToken(){}.getType());
for(EventDtoClass EventDtoClass:EventDtoClass){
EventCustomDTO EventCustomDTO=null;
for(EventCustomDTO模型:eventCustomDTOList){
if(eventDtoClass.getId().equals(model.getId())){
eventCustomDTO=模型;
打破
}
}
if(eventCustomDTO==null){
eventCustomDTO=新的eventCustomDTO();
eventCustomDTO.getId()=eventDtoClass.getId();
添加(eventCustomDTO);
}
eventCustomDTO.getEventType().add(新类型的事件(eventDtoClass.GetEventTypeId(),eventDtoClass.getColor());
}
returneventcustomdtolist;
}
由于预期结果中有不同的JSON,因此需要有两个类进行序列化/反序列化。让我们考虑下面的类:
@Getter
@AllArgsConstructor
private class ObjectA {
public Long id;
public String color;
}
@ToString
@AllArgsConstructor
private class ObjectB {
public Long id;
public String eventType;
}
在这种情况下,获得预期结果的代码如下:
List<ObjectA> object = List.of(new ObjectA(1L, "green"), new ObjectA(1L, "red"), new ObjectA(2L, "yellow"));
object.stream().collect(Collectors.groupingBy(ObjectA::getId))
.entrySet() // from map of grouped ObjectA's
.stream()
.map(entry -> new ObjectB(entry.getKey(), // GroupID
entry.getValue() // list of grouped ObjectA's by current GroupID
.stream()
.map(ObjectA::getColor) // change to stream of all color strings from grouped ObjectA's by current GroupID
.collect(Collectors.joining(", ")))
).forEach(System.out::println);
//.collect(Collectors.toList());
您可以根据您的用例简单地调整代码。如果已经找到模型,这不是要添加两次吗?我认为你应该添加modelList.add(tempModel);持续对于空检查?jsonString上放了什么?jsonString是java字符串格式的实际响应JSON。此行中的错误是:eventCustomDTO.getId()=eventDtoClass.getId();变量expectedI需要传递列表而不是jsonString
ObjectB(id=1, eventType=green, red)
ObjectB(id=2, eventType=yellow)