Manage one object inside list of object java.

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

Manage one object inside list of object java

问题

以下是已经翻译好的部分:

  1. Actual Response(实际响应):
  1. [
  2. {
  3. "id": 21,
  4. "startDate": "2020-07-21",
  5. "startTime": "10:28:34+05:30",
  6. "endDate": "2020-07-21",
  7. "endTime": "19:28:34+05:30",
  8. "eventName": "Stretegic Planing for Business Success",
  9. "location": "America Phoenix",
  10. "city": "San Jose de Gracia",
  11. "eventImg": "Image Url",
  12. "passport": false,
  13. "passportDiscountPercent": 5,
  14. "featured": true,
  15. "myFavourite": true,
  16. "color": "Yellow"
  17. },
  18. {
  19. "id": 21,
  20. "startDate": "2020-07-21",
  21. "startTime": "10:28:34+05:30",
  22. "endDate": "2020-07-21",
  23. "endTime": "19:28:34+05:30",
  24. "eventName": "Stretegic Planing for Business Success",
  25. "location": "America Phoenix",
  26. "city": "San Jose de Gracia",
  27. "eventImg": "Image Url",
  28. "passport": false,
  29. "passportDiscountPercent": 5,
  30. "featured": true,
  31. "myFavourite": true,
  32. "color": "Green"
  33. },
  34. {
  35. "id": 76,
  36. "startDate": "2020-07-21",
  37. "startTime": "10:28:34+05:30",
  38. "endDate": "2020-07-21",
  39. "endTime": "19:28:34+05:30",
  40. "eventName": "Stretegic Planing for Business Success",
  41. "location": "string",
  42. "city": "Villa Juarez",
  43. "eventImg": "Imgage url",
  44. "passport": false,
  45. "passportDiscountPercent": 5,
  46. "featured": false,
  47. "myFavourite": true,
  48. "color": "Red"
  49. },
  50. {
  51. "id": 76,
  52. "startDate": "2020-07-21",
  53. "startTime": "10:28:34+05:30",
  54. "endDate": "2020-07-21",
  55. "endTime": "19:28:34+05:30",
  56. "eventName": "Stretegic Planing for Business Success",
  57. "location": "string",
  58. "city": "Villa Juarez",
  59. "eventImg": "Imgage url",
  60. "passport": false,
  61. "passportDiscountPercent": 5,
  62. "featured": false,
  63. "myFavourite": true,
  64. "color": "Black"
  65. }
  66. ]
  1. Expected Response(预期响应):
  1. [
  2. {
  3. "id": 21,
  4. "startDate": "2020-07-21",
  5. "startTime": "10:28:34+05:30",
  6. "endDate": "2020-07-21",
  7. "endTime": "19:28:34+05:30",
  8. "eventName": "Stretegic Planing for Business Success",
  9. "location": "America Phoenix",
  10. "city": "San Jose de Gracia",
  11. "eventImg": "Image Url",
  12. "passport": false,
  13. "passportDiscountPercent": 5,
  14. "featured": true,
  15. "myFavourite": true,
  16. "eventType": [
  17. {
  18. "color": "Yellow"
  19. },
  20. {
  21. "color": "Green"
  22. }
  23. ]
  24. },
  25. {
  26. "id": 76,
  27. "startDate": "2020-07-21",
  28. "startTime": "10:28:34+05:30",
  29. "endDate": "2020-07-21",
  30. "endTime": "19:28:34+05:30",
  31. "eventName": "Stretegic Planing for Business Success",
  32. "location": "string",
  33. "city": "Villa Juarez",
  34. "eventImg": "Imgage url",
  35. "passport": false,
  36. "passportDiscountPercent": 5,
  37. "featured": false,
  38. "myFavourite": true,
  39. "eventType": [
  40. {
  41. "color": "Red"
  42. },
  43. {
  44. "color": "Black"
  45. }
  46. ]
  47. }
  48. ]
英文:

I have List of object that list of object is shown same object multiple time but only color field different for all duplicate object. I want to change response.

if anyone have solution for this create common method and pass this list and covert into as per my expectation response.

1.Actual Response

  1. [
  2. {
  3. "id": 21,
  4. "startDate": "2020-07-21",
  5. "startTime": "10:28:34+05:30",
  6. "endDate": "2020-07-21",
  7. "endTime": "19:28:34+05:30",
  8. "eventName": "Stretegic Planing for Business Success",
  9. "location": "America Phoenix",
  10. "city": "San Jose de Gracia",
  11. "eventImg": "Image Url",
  12. "passport": false,
  13. "passportDiscountPercent": 5,
  14. "featured": true,
  15. "myFavourite": true,
  16. "color": "Yellow"
  17. },
  18. {
  19. "id": 21,
  20. "startDate": "2020-07-21",
  21. "startTime": "10:28:34+05:30",
  22. "endDate": "2020-07-21",
  23. "endTime": "19:28:34+05:30",
  24. "eventName": "Stretegic Planing for Business Success",
  25. "location": "America Phoenix",
  26. "city": "San Jose de Gracia",
  27. "eventImg": "Image Url",
  28. "passport": false,
  29. "passportDiscountPercent": 5,
  30. "featured": true,
  31. "myFavourite": true,
  32. "color": "Green"
  33. },
  34. {
  35. "id": 76,
  36. "startDate": "2020-07-21",
  37. "startTime": "10:28:34+05:30",
  38. "endDate": "2020-07-21",
  39. "endTime": "19:28:34+05:30",
  40. "eventName": "Stretegic Planing for Business Success",
  41. "location": "string",
  42. "city": "Villa Juarez",
  43. "eventImg": "Imgage url",
  44. "passport": false,
  45. "passportDiscountPercent": 5,
  46. "featured": false,
  47. "myFavourite": true,
  48. "color": "Red"
  49. },
  50. {
  51. "id": 76,
  52. "startDate": "2020-07-21",
  53. "startTime": "10:28:34+05:30",
  54. "endDate": "2020-07-21",
  55. "endTime": "19:28:34+05:30",
  56. "eventName": "Stretegic Planing for Business Success",
  57. "location": "string",
  58. "city": "Villa Juarez",
  59. "eventImg": "Imgage url",
  60. "passport": false,
  61. "passportDiscountPercent": 5,
  62. "featured": false,
  63. "myFavourite": true,
  64. "color": "Black"
  65. }
  66. ]

2.Expect Response

  1. [
  2. {
  3. "id": 21,
  4. "startDate": "2020-07-21",
  5. "startTime": "10:28:34+05:30",
  6. "endDate": "2020-07-21",
  7. "endTime": "19:28:34+05:30",
  8. "eventName": "Stretegic Planing for Business Success",
  9. "location": "America Phoenix",
  10. "city": "San Jose de Gracia",
  11. "eventImg": "Image Url",
  12. "passport": false,
  13. "passportDiscountPercent": 5,
  14. "featured": true,
  15. "myFavourite": true,
  16. "eventType": [
  17. {
  18. "color": "Yellow"
  19. },
  20. {
  21. "color": "Green"
  22. }
  23. ]
  24. },
  25. {
  26. "id": 76,
  27. "startDate": "2020-07-21",
  28. "startTime": "10:28:34+05:30",
  29. "endDate": "2020-07-21",
  30. "endTime": "19:28:34+05:30",
  31. "eventName": "Stretegic Planing for Business Success",
  32. "location": "string",
  33. "city": "Villa Juarez",
  34. "eventImg": "Imgage url",
  35. "passport": false,
  36. "passportDiscountPercent": 5,
  37. "featured": false,
  38. "myFavourite": true,
  39. "eventType": [
  40. {
  41. "color": "Red"
  42. },
  43. {
  44. "color": "Black"
  45. }
  46. ]
  47. }
  48. ]

答案1

得分: 2

Here's the translated code without the comments:

  1. public class InitialModel {
  2. String id;
  3. String color;
  4. }
  5. public class Model {
  6. String id;
  7. List<Color> eventType = new ArrayList<>();
  8. class Color {
  9. String color;
  10. public Color(String color) {
  11. this.color = color;
  12. }
  13. }
  14. }
  15. List<Model> modelList = new ArrayList<>();
  16. List<InitialModel> initialModelList = new Gson().fromJson(jsonString, new TypeToken<List<Model>>(){}.getType());
  17. for (InitialModel initialModel : initialModelList) {
  18. Model tempModel = null;
  19. for(Model model : modelList){
  20. if(initialModel.id.equals(model.id)){
  21. tempModel = model;
  22. break;
  23. }
  24. }
  25. if(tempModel==null) {
  26. tempModel = new Model();
  27. tempModel.id = initialModel.id;
  28. modelList.add(tempModel);
  29. }
  30. tempModel.eventType.add(new Color(initialModel.color));
  31. }
  32. String resultJson = new Gson().toJson(modelList);
  33. private List<EventCustomDTO> commonListView(List<EventDtoClass> eventDtoClassList) {
  34. List<EventCustomDTO> eventCustomDTOList = new ArrayList<>();
  35. List<EventDtoClass> eventDtoClasses = new Gson().fromJson(eventDtoClassList.toString(), new TypeToken<List<EventCustomDTO>>(){}.getType());
  36. for (EventDtoClass eventDtoClass : eventDtoClasses) {
  37. EventCustomDTO eventCustomDTO = null;
  38. for(EventCustomDTO model : eventCustomDTOList){
  39. if(eventDtoClass.getId().equals(model.getId())){
  40. eventCustomDTO = model;
  41. break;
  42. }
  43. }
  44. if(eventCustomDTO == null) {
  45. eventCustomDTO = new EventCustomDTO();
  46. eventCustomDTO.setId(eventDtoClass.getId());
  47. eventCustomDTOList.add(eventCustomDTO);
  48. }
  49. eventCustomDTO.getEventType().add(new TypesOfEvents(eventDtoClass.getEvenTypeId(), eventDtoClass.getColor()));
  50. }
  51. return eventCustomDTOList;
  52. }

Please note that some parts of the code contain HTML-encoded characters (e.g., &lt; and &gt;). You may need to replace these with their actual characters for the code to work correctly.

英文:

Step 1: Create Schema Classes

Initial Response Model:

  1. public class InitialModel {
  2. String id;
  3. String color;
  4. }

Final/Converted Response Model:

  1. public class Model {
  2. String id;
  3. List&lt;Color&gt; eventType = new ArrayList&lt;&gt;();
  4. class Color {
  5. String color;
  6. public Color(String color) {
  7. this.color = color;
  8. }
  9. }
  10. }

Step 2: Iterate initial response and convert it to the final response

  1. List&lt;Model&gt; modelList = new ArrayList&lt;&gt;();
  2. List&lt;InitialModel&gt; initialModelList = new Gson().fromJson(jsonString, new TypeToken&lt;List&lt;Model&gt;&gt;(){}.getType());
  3. for (InitialModel initialModel : initialModelList) {
  4. Model tempModel = null;
  5. // Check if model with same id already exists
  6. for(Model model : modelList){
  7. if(initialModel.id.equals(model.id)){
  8. tempModel = model;
  9. break;
  10. }
  11. }
  12. // Create new model if id doesn&#39;t exists
  13. if(tempModel==null) {
  14. tempModel = new Model();
  15. tempModel.id = initialModel.id;
  16. modelList.add(tempModel);
  17. }
  18. tempModel.eventType.add(new Color(initialModel.color));
  19. }

Result Json:

  1. String resultJson = new Gson().toJson(modelList);

1.I have add your code but some error is getting

  1. private List&lt;EventCustomDTO&gt; commonListView(List&lt;EventDtoClass&gt; eventDtoClassList) {
  2. List&lt;EventCustomDTO&gt; eventCustomDTOList = new ArrayList&lt;&gt;();
  3. List&lt;EventDtoClass&gt; eventDtoClasses = new Gson().fromJson(eventDtoClassList.toString(), new TypeToken&lt;List&lt;EventCustomDTO&gt;&gt;(){}.getType());
  4. for (EventDtoClass eventDtoClass : eventDtoClasses) {
  5. EventCustomDTO eventCustomDTO = null;
  6. for(EventCustomDTO model : eventCustomDTOList){
  7. if(eventDtoClass.getId().equals(model.getId())){
  8. eventCustomDTO = model;
  9. break;
  10. }
  11. }
  12. if(eventCustomDTO == null) {
  13. eventCustomDTO = new EventCustomDTO();
  14. eventCustomDTO.getId() = eventDtoClass.getId();
  15. eventCustomDTOList.add(eventCustomDTO);
  16. }
  17. eventCustomDTO.getEventType().add(new TypesOfEvents(eventDtoClass.getEvenTypeId(),eventDtoClass.getColor()));
  18. }
  19. return eventCustomDTOList;
  20. }

答案2

得分: 1

以下是已翻译的部分:

由于您的预期结果中有不同的 JSON,您需要有两个类来进行序列化/反序列化。让我们考虑以下类:

  1. @Getter
  2. @AllArgsConstructor
  3. private class ObjectA {
  4. public Long id;
  5. public String color;
  6. }
  7. @ToString
  8. @AllArgsConstructor
  9. private class ObjectB {
  10. public Long id;
  11. public String eventType;
  12. }

现在,在这种情况下获得您期望的结果的代码可能如下所示:

  1. List<ObjectA> object = List.of(new ObjectA(1L, "green"), new ObjectA(1L, "red"), new ObjectA(2L, "yellow"));
  2. object.stream().collect(Collectors.groupingBy(ObjectA::getId))
  3. .entrySet() // from map of grouped ObjectA's
  4. .stream()
  5. .map(entry -> new ObjectB(entry.getKey(), // GroupID
  6. entry.getValue() // list of grouped ObjectA's by current GroupID
  7. .stream()
  8. .map(ObjectA::getColor) // change to stream of all color strings from grouped ObjectA's by current GroupID
  9. .collect(Collectors.joining(", ")))
  10. ).forEach(System.out::println);
  11. //.collect(Collectors.toList());

输出为:

  1. ObjectB(id=1, eventType=green, red)
  2. ObjectB(id=2, eventType=yellow)

您可以根据您的用例简单调整该代码。

英文:

Since you have different JSON in your expected result you will need to have 2 classes to serialize/deserialize from. Lets consider following classes:

  1. @Getter
  2. @AllArgsConstructor
  3. private class ObjectA {
  4. public Long id;
  5. public String color;
  6. }
  7. @ToString
  8. @AllArgsConstructor
  9. private class ObjectB {
  10. public Long id;
  11. public String eventType;
  12. }

Now the code to obtain your expected result in this case would be something like this:

  1. List&lt;ObjectA&gt; object = List.of(new ObjectA(1L, &quot;green&quot;), new ObjectA(1L, &quot;red&quot;), new ObjectA(2L, &quot;yellow&quot;));
  2. object.stream().collect(Collectors.groupingBy(ObjectA::getId))
  3. .entrySet() // from map of grouped ObjectA&#39;s
  4. .stream()
  5. .map(entry -&gt; new ObjectB(entry.getKey(), // GroupID
  6. entry.getValue() // list of grouped ObjectA&#39;s by current GroupID
  7. .stream()
  8. .map(ObjectA::getColor) // change to stream of all color strings from grouped ObjectA&#39;s by current GroupID
  9. .collect(Collectors.joining(&quot;, &quot;)))
  10. ).forEach(System.out::println);
  11. //.collect(Collectors.toList());

With output:

  1. ObjectB(id=1, eventType=green, red)
  2. ObjectB(id=2, eventType=yellow)

You can adjust that code simply for your use case.

huangapple
  • 本文由 发表于 2020年7月31日 16:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188270.html
匿名

发表评论

匿名网友

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

确定