简体   繁体   中英

Iterate over two hashMap and compare the keys and compare the elements that form the key to populate final hashMap

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.
}

I have an existing Map that has the following values. Assuming the values are coming from a random data source.

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());

I want a target map that should contain only OpsCompleteRecord whose id is 1 and sequenceNumber is 3 , and another one with id =2 and sequenceNumber =2. [Latest sequenceNumber for a id ]

targetMap

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

I am trying to implement with the below logic and code. Need your help in populating targetMap. I think I am messing up with the logic.

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           
        }       

    }

I am using Java7.

In Java7 , maybe you can try to use guava Maps.filterKeys , like:

    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));
        }
    });

In Java8 , you can use stream with filter and collector can easy to do this:

    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));

another thing need to call out: sequenceNumber is long type, so it should use new OpsRecord("2", 1) .

Hopefully, it's helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM