I have two LinkedHashMap<String, MyType>
where keys can be common (say key from first map can be "key" and key from second map can be also "key". I need to compare this two maps and when keys are equal I want create new object where firsMapEntry.getValue()
secondMapEntry.getValue()
are attributes of new object [ new MyObject(firsMapEntry.getValue(), secondMapEntry.getValue())
]. Maps are in Collection
so I iterate through collections.
Here's my source:
firstIterator = firsCollection.iterator();
secondIterator = secondCollection.iterator();
while (firstIterator.hasNext() && secondIterator.hasNext()) {
Map<String, MyType1> firsMap = firstIterator.next();
Map<String, List<MyType2>> secondMap = secondIterator
.next();
firstMapIterator = firstMap.entrySet().iterator();
secondMapIterator = secondMap.entrySet().iterator();
while (firstMapIterator.hasNext() && secondMapIterator.hasNext()) {
Map.Entry<String, MyType1> firstEntry = ((Map.Entry<String, MyType1>) firstMapIterator.next());
Map.Entry<String, List<MyType2>> secondEntry = ((Map.Entry<String, List<MyType2>>) secondMapIterator.next());
// Here I was trying to compare keys but unsuccessfuly
// if keys are equal create new object
My Object object = new MyObject(firsEntry.getValue(),
secondEntry.getValue());
Map<String, MyObject> map = new LinkedHashMap<String,MyObject(); map.put("key", object);
}
}
Return type of method is List<Map<String, MyObject>>
.
I don't know if my method is effecient, but how to compare two keys and create new object from values. Every help will be appreciated. Thank you.