2024-03-12 14:08:15 +08:00
|
|
|
/**
|
|
|
|
* File: array_hash_map.kt
|
|
|
|
* Created Time: 2024-01-25
|
|
|
|
* Author: curtishd (1023632660@qq.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
package chapter_hashing
|
|
|
|
|
|
|
|
/* 键值对 */
|
|
|
|
class Pair(
|
|
|
|
var key: Int,
|
2024-04-10 18:09:43 +08:00
|
|
|
var _val: String
|
2024-03-12 14:08:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
/* 基于数组实现的哈希表 */
|
|
|
|
class ArrayHashMap {
|
2024-04-08 16:09:34 +08:00
|
|
|
// 初始化数组,包含 100 个桶
|
2024-03-12 14:08:15 +08:00
|
|
|
private val buckets = arrayOfNulls<Pair>(100)
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
fun hashFunc(key: Int): Int {
|
|
|
|
val index = key % 100
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
fun get(key: Int): String? {
|
|
|
|
val index = hashFunc(key)
|
|
|
|
val pair = buckets[index] ?: return null
|
2024-04-10 18:09:43 +08:00
|
|
|
return pair._val
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 添加操作 */
|
2024-04-10 18:09:43 +08:00
|
|
|
fun put(key: Int, _val: String) {
|
|
|
|
val pair = Pair(key, _val)
|
2024-03-12 14:08:15 +08:00
|
|
|
val index = hashFunc(key)
|
|
|
|
buckets[index] = pair
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
fun remove(key: Int) {
|
|
|
|
val index = hashFunc(key)
|
|
|
|
// 置为 null ,代表删除
|
|
|
|
buckets[index] = null
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键值对 */
|
|
|
|
fun pairSet(): MutableList<Pair> {
|
2024-04-08 16:09:34 +08:00
|
|
|
val pairSet = mutableListOf<Pair>()
|
2024-03-12 14:08:15 +08:00
|
|
|
for (pair in buckets) {
|
2024-04-08 16:09:34 +08:00
|
|
|
if (pair != null)
|
|
|
|
pairSet.add(pair)
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
|
|
|
return pairSet
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键 */
|
|
|
|
fun keySet(): MutableList<Int> {
|
2024-04-08 16:09:34 +08:00
|
|
|
val keySet = mutableListOf<Int>()
|
2024-03-12 14:08:15 +08:00
|
|
|
for (pair in buckets) {
|
2024-04-08 16:09:34 +08:00
|
|
|
if (pair != null)
|
|
|
|
keySet.add(pair.key)
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
|
|
|
return keySet
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有值 */
|
|
|
|
fun valueSet(): MutableList<String> {
|
2024-04-08 16:09:34 +08:00
|
|
|
val valueSet = mutableListOf<String>()
|
2024-03-12 14:08:15 +08:00
|
|
|
for (pair in buckets) {
|
2024-04-13 20:09:39 +08:00
|
|
|
if (pair != null)
|
|
|
|
valueSet.add(pair._val)
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
|
|
|
return valueSet
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
fun print() {
|
|
|
|
for (kv in pairSet()) {
|
|
|
|
val key = kv.key
|
2024-04-10 18:09:43 +08:00
|
|
|
val _val = kv._val
|
2024-04-13 20:09:39 +08:00
|
|
|
println("$key -> $_val")
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
fun main() {
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
val map = ArrayHashMap()
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
map.put(12836, "小哈")
|
|
|
|
map.put(15937, "小啰")
|
|
|
|
map.put(16750, "小算")
|
|
|
|
map.put(13276, "小法")
|
|
|
|
map.put(10583, "小鸭")
|
|
|
|
println("\n添加完成后,哈希表为\nKey -> Value")
|
|
|
|
map.print()
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
// 向哈希表中输入键 key ,得到值 value
|
2024-04-08 16:09:34 +08:00
|
|
|
val name = map.get(15937)
|
2024-03-12 14:08:15 +08:00
|
|
|
println("\n输入学号 15937 ,查询到姓名 $name")
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
map.remove(10583)
|
|
|
|
println("\n删除 10583 后,哈希表为\nKey -> Value")
|
|
|
|
map.print()
|
|
|
|
|
|
|
|
/* 遍历哈希表 */
|
2024-04-08 16:09:34 +08:00
|
|
|
println("\n遍历键值对 Key -> Value")
|
2024-03-12 14:08:15 +08:00
|
|
|
for (kv in map.pairSet()) {
|
2024-04-10 18:09:43 +08:00
|
|
|
println("${kv.key} -> ${kv._val}")
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
|
|
|
println("\n单独遍历键 Key")
|
|
|
|
for (key in map.keySet()) {
|
|
|
|
println(key)
|
|
|
|
}
|
|
|
|
println("\n单独遍历值 Value")
|
2024-04-10 18:09:43 +08:00
|
|
|
for (_val in map.valueSet()) {
|
|
|
|
println(_val)
|
2024-03-12 14:08:15 +08:00
|
|
|
}
|
2024-04-08 16:09:34 +08:00
|
|
|
}
|