Java散列和散列码的实现
转自:https://blog.csdn.net/al_assad/article/details/52989525
- <span style="color:#000000;">import java.util.*;
- public class SimpleHashMap<K,V> extends AbstractMap<K,V>{
- static final int SIZE = 997; //默认bucket数量;
- @SuppressWarnings("unchecked")
- LinkedList<MapEntry<K,V>>[] buckets = new LinkedList[SIZE];
- //放置映射关系,返回原先的映射Value;
- public V put (K key, V value){
- int index = Math.abs(key.hashCode()) % SIZE;
- if(buckets[index] == null)
- buckets[index] = new LinkedList<MapEntry<K, V>>();
- V oldValue = null;
- LinkedList<MapEntry<K,V>> bucket = buckets[index];
- MapEntry<K, V> pair = new MapEntry<K,V>(key,value);
- ListIterator<MapEntry<K,V>> iter = bucket.listIterator();
- //检查重复key,更新value
- boolean found = false;
- while(iter.hasNext()){
- MapEntry<K,V> tempPair = iter.next();
- if(tempPair.getKey().equals(key)){
- oldValue = tempPair.getValue();
- iter.set(pair);
- found = true;
- break;
- }
- }
- if(!found)
- buckets[index].add(pair);
- return oldValue;
- }
- public V get(Object key){
- int index = Math.abs(key.hashCode()) % SIZE;
- if(buckets[index] == null)
- return null;
- for(MapEntry<K,V> pair : buckets[index]){
- if(pair.getKey().equals(key))
- return pair.getValue();
- }
- return null;
- }
- @Override
- public Set<Map.Entry<K, V>> entrySet() {
- Set<Map.Entry<K, V>> set = new HashSet<Map.Entry<K,V>>();
- for(LinkedList<MapEntry<K,V>> bucket : buckets){
- if(bucket == null)
- continue;
- for(MapEntry<K,V> pair : bucket)
- set.add(pair);
- }
- return set;
- }
- class MapEntry<K,V> implements Map.Entry<K, V>{
- K key;
- V value;
- public MapEntry(K key,V value){
- this.key = key;
- this.value = value;
- }
- @Override
- public K getKey(){
- return this.key;
- }
- @Override
- public V getValue() {
- return this.value;
- }
- @Override
- public V setValue(V value) {
- return this.value = value;
- }
- }
- /*实现可以引用在在HashMap对象的key对象,该对象必须实现hashCode方法*/
- import java.util.*;
- public class CountString {
- private static List<String> created = new ArrayList<String>();
- //用于记录一次程序运行创建的所哟 String s实例,当s发生重复时,生成唯一的id,以产生唯一的散列值;
- private String s ; //用于记录内容的数据域
- private int id = 0; //记录相同String的CountString对象的编号
- public CountString(String str){
- this.s = str;
- created.add(s);
- for(String e : created){
- if(e.equals(s))
- this.id++;
- }
- }
- public int hashCode(){
- int result = 17; //设置初始偏移量
- result = 37*result + s.hashCode(); //37稀释值
- result = 37*result + id; //进行第二次稀释,放置ID值破坏s.hasCode;
- return result;
- }
- public boolean equals(Object o){
- return o instanceof CountString && s.equals(((CountString)o).getS())
- && id == ((CountString)o).getId();
- }
- public String getS(){
- return this.s;
- }
- public int getId(){
- return this.getId();
- }
- public String toString(){
- return "String:"+s+" id:"+id+" hashCode: "+hashCode();
- }
- }
- </span>
Java散列和散列码的实现的更多相关文章
- 【Java集合学习】HashMap源码之“拉链法”散列冲突的解决
1.HashMap的概念 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射. HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io ...
- java 散列与散列码探讨 ,简单HashMap实现散列映射表运行各种操作示列
java 散列与散列码探讨 ,简单HashMap实现散列映射表运行各种操作示列 package org.rui.collection2.maps; /** * 散列与散列码 * 将土拔鼠对象与预报对象 ...
- [转]java.sql.SQLException: 无效的列索引
原文地址:http://blog.sina.com.cn/s/blog_6bc811e401011a17.html java.sql.SQLException: 无效的列索引 “无效的列索引”其实是个 ...
- java.sql.SQLException: 无效的列索引
java.sql.SQLException: 无效的列索引 "无效的列索引"其实是个低级的错误,原因无非几个: 1.sql串的?号数目和提供的变量数目不一致: 例如:jdbcTem ...
- Java.sql.SQLException: 无效的列类型: 1111
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: ...
- Cause: java.sql.SQLException: 无效的列索引
今天调试代码发现“Cause: java.sql.SQLException: 无效的列索引”,查资料得出结论如下: 1.sql串的?号用''括了起来. 例如:select* from user t ...
- oracle: jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
https://www.cnblogs.com/mmlw/p/5808072.html org.mybatis.spring.MyBatisSystemException: nested except ...
- Java JTable列顺序和列宽度保存在用户本地
上周碰到了一个棘手的需求,就是要把用JTable的列顺序和列宽度保存下来,这次用户调整了列宽度,关闭程序,下次再打开时,这个列的宽还是要保持,因为SWing的特性,都是在程序启动时就确定了列顺序和列宽 ...
- java学习-zxing生成二维码矩阵的简单例子
这个例子需要使用google的开源项目zxing的核心jar包 core-3.2.0.jar 可以百度搜索下载jar文件,也可使用maven添加依赖 <dependency> <gr ...
随机推荐
- Solr4.0使用
http://blog.sina.com.cn/s/blog_64dab14801013k7g.html Solr简介 Solr是一个非常流行的,高性能的开源企业级搜索引擎平台,属于Apache Lu ...
- WPF SL 属性生成器
在开发WPF 和SL应用的时候通用会用到MVVM模式,每次写到类属性的时候要不断的写属性更新时通知方法,写多了就嫌烦,就手写了个属性生成工具,在属性更新的时候添加了更新通知方法. 工具中支持自定义类对 ...
- 数据库对m³等特殊符号的支持
目前我只遇到过m³这个特殊符号,会影响正常使用. 比如,我在数据库中搜索: select * from table where container='10m³'; 即使数据库中对应的值,但也无法搜索出 ...
- unity3d一个物体围绕另一个物体旋转
可以使用RotateAround,代码如下: transform.RotateAround (Sun.transform.position, Vector3.down, ); 其中第一个参数是要围绕哪 ...
- jquery的onclick(this)方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android studio - 提取局部变量,全局变量,方法快捷键
提取局部变量:Ctrl+Alt+V 提取全局变量:Ctrl+Alt+F 提取方法:Shit+Alt+M
- Ehcache2 的配置(不使用配置文件)
EhCache是一个开放源码的,基于标准的高速缓存系统. 网上关于EhCache的使用配置很多,但是一般是基于配置文件的.但是实际应用中.我们可能需要动态的管理缓存,这时候单纯配置文件就不够用了. 所 ...
- 解决linux下/etc/rc.local开机器不执行的原因
前不久因项目需要写了开机启动其他程序的shell脚本,因工作忙,调试完给了技术支持人员,也没去注意过. 到后来,有几台服务器突然被重启了,这时候领导问,怎么开机启动的脚本没起作用,还被批了一顿,哎,做 ...
- linux下出现ping:unknown host www.baidu.com问题时的解决办法——ubuntu下局域网络的配置
如果ping域名的时候出现ping:unknown host xxx.xxx 但是ping IP地址的时候可以通的话 可知是dns服务器没有配置好, 查看一下配置文件/etc/resolv.conf ...
- bzoj2005 能量采集 莫比乌斯或者普通容斥
/** 题目:bzoj2005 能量采集 链接:https://vjudge.net/contest/178455#problem/F 题意:栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可 ...