gpt4 book ai didi

java - 迭代两个 hashMap 并比较键并比较形成键的元素以填充最终的 hashMap

转载 作者:行者123 更新时间:2023-12-02 03:22:56 26 4
gpt4 key购买 nike

Class OpsRecord{
private String id;
private long sequenceNumber;

//implemented hashcode and equals method considering both id and version.
//So two OpsRecord are equal only if two OpsRecord has same id and sequenceNumber.
}

Class OpsCompleteRecord{
// This class has 20 class variables including id and sequenceNumber.
}

我有一个现有的Map,它具有以下值。假设这些值来自随机数据源。

Map<OpsRecord, OpsCompleteRecord> sourceMap = new HashMap<>();



sourceMap.put(new OpsRecord("1","1"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1","3"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1","2"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2","2"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2","1"), new OpsCompleteRecord());

我想要一个目标 map ,该 map 应仅包含 OpsCompleteRecord ,其 id 为 1 且 sequenceNumber 为 3 ,以及另一个包含 id=2 和 sequenceNumber=2。[id 的最新 sequenceNumber]

目标 map

new OpsRecord("1","3"), new OpsCompleteRecord()
new OpsRecord("2","2"), new OpsCompleteRecord()

我正在尝试使用以下逻辑和代码来实现。需要您帮助填充 targetMap。我想我搞乱了逻辑。

public static void main(String[] args) {

Map<OpsRecord, OpsCompleteRecord> sourceMap = new HashMap<>();

sourceMap.put(new OpsRecord("1","1"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1","3"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1","2"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2","2"), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2","1"), new OpsCompleteRecord());

// Populate the target map
Map<OpsRecord, OpsCompleteRecord> targetMap = new HashMap<>();

Iterator<OpsRecord> itr = sourceMap.keySet().iterator();

while(itr.hasNext()){
OpsRecord opsRecord = itr.next();
// Iterate over targetMap.keySet().
// Take each key.getId()
// Compare targetMap key.getId() with opsRecord.getId()
// and if equal then Compare targetMap key.getSequenceNumber() with opsRecord.getSequenceNumber()
// if getSequenceNumber is small in targetMap, overwrite the object else ignore.
// else if all key.getId() is not equal to opsRecord.getId() then add that key and value to targetMap
}

}

我使用的是Java7。

最佳答案

Java7中,也许你可以尝试使用guava Maps.filterKeys,例如:

    Map<OpsRecord, OpsCompleteRecord> sourceMap = new HashMap();

sourceMap.put(new OpsRecord("1", 1), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1", 3), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1", 2), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2", 2), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2", 1), new OpsCompleteRecord());
Map<OpsRecord, OpsCompleteRecord> targetMap = Maps.filterKeys(sourceMap, new Predicate<OpsRecord>() {
@Override
public boolean apply(OpsRecord input) {
return input.id.equals(Long.toString(input.sequenceNumber));
}
});

Java8中,您可以将streamfilter一起使用,collector可以轻松做到这一点:

    sourceMap.put(new OpsRecord("1", 1), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1", 3), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("1", 2), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2", 2), new OpsCompleteRecord());
sourceMap.put(new OpsRecord("2", 1), new OpsCompleteRecord());
Map<OpsRecord, OpsCompleteRecord> targetmap =
sourceMap.entrySet().stream()
.filter(i -> i.getKey().id.equals(Long.toString(i.getKey().sequenceNumber)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

还有一点需要注意:sequenceNumberlong类型,所以应该使用new OpsRecord("2", 1)

希望对您有所帮助。

关于java - 迭代两个 hashMap 并比较键并比较形成键的元素以填充最终的 hashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39382447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com