有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java根据对象中的属性添加到列表中

private static CodeList prepareChangeObject(CodeList codeList, CodeList existingCodeList, boolean dataExists)
            throws EntityValidationException {
        CodeList UpdatedCodeList = new CodeList();
        BeanUtils.copyProperties(codeList, UpdatedCodeList);
        Set<CodeListData> updatedDataList =UpdatedCodeList.getData().stream().collect(Collectors.toSet());
        updatedDataList.addAll(existingCodeList.getData());
        List<CodeListData> finalData = updatedDataList.stream().collect(
                collectingAndThen(toCollection(() -> new TreeSet<>(comparing(CodeListData::getKey))), ArrayList::new));
        finalData = finalData.stream().filter(data -> {
            if (data.getOperation() == null)
                data.setOperation(MetaConstants.OPERATION_ADD);
            if (null != data.getOperation()) {
                if (data.getOperation().equals(MetaConstants.OPERATION_DELETE) && dataExists)
                    exceptionMessagesList.add(
                            new ExceptionMessage("foundation.OperationNotAllowed", new String[] { data.getKey() }));
                if (data.getOperation().equals(MetaConstants.OPERATION_DELETE) && !dataExists)
                    return false;
                if (data.getOperation().equals(MetaConstants.OPERATION_ADD))
                    return true;
            }
            return false;

        }).collect(Collectors.toList());
        UpdatedCodeList.setData(finalData);
        if(!CollectionUtils.isEmpty(exceptionMessagesList))
            throw new EntityValidationException(HTTPClientError.BAD_REQUEST, exceptionMessagesList);
        return UpdatedCodeList;
    }

一,。上述方法应基于指定的操作返回一个change对象

    public static void main(String[] args) throws JsonMappingException, JsonProcessingException, EntityValidationException {
    String jsonPost = "{\n"
            + "  \"id\": \"temperature101\",\n"
            + "  \"descriptions\": [\n"
            + "    {\n"
            + "      \"languageCode\": \"en\",\n"
            + "      \"description\": \"description\",\n"
            + "      \"longDescription\": \"long description\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"languageCode\": \"de\",\n"
            + "      \"description\": \"description\",\n"
            + "      \"longDescription\": \"long description\"\n"
            + "    }\n"
            + "  ],\n"
            + "  \"dataType\": \"String\",\n"
            + "  \"data\": [\n"
            + "    {\n"
            + "      \"key\": \"val1\",\n"
            + "      \"value\": \"High\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"key\": \"val4\",\n"
            + "      \"value\": \"Medium\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"key\": \"val3\",\n"
            + "      \"value\": \"Low\"\n"
            + "    }\n"
            + "  ]\n"
            + "}";
    String jsonPatch = "{\n"
            + "  \"id\": \"temperature101\",\n"
            + "  \"descriptions\": [\n"
            + "    {\n"
            + "      \"languageCode\": \"en\",\n"
            + "      \"description\": \"description1\",\n"
            + "      \"longDescription\": \"long description1\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"languageCode\": \"de\",\n"
            + "      \"description\": \"description1\",\n"
            + "      \"longDescription\": \"long description1\"\n"
            + "    }\n"
            + "  ],\n"
            + "  \"dataType\": \"String\",\n"
            + "  \"data\": [\n"
            + "    {\n"
            + "      \"key\": \"val1\",\n"
            + "      \"value\": \"High\",\n"
            + "      \"operation\": \"DELETE\"\n"
            + "\n"
            + "    },\n"
            + "    {\n"
            + "      \"key\": \"val4\",\n"
            + "      \"value\": \"Medium\",\n"
            + "      \"operation\": \"DELETE\"\n"
            + "    }\n"
            + "  ]\n"
            + "}";
    ObjectMapper mapper = new ObjectMapper();
    CodeList ExistingodeList = mapper.readValue(jsonPost, CodeList.class);
    CodeList currentCodeList = mapper.readValue(jsonPatch, CodeList.class);
    CodeList upDatetedCodeList = prepareChangeObject(currentCodeList, ExistingodeList, false);
    Gson gson = new Gson();
    System.out.println(upDatetedCodeList.toString());
    System.out.println(gson.toJson(upDatetedCodeList));
    
}
  1. 以上是测试代码的主要方法
@Data
public class CodeListData {
    
    @Column("ID")
    @JsonIgnore
    private String id;
    
    @Column("KEY")
    private String key;
    
    @Column("VALUE")
    private String value;
    
    @Column("ETAG")
    @JsonIgnore
    private String etag;
    
    @JsonIgnore
    @Column("VERSION")
    @Version
    private Long version;
    
    @Transient
    @JsonProperty(access = Access.WRITE_ONLY)
    private String operation;

}
  1. 供参考
@Data
public class CodeList implements Persistable<String>{

    @NotNull
    @Id
    @Column("ID")
    private String id;
    
    @MappedCollection(idColumn = "ID", keyColumn = "ID_SEQ")
    private List<CodeListDescriptions> descriptions = new ArrayList<>();
    
    @Column("DATA_TYPE")
    private String dataType;
    
    @Column("LENGTH")
    private String length;
    
    @MappedCollection(idColumn = "ID", keyColumn = "ID_SEQ")
    private List<CodeListData> data = new ArrayList<CodeListData>();
    
    @JsonIgnore
    @Column("VERSION")
    private Long version;
    
    @Column("ETAG")
    @JsonIgnore
    private String etag;
    
    @Transient
    @JsonIgnore
    @Setter
    private boolean isInsert;

    @JsonIgnore
    public boolean isNew() {
        return isInsert;
    }
}

四,。代码表pojo

@Data
public class CodeListDescriptions{

    @Column("ISO")
    public String languageCode;
    
    @Column("DESCRIPTION")
    public String description;
    
    @Column("LONG_DESCRIPTION")
    public String longDescription;
    
    @Column("ID")
    @JsonIgnore
    private String id;

}

五,。所需的最终输出只有"val3"个。该方法应该基于操作DELETEADD删除或添加。问题在于,val4仍在输出中,因为在创建finalData时,它倾向于从不包含任何操作的现有数据中保留"val4"。最终输出需要包含附加值和有效负载中未指定但存在于第一个有效负载中的值


共 (2) 个答案

  1. # 1 楼答案

    从现有数据集和更新数据集创建Set<CodeListData>CodeListData在所有字段上定义其equalshashCode方法。请注意,Lombok忽略了transient字段,但这些字段与用@Transient注释的字段不同。所以在Set中有val4两次,一次使用DELETE操作,一次不使用任何操作。带有DELETE的将被过滤掉,但是没有操作的将默认为ADD并保留在SET

    注释:我认为不幸的是,在{{CD14}}中结束任何类型的变量^ {< CD8>}。

  2. # 2 楼答案

    it prefers to keep the val4 from existing data which does not contain any operation

    这段代码默认使用add操作,因此无论是否有操作,都要显式设置一个

    if (data.getOperation() == null)
      data.setOperation(MetaConstants.OPERATION_ADD);
    

    然后你检查一下

    if (null != data.getOperation()) { // this is always true, because you use the default one line before
      ...
      if (data.getOperation().equals(MetaConstants.OPERATION_ADD))
        return true;
    }
    
    

    所以最终决赛

    return false;
    

    永远都联系不到