如果列表中存在相同的元素,则避免添加相同的元素,而只是将元素添加到现有列表中

avoid adding the same element if exist in the list instead just add the elements inside the existing list

下面是示例代码

                String jsonString = "{\n" +
                        "   \"models\":[\n" +
                        "      {\n" +
                        "         \"model\":{\n" +
                        "            \"code\":\"ALL\",\n" +
                        "            \"type\":null,\n" +
                        "            \"name\":\"ALL\",\n" +
                        "            \"feature_types\":null\n" +
                        "         }\n" +
                        "      },\n" +
                        "      {\n" +
                        "         \"model\":{\n" +
                        "            \"code\":\"102e\",\n" +
                        "            \"defaultLookup\":\"false\",\n" +
                        "            \"type\":\"SIT\",\n" +
                        "            \"name\":\"MUSTANG\",\n" +
                        "            \"feature_types\":[\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"A\",\n" +
                        "                     \"desc\":\"All feature types\"\n" +
                        "                  }\n" +
                        "               },\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"B\",\n" +
                        "                     \"desc\":\"Series\"\n" +
                        "                  }\n" +
                        "               },\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"C\",\n" +
                        "                     \"desc\":\"BodyStyle\"\n" +
                        "                  }\n" +
                        "               }\n" +
                        "            ]\n" +
                        "         }\n" +
                        "      },\n" +
                        "      {\n" +
                        "         \"model\":{\n" +
                        "            \"code\":\"980p\",\n" +
                        "            \"defaultLookup\":\"false\",\n" +
                        "            \"type\":\"SIT\",\n" +
                        "            \"name\":\"Ranger\",\n" +
                        "            \"feature_types\":[\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"C\",\n" +
                        "                     \"desc\":\"All feature types\"\n" +
                        "                  }\n" +
                        "               },\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"D\",\n" +
                        "                     \"desc\":\"Series\"\n" +
                        "                  }\n" +
                        "               } \n" +
                        "            ]\n" +
                        "         }\n" +
                        "      },\n" +
                        "      {\n" +
                        "         \"model\":{\n" +
                        "            \"code\":\"kkpou\",\n" +
                        "            \"defaultLookup\":\"false\",\n" +
                        "            \"type\":\"UAT\",\n" +
                        "            \"name\":\"Transit Custom\",\n" +
                        "            \"feature_types\":[\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"F\",\n" +
                        "                     \"desc\":\"All feature types\"\n" +
                        "                  }\n" +
                        "               },\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"G\",\n" +
                        "                     \"desc\":\"Series\"\n" +
                        "                  }\n" +
                        "               },\n" +
                        "               {\n" +
                        "                  \"feature_type\":{\n" +
                        "                     \"code\":\"H\",\n" +
                        "                     \"desc\":\"Payload\"\n" +
                        "                  }\n" +
                        "               }\n" +
                        "            ]\n" +
                        "         }\n" +
                        "      }\n" +
                        "   ]\n" +
                        "}";
    
    for(int i = 0; i<myData.size();i++)
        {
            String type = "SIT";
            FeaturedItems item = resultList.stream().filter(featureItem -> type != null && type.equals(featureItem.getType())).findFirst().orElse(null);
            if (type != null) {
                item = FeaturedItems.builder().type(type).items(new ArrayList<>()).build();
                resultList.add(item);//if the item already exists in the list don't add the new item, instead just add the elements in the exisiting item.
 //tried the below commented code to add the item if it doesn't contain in the list -- start
                  /*boolean flagFound = false;
                    for (FeaturedItems featureItem : resultList) {
                        if (featureItem.getType().equalsIgnoreCase(type)) {
                            flagFound = true;
                            break;
                        }
                    }
                  if(!flagFound) resultList.add(item);*/
 //tried the above commented code to add the item if it doesn't contain in the list -- End
                for (int count = 0; count < features.size(); count++) {
                    String id = getFid(count);
                    MyDataBuild build = ....//logic to set values in the properties
                    item.getItems().add(build);
                }
            }    
        }
      lookUpData.setFeatureGroups(resultList);
    }   
    }
    

如果 type 值已经在定义的 featureItems 中定义,那么我不会在 featureItems 列表中创建新对象,而是需要将 unique items(desc,id) 添加到现有的 items 元素中以匹配 type .如果类型在 featureItems 列表中匹配,上面提到的代码片段不会将元素添加到现有项目,而是创建新元素,如输出 json 示例中所示。

改用 Map 会让您的生活更轻松。但是,您的示例缺少一些数据,因此很难理解您的代码中实际发生了什么。所以我只能给你一个简单的用法示例。

Map<String, FeaturedItems> resultMap = new HashMap<>();
// Get the FeaturedItems for the given type. If none is present create a new one.
FeaturedItems items = resultMap.computeIfAbsent(type, k -> FeaturedItems.builder().type(k).items(new ArrayList<>()).build());
// Add your item to the list
Sale newItem // Obtain new item
items.getItems().add(newItem);