Java语言中使用哈希函数 [英] Hashing function used in Java Language

查看:1035
本文介绍了Java语言中使用哈希函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Java对HashMaps或HashTables有内置的支持。

有人知道Java语言使用什么样的哈希函数或技术吗?

是否可以调整这些函数以使其更适合于某个应用程序以提高性能并缩短访问时间?



非常感谢您的阅读!

解决方案

重写您的类的 hashCode()方法以使用不仅适合您的应用程序,而且适合您的个人类型的哈希算法:

  public class Employee {

private int id;
//默认实现可能希望使用name作为hashCode的一部分
private String name;

@Override
public int hashCode(){
//我们知道ID总是唯一的,所以不要在计算
时使用名称// hash码。
return id;
}
}


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!

解决方案

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;
   }
}

这篇关于Java语言中使用哈希函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆