链接法散列表的 java 实现

本文介绍了一种基于单独链接法解决冲突的散列表实现方法,并通过一个具体的Java代码示例展示了如何构造散列表,包括插入、删除、查找等基本操作。此外,还提供了一个简单的员工信息录入案例来演示散列表的实际运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.*;

/*
 * 此散列表只对提供适当 equals 方法和返回一个 int 型量的 hashCode 方法的对象工作
 */
public class SeparateChainingHashTable<Type> {
    private static final int DEFAULT_TABLE_SIZE = 5;

    private List<Type> [] theLists;
    private int currentSize;        //插入的项数

    private void rehash() {
        List<Type> [] oldLists = theLists;

        //创建一个两倍的表
        theLists = new List[ 2 * theLists.length ];

        for ( int i = 0; i < theLists.length; i++) 
            theLists[i] = new LinkedList<Type>();

        //把旧表插入
        currentSize = 0;
        for ( int i = 0; i < oldLists.length; i++) {
            for ( Type item : oldLists[i])
                insert(item);
        }
    }

    private int myhash( Type x ) {
        int hashVal = x.hashCode();

        hashVal %= theLists.length;
        if ( hashVal < 0 )
            hashVal += theLists.length;

        return hashVal;
    }

    public SeparateChainingHashTable() {
        this(DEFAULT_TABLE_SIZE);
    }

    public SeparateChainingHashTable( int size ) {
        theLists = new LinkedList [ size ];
        for ( int i = 0; i < theLists.length; i++) {
            theLists[i] = new LinkedList<Type>();
        }
    }

    public void insert( Type x ) {
        List<Type> list = theLists[ myhash(x)];
        if ( !list.contains(x) ) {
            list.add(x);

            if (++currentSize > 2 * theLists.length)  
                rehash();
        }
    }

    public void remove( Type x ) {
        List<Type> list = theLists[ myhash(x) ];
        if ( list.contains(x)) {
            list.remove(x);
            currentSize--;
        }
    }

    public boolean contains( Type x ) {
        List<Type> list = theLists[ myhash(x) ];
        return list.contains(x);
    }

    public void makeEmpty() {
        for( int i = 0; i < theLists.length; i++) 
            theLists[i].clear();
        currentSize = 0;
    }

    public void print() {
        for(int i = 0; i < theLists.length; i++) {
            System.out.print(i);
            System.out.println(theLists[i]);
        }
    }

    public static void main( String [] args ) {
        Scanner scanner = new Scanner(System.in);
        SeparateChainingHashTable<Employee> hashTable = new SeparateChainingHashTable<>();

        System.out.println("请输入名字:");
        String n = scanner.next();

        while ( !n.equals("!") ) {
            Employee employee = new Employee(n);
            hashTable.insert(employee);
            n = scanner.next();
        }
        hashTable.print();
        scanner.close();
    }
}

class Employee {
    private String name;

    public boolean equals( Object rhs ) {
        return rhs instanceof Employee && name.equals( ( (Employee)rhs).name );
    }

    public int hashCode() {
        return name.hashCode();
    }

    public Employee( String n ) {
        name = n;
    }

    public String toString() {
        return name;
    }
}

输入

mike
job
hah
wow
sha
bob
haya
llo
leilei
niuniu
didi
!

输出

0[didi]
1[]
2[mike, leilei]
3[]
4[]
5[job, hah]
6[sha]
7[bob, llo]
8[niuniu]
9[wow, haya]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CHOOOU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值