In this Q&A, we'll go over methods provided by Java to compute hash code for a variable list of objects
Objects class provides a hash() function to calculate hash code for an array of objects
int hc = Objects.hash("1","2","3","4");
System.out.println(hc);
This method is just a simple wrapper around Arrays.hashCode(). Arrays.hashCode() calls hashCode() of each element to arrive at the hash code. Hash code returned by Arrays.hashCode() is consistent with the code returned by List objects. See code below.
int[] ia = {1,2,3,4};
List<Integer> li = Arrays.asList(1,2,3,4);
assertEquals("HC equals", Arrays.hashCode(ia), li.hashCode());
For nested arrays, if you want a hash code based on the contents of nested array elements you can use the deepHashCode() method
int[] ia = {1,2,3,4};
List<Integer> li = Arrays.asList(1,2,3,4);
assertEquals("HC equals", Arrays.hashCode(ia), li.hashCode());
For nested arrays, if you want a hash code based on the contents of nested array elements you can use the deepHashCode() method
Comments
Post a Comment