Files
hello-algo/en/codes/java/chapter_searching/hashing_search.java
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

52 lines
1.7 KiB
Java

/**
* File: hashing_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
import java.util.*;
public class hashing_search {
/* Hash-based search (array) */
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
// Hash table key: target element; value: index
// Return -1 if the hash table does not contain this key
return map.getOrDefault(target, -1);
}
/* Hash-based search (linked list) */
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// Hash table key: target node value; value: node object
// Return null if the hash table does not contain this key
return map.getOrDefault(target, null);
}
public static void main(String[] args) {
int target = 3;
/* Hash-based search (array) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// Initialize the hash table
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i); // key: element; value: index
}
int index = hashingSearchArray(map, target);
System.out.println("Index of target element 3 = " + index);
/* Hash-based search (linked list) */
ListNode head = ListNode.arrToLinkedList(nums);
// Initialize the hash table
Map<Integer, ListNode> map1 = new HashMap<>();
while (head != null) {
map1.put(head.val, head); // key: node value; value: node
head = head.next;
}
ListNode node = hashingSearchLinkedList(map1, target);
System.out.println("The node object corresponding to target value 3 is " + node);
}
}