Java散列函数知识点(含面试大厂题和源码)

散列函数(Hash Function),也称为哈希函数或哈希算法,是一种将任意大小的数据转换成固定大小的值的函数。这个转换后的值通常被称为哈希值或哈希码。散列函数在计算机科学中有着广泛的应用,尤其是在数据检索、加密和数据结构(如哈希表)等领域。

散列函数的特点:

  1. 确定性:对于相同的输入,散列函数总是产生相同的输出。
  2. 快速计算:散列函数应该能够快速计算出输入数据的哈希值。
  3. 抗碰撞性:找到两个不同的输入,使它们产生相同的输出(即碰撞),应该是非常困难的。
  4. 雪崩效应:散列函数的输出应该看起来是随机的,即使输入数据只有微小的差别,产生的哈希值也应该有显著的不同。

散列函数的应用:

  1. 哈希表:通过散列函数将键映射到哈希表的数组索引,实现快速的数据存取。
  2. 加密和安全:散列函数用于加密算法,确保数据的安全性和完整性。
  3. 数据校验:散列函数可以用于验证数据的完整性,如文件校验、数字签名等。
  4. 负载均衡:在分布式系统中,散列函数可以帮助均匀地分配负载。

常见的散列函数:

  1. MD5:一种广泛使用的加密散列函数,产生128位的哈希值。
  2. SHA-1:安全哈希算法(Secure Hash Algorithm)的一种,产生160位的哈希值。
  3. SHA-256:SHA-2家族中的一员,产生256位的哈希值,比SHA-1更安全。
  4. SHA-3:最新的安全哈希算法,设计上比SHA-2更抵御攻击。

简单散列函数的Java实现:

public class SimpleHashFunction {
    public static int hash(String key, int arraySize) {
        int hashValue = 0;
        for (int i = 0; i < key.length(); i++) {
            hashValue = (hashValue << 5) - hashValue + key.charAt(i);
            hashValue |= 0; // Convert to 32bit positive value
        }
        return Math.abs(hashValue) % arraySize;
    }

    public static void main(String[] args) {
        String key = "Kimi";
        int arraySize = 10;
        int hashValue = hash(key, arraySize);
        System.out.println("Hash value for '" + key + "': " + hashValue);
    }
}

在面试中,了解散列函数的原理和应用是非常重要的。面试官可能会询问你关于散列函数的设计原则、碰撞处理方法、以及不同散列函数的优缺点。希望这些知识点和示例代码能够帮助你更好地准备面试!

题目 1:设计哈希表

描述
设计一个哈希表,包括初始化、插入、删除和查找操作,并确保哈希表在处理大量数据时具有高效的性能。

示例

哈希表的初始大小为 10,支持插入 key-value 对、删除指定的 key、查找指定的 key,以及返回哈希表的大小。

Java 源码

public class HashTable {
    static class Entry {
        int key;
        String value;
        Entry next;

        Entry(int key, String value) {
            this.key = key;
            this.value = value;
            this.next = null;
        }
    }

    private static final int INIT_CAPACITY = 10;
    private Entry[] buckets;
    private int size;

    public HashTable() {
        buckets = new Entry[INIT_CAPACITY];
        size = 0;
    }

    public void put(int key, String value) {
        int index = hash(key);
        Entry entry = new Entry(key, value);
        if (buckets[index] == null) {
            buckets[index] = entry;
        } else {
            buckets[index] = addEntry(buckets[index], entry);
        }
        size++;
    }

    public void remove(int key) {
        int index = hash(key);
        if (buckets[index] != null) {
            buckets[index] = removeEntry(buckets[index], key);
        }
    }

    public String get(int key) {
        int index = hash(key);
        Entry entry = buckets[index];
        while (entry != null) {
            if (entry.key == key) {
                return entry.value;
            }
            entry = entry.next;
        }
        return null;
    }

    public int getSize() {
        return size;
    }

    private int hash(int key) {
        return Math.abs(key % buckets.length);
    }

    private Entry addEntry(Entry head, Entry entry) {
        if (head == null) {
            return entry;
        }
        Entry current = head;
        while (current.next != null) {
            if (current.next.key == entry.key) {
                current.next.value = entry.value;
                return head;
            }
            current = current.next;
        }
        current.next = entry;
        return head;
    }

    private Entry removeEntry(Entry head, int key) {
        if (head == null) {
            return null;
        }
        if (head.key == key) {
            Entry result = head.next;
            head.next = null;
            return result;
        }
        Entry current = head;
        while (current.next != null) {
            if (current.next.key == key) {
                Entry toDelete = current.next;
                current.next = current.next.next;
                return toDelete;
            }
            current = current.next;
        }
        return head;
    }

    public static void main(String[] args) {
        HashTable table = new HashTable();
        table.put(1, "one");
        table.put(2, "two");
        table.put(3, "three");
        System.out.println("Hash table size: " + table.getSize());
        System.out.println("Value for key 2: " + table.get(2));
        table.remove(2);
        System.out.println("Value for key 2 after removal: " + table.get(2));
    }
}

题目 2:解决散列冲突

描述
实现一个解决散列冲突的链地址法(Chaining)哈希表,并提供插入和查找操作。

示例

插入 key-value 对 (1, "a") 和 (1, "b"),查找 key 为 1 的所有值。

Java 源码

public class ChainingHashTable {
    private static class Entry {
        int key;
        String value;
        Entry next;

        Entry(int key, String value) {
            this.key = key;
            this.value = value;
            this.next = null;
        }
    }

    private Entry[] table;
    private int size;

    public ChainingHashTable(int capacity) {
        table = new Entry[capacity];
        size = 0;
    }

    public void insert(int key, String value) {
        int index = hash(key);
        Entry newEntry = new Entry(key, value);
        if (table[index] == null) {
            table[index] = newEntry;
        } else {
            Entry current = table[index];
            while (current.next != null) {
                if (current.key == key) {
                    current.value = value; // Update existing key
                    return;
                }
                current = current.next;
            }
            current.next = newEntry;
        }
        size++;
    }

    public String search(int key) {
        int index = hash(key);
        Entry entry = table[index];
        while (entry != null) {
            if (entry.key == key) {
                return entry.value;
            }
            entry = entry.next;
        }
        return null;
    }

    private int hash(int key) {
        return Math.abs(key % table.length);
    }

    public static void main(String[] args) {
        ChainingHashTable table = new ChainingHashTable(10);
        table.insert(1, "a");
        table.insert(1, "b");
        System.out.println("Value for key 1: " + table.search(1));
    }
}

题目 3:计算字符串的哈希值

描述
实现一个简单的散列函数,计算字符串的哈希值,并处理散列冲突。

示例

输入字符串 "hello",输出其哈希值。

Java 源码

public class SimpleStringHash {
    public int hash(String s) {
        int hashValue = 0;
        int prime = 31; // 使用一个质数作为乘数
        for (int i = 0; i < s.length(); i++) {
            hashValue = (hashValue * prime) + s.charAt(i);
        }
        return Math.abs(hashValue) % 100; // 取绝对值并限制哈希值的范围
    }

    public static void main(String[] args) {
        SimpleStringHash hash = new SimpleStringHash();
        int hashValue = hash.hash("hello");
        System.out.println("Hash value for 'hello': " + hashValue);
    }
}

这些题目和源码展示了散列函数在不同场景下的应用。在面试中,能够根据问题的特点选择合适的算法并实现其解决方案是非常重要的。希望这些示例能够帮助你更好地准备面试!

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值