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());
我想要一个仅包含
OpsCompleteRecord
且id
为3的sequenceNumber
以及另一个id
= 2和sequenceNumber
= 2的目标地图。[对于
sequenceNumber
最新的id
]targetMap
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中,可以将
stream
与filter
一起使用,并且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));
另一件事需要注意:
sequenceNumber
是long
类型,因此应使用new OpsRecord("2", 1)
。希望它会有所帮助。