I would like to check the equality of two LinkedHashMaps in Java.
equals()The method is in AbstractMapand only checks if the same key and value are in the comparison list. Thus, the insertion order is not checked:
package com.stackoverflow.tests;
import java.util.LinkedHashMap;
public class LinkedHashMapEqualsTest {
public static void main(String[] args) {
LinkedHashMap<String, String> lhm1 = new LinkedHashMap<String, String>();
lhm1.put("A", "1");
lhm1.put("B", "2");
lhm1.put("C", "3");
LinkedHashMap<String, String> lhm2 = new LinkedHashMap<String, String>();
lhm2.put("A", "1");
lhm2.put("B", "2");
lhm2.put("C", "3");
LinkedHashMap<String, String> lhm3 = new LinkedHashMap<String, String>();
lhm3.put("A", "1");
lhm3.put("C", "3");
lhm3.put("B", "2");
LinkedHashMap<String, String> lhm4 = new LinkedHashMap<String, String>();
lhm4.put("A", "1");
lhm4.put("B", "2");
LinkedHashMap<String, String> lhm5 = new LinkedHashMap<String, String>();
lhm5.put("A", "2");
lhm5.put("B", "2");
lhm5.put("C", "3");
if(lhm1.equals(lhm1)) {
System.out.println("Positive control. - SUCCESS");
}
if(lhm1.equals(lhm2)) {
System.out.println("lhm1 does equal lhm2; as expected. - SUCCESS");
}
if(lhm1.equals(lhm3)) {
System.out.println("lhm1 does equal lhm3, although the insert-order is different.");
}
if(!lhm1.equals(lhm4)) {
System.out.println("Negative control 1. - SUCCESS");
}
if(!lhm1.equals(lhm5)) {
System.out.println("Negative control 2. - SUCCESS");
}
}
}
How can I check if the insertion order is the same for both comparison lists?
source
share