Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Hash;
- /**
- *
- * good: space OK!
- * bad: collision ...
- *
- */
- public class Hash_001_HashTable {
- static int space_limit = 10;
- private Integer[] nums;
- private int[] hash_table;
- public Hash_001_HashTable(Integer[] nums) {
- this.nums = nums;
- }
- public void bulid_hash_table() {
- this.hash_table = new int[space_limit];
- for (int i = 0; i < nums.length; i++) {
- add(nums[i]);
- }
- }
- private void add(Integer num) {
- int index = hash(num);
- int value = num;
- this.hash_table[index] = value;
- }
- private int hash(int num){
- return num % 10;
- }
- /** find by index **/
- private void hash_search(int target_num) {
- int index = hash(target_num);
- if (this.hash_table[index] == target_num) {
- System.out.println("found(hash): " + target_num);
- }
- }
- private static Integer[] get_raw_ary() {
- Integer[] nums = {78,6,80,73,27,61,35,44,29,2};
- return nums;
- }
- public static void main(String[] args) {
- // hash search
- int target_num = 29;
- Integer[] nums = get_raw_ary();
- // build hash table
- Hash_001_HashTable hash_table = new Hash_001_HashTable(nums);
- hash_table.bulid_hash_table();
- // search it!
- hash_table.hash_search(target_num);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement