Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing function used in Java Language

I know that Java has beautiful inbuilt support for the HashMaps or HashTables.

Does anybody have knowledge that what kind of hashing functions or techniques are employed by Java language?

Is it possible to tweak those functions to be able to make them more specific to one's application in order to improve performance and reducing access time?

Thanks a lot for reading!

like image 934
shuby_rocks Avatar asked Nov 29 '22 05:11

shuby_rocks


1 Answers

Java allows you to override the hashCode() method for your Classes to use a hashing algorithm that is not only well suited to your application, but to your individual types:

public class Employee {

   private int id;
   // Default implementation might want to use "name" for as part of hashCode
   private String name; 

   @Override
   public int hashCode() {
     // We know that ID is always unique, so don't use name in calculating 
     // the hash code.
     return id;
   }
}
like image 83
levik Avatar answered Dec 09 '22 13:12

levik