Comparator that can compare LinkedHashMaps
I am new to Comparators
in Java
Can someone help 开发者_运维百科me to create a Comparator
to compare LinkedHashmaps
by comparing keys/values?
Each Map
(actually, all subclasses of AbstractMap
) has already implemented the equals(..)
method in a way that it compares the key/value pairs.
So, if using a Comparator
, you can use:
public int compare(Map first, Map second){
// appropriate null checks here
return first.equals(second) ? 0 : 1;
}
But I'd rather put the Map
s in a new HashSet
, which will automatically remove duplicates:
Set<Map> uniqueMaps = new HashSet<Map>(listOfMaps);
I'd do something like this: (psuedocode)
for(each entry in firstList) {
a=firstList.nextKey();
b=secondList.nextKey();
if(a != b !! firstList.get(a) != firstList.get(b))
return false;
}
return true;
but that implies that you are using object identity (in otherwords, each object must be the same exact object in both lists).
If this isn't the case you have to implement .equals in every single object that might be in either list, then replace:
if(a != b ...
with
if(!a.equals(b)...
or something like that.
Also, as someone said in the comments, there is a really good chance that there is a better way than this to solve whatever problem you are trying to solve.
Here is a sample Pseudo-code. It does not handle nulls (which can be added if you want) and compares Map and a Map will not be very different/difficult :). There is a test case and lot of lines wasted to actually set up a list of maps, but should be easy to follow.
package com.ekanathk;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
class MapEntryComparator implements Comparator<Map<String, String>> {
private String[] keysToCompare;
//What are the key columns you want to compare ???
public MapEntryComparator(String[] keysToCompare) {
this.keysToCompare = keysToCompare;
}
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
for(String s: keysToCompare) {
//HANDLE NULLS if any
int comparison = o1.get(s).compareTo(o2.get(s));
if(comparison != 0) {
return comparison;
}
}
return 0;
}
}
public class ComparatorCheck {
@Test
public void testSimple() {
LinkedHashMap<String, String> m1 = getMapSample("X", "M", "C", "D");
LinkedHashMap<String, String> m2 = getMapSample("A", "B", "C", "D");
LinkedHashMap<String, String> m3 = getMapSample("A", "B", "C", "D");
LinkedHashMap<String, String> m4 = getMapSample("A", "B", "X", "D");
List<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String,String>>();
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
MapEntryComparator comparator = new MapEntryComparator(new String[]{"k1", "k2", "k3"});
Collections.sort(list, comparator);
assertEquals(m2, list.get(0));
assertEquals(m3, list.get(1));
assertEquals(m4, list.get(2));
assertEquals(m1, list.get(3));
}
private LinkedHashMap<String, String> getMapSample(String v1, String v2, String v3, String v4) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("k1", v1);
map.put("k2", v2);
map.put("k3", v3);
map.put("k4", v4);
return map;
}
}
精彩评论