I need to store object type of class edge as Key and ArrayList as Value
public class edge {
String edgeType; // Horizontal or Vertical edge
int i; //i and j for a cell in 2D matrix
int j;
}
HashMap<edge, ArrayList<String>> edgeHM = new HashMap<edge, ArrayList<String>>();
Each key's value Arraylist is initialized with 0 and 1
When I retrieve and edit one of the entries using the function reduceEdgeDomain
multiple keys get updated with the value. Hash Collision?
Eg. need to update Key H21
, keys updated H21
and V21
(edgetype, i, j)
H22 <-> V22 or V53 <-> H53
public static void reduceEdgeDomain(HashMap<edge, ArrayList<String>> edgeHM, ArrayList<edge> nonEssEdges){
ArrayList<String> tempAL;
for(int i=0; i<nonEssEdges.size(); i++)
{
edge str = nonEssEdges.get(i);
if(edgeHM.containsKey(str)){
tempAL = edgeHM.get(str);
edgeHM.remove(str);
tempAL.remove("1");
edgeHM.put(str, tempAL);
}
}
}
override methods for equals()
and hashcode()
public boolean equals(Object o){
if (this == o){
return true;
}
if(!(o instanceof edge)){
return false;
}
edge edge = (edge) o;
return i==edge.i && j==edge.j && Objects.equals(edgeType, edge.edgeType);
}
@Override
public int hashCode() {
String uniqueIdentified = edgeType + i + j;
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] hash = digest.digest(uniqueIdentified.getBytes(StandardCharsets.UTF_8));
ByteBuffer wrapped = ByteBuffer.wrap(hash); // big-endian by default
short num = wrapped.getShort();
return num;
}