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 ...
随机推荐
- Specification模式的一个不错的示例代码
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Specifi ...
- requests.exceptions.MissingSchema: Invalid URL 'xxxxxxxxxxxxx': No schema supplied. Perhaps you meant xxxxxxxxxxxxx
import requests session = requests.session() carProposalUrl = "www.caaaa.com.cn/aaaa/aaaaa/carP ...
- Adreno Profiler连接安卓手机profile第三方app渲染过程
1,下载Android SDK,adb在Android SDK/platform-tools下. 2,添加adb环境变量:http://www.cnblogs.com/xwlyun/archive/2 ...
- JSP应用开发 -------- 电纸书(未完待续)
http://www.educity.cn/jiaocheng/j9415.html JSP程序员常用的技术 第1章 JSP及其相关技术导航 [本章专家知识导学] JSP是一种编程语言,也是一种动 ...
- 又开一坑,运动图形MoGraph for Unity
Fragment+random: Vertex+random, Vertex+plain Vertex+Sound Plexus like 写了个大概,暂时没这方面需求先放这边了. C4D原版片段和克 ...
- vivado error [Labtools 27-2149]
使用vivado2014.3时遇到了这种错误 ERROR: [Labtools 27-2149] File E:/project/V7_PCIE/prj/PhasedProject/M_PcieGen ...
- PHPCMS模块开发相关文件
名称 类型说明 Guestbook 模块后台控制器 index 模块前台控制器 Install 安装文件夹 ├templates ├config.inc.php ├extention.inc.php ...
- C#多线程方法同步
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- python-list.sort && lambda
dictionary是一个有元组组成的list,变量名有点歧义,这里是想表达 由元组组成的list哈. 之所以用dictionary是因为模拟的将字典转换成list. 将list进行排序,而根据lam ...
- Action的mapping.findFoward(forwardName)必须要在struts-config.xml中的对应的action节点配置一个forward节点
比如说你有个SampleAction,在execute(ActionMapping mapping, ...)中写了句 return mapping.findForward("some_pa ...