How to generate a hashcode from object with two list containing the same type of objects

505 views Asked by At

Let's say I have a class (the equal method also exists):

public class SomeClassA {
    private int a;
    private int b;

        @Override
 public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + a;
    result = prime * result + b;
    return result;
 }
}

And another class:

public class SomeClassB {
  List<SomeClassA> firstList;
  List<SomeClassA> secondList;

How do I construct the hashcode so that two objects are seen as equals if they have the same objects in firstList and the same objects in secondList.

//Hank

1

There are 1 answers

6
AudioBubble On BEST ANSWER
 public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + firstList.hashCode();
    result = prime * result + secondList.hashCode();
    return result;
 }