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 ...
随机推荐
- elem.attr()无法正确判断checkbox是否选中
本篇文章由:http://xinpure.com/elem-attr-does-not-correctly-determine-whether-the-checkbox-is-selected/ 关于 ...
- FtpHelper类匿名获取FTP文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- jQuery源代码学习:经常使用正則表達式
转载自:http://nuysoft.iteye.com/blog/1217898 经常使用的数字正则(严格匹配) 正则 含义 ^[1-9]\d*$ 匹配正整数 ^-[1-9]\d*$ 匹配负整数 ^ ...
- [Oracle] enq: TX - row lock contention 优化案例
依据开发反馈.近期每天早上7:30应用会报警.应用的日志显示数据库连接池满了.新的连接被拒绝. 首先.我做了ASH报告(报告区间:7:25 ~ 7:35),从ASH的等待事件发现enq: TX - r ...
- GTD实用指南(转载)
时间管理第一层:记录每日时间开支,认识自己的时间黑洞.你会惊讶地发现,每天的无意义时间很可能在70%以上. [避开时间黑洞的小策略:1.彻底关闭聊天工具:2.关掉邮件的到达提醒功能:3.保持办公环境的 ...
- php 获取客户端的浏览器信息
就是访问的时候,通过服务端来判断用户是否为移动端,如果是的话就重定向(移动端的页面).事实上现在都是一套搞定的了. 但是还是记录一下吧.没准以后用的到 http://detectmobilebr ...
- Groovy小结:java调用Groovy方法并传递参数
Groovy小结:java调用Groovy方法并传递参数 @(JAVA总结) 1. 场景描述 在网上查了资料发现,java有三种方式调用groovy脚本.但是真正在实际的服务器环境中,嵌入groovy ...
- 点滴积累【SQL Server】---使用Kettle实时同步DB2数据到SQLserver
效果: 描述: 此操作适用于单点登录的同步用户. 首先,使用kettle将DB2数据同步到SQL中,然后添加到windows的任务计划中.定时执行同步数据. 特殊说明:此工具涉及到公司版权,所以不方便 ...
- 547. Intersection of Two Arrays【easy】
Given two arrays, write a function to compute their intersection. Notice Each element in the result ...
- python之字符串处理
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: strformat.py #A ...