猿问

比较两个 java 列表并将元素添加到最终列表?

我现在有两个具有相同类型对象的列表,我正在尝试比较这两个列表并创建具有这些限制的最终列表

  • dbRecord列表的所有记录应该在最终列表中可用

  • 如果两个列表的对象labelName相同,则从列表dbrecord中更新列表的对象userRecord

  • 如果在列表中发现任何额外记录(与的列表对象userRecord比较),则应将其添加到最终列表中。labelNamedbRecord

我尝试使用此代码,但问题是它没有将userRecord列表中找到的任何额外对象添加到最终列表

List<Labels> dbRecord = roleRecord.getLabels();

List<Labels> userRecord = role.getLabels();

List<Labels> updatedLables = new ArrayList<>();

boolean recordFound = false;

            for (Labels labels : dbRecord) {

                innerloop: for (Labels innerLabels : userRecord) {

                    if (StringUtils.compare(labels.getLabelName().replaceAll("\\s+", "").toLowerCase(),

                            innerLabels.getLabelName().replaceAll("\\s+", "").toLowerCase()) == 0) {

                        recordFound = true;

                        labels.setAccess(innerLabels.getAccess());

                        labels.setMatch(innerLabels.getMatch());

                        updatedLables.add(labels);

                    }

                    if (recordFound) {

                        break innerloop;

                    }

                }

                if (!recordFound) {

                    updatedLables.add(labels);

                }

                recordFound = false;

            }

我在这段代码中缺少什么逻辑?



慕仙森
浏览 84回答 1
1回答

德玛西亚99

这样的事情适合您的需要吗?List<Labels> dbRecord = roleRecord.getLabels();&nbsp; &nbsp; &nbsp; &nbsp; List<Labels> userRecord = role.getLabels();&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Labels> labelsByName = dbRecord.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(this::getCorrectedLabelName, i -> i));&nbsp; &nbsp; &nbsp; &nbsp; userRecord.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(label -> labelsByName.containsKey(getCorrectedLabelName(label)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(label -> labelsByName.put(getCorrectedLabelName(label), label));&nbsp; &nbsp; &nbsp; &nbsp; Collection<Labels> updatedLabels = labelsByName.values();和private String getCorrectedLabelName(Labels label) {&nbsp; &nbsp; &nbsp; &nbsp; return label.getLabelName().replaceAll("\\s+", "").toLowerCase();&nbsp; &nbsp; }首先,您使用列表按名称创建标签映射dbRecord。userRecord然后您可以轻松地从中找到与名称匹配的那些,并为它们中的每一个替换地图中的标签。最后你收集地图值!PS:这都是假设Labels没有重名!希望这可以帮助- - 编辑&nbsp;List<Labels> dbRecord = roleRecord.getLabels();&nbsp; &nbsp; &nbsp; &nbsp; List<Labels> userRecord = role.getLabels();&nbsp; &nbsp; &nbsp; &nbsp; // create Map from dbRecord&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Labels> labelsByName = dbRecord.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(this::getCorrectedLabelName, i -> i));&nbsp; &nbsp; &nbsp; &nbsp; userRecord&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for each of userRecord&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(userRecordLabel -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if in dbRecord, update dbRecord labels&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(labelsByName.containsKey(getCorrectedLabelName(userRecordLabel))){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Labels dbRecordLabel = labelsByName.get(getCorrectedLabelName(userRecordLabel));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbRecordLabel.setAccess(userRecordLabel.getAccess());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbRecordLabel.setMatch(userRecordLabel.getMatch());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelsByName.put(getCorrectedLabelName(userRecordLabel), dbRecordLabel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{// if not then add to map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelsByName.put(getCorrectedLabelName(userRecordLabel), userRecordLabel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; // final complete list&nbsp; &nbsp; &nbsp; &nbsp; Collection<Labels> updatedLabels = labelsByName.values();这是应该工作或你的完整更正代码!
随时随地看视频慕课网APP

相关分类

Java
我要回答