为什么我的哈希图不返回字数统计?

时间:2018-11-27 11:09:03

标签: get hashmap keyvaluepair containskey linear-probing

我正在创建一个进行线性探测以查找密钥索引的哈希图。如果键已在索引中,则我想增加其值,而不是将其添加到新索引中。

例如,如果我得到字符串“五,五,五”的字数统计,我的输出将是5 1、5 1、5 1而不是53。

我认为它必须是我的containsKey方法,该方法使用get方法检查我的钥匙是否已经在地图中。下面是我的Hashmap.java类。

import java.util.Hashtable;
import java.util.ArrayList;
import java.lang.Object;

public class Hashmap<K,V> implements MapSet<K,V>
{
    private Object hashMap[]; //hash table
    private int capacity; // capacity == table.length
    private int collisions; // number of collisions
    private int numItems; // number of hash table entries


    public Hashmap(int arrayCapacity){
        capacity = arrayCapacity;
        hashMap = new Object[capacity];
        collisions = 0;
        numItems = 0;

    }

    //Returns true if the map contains a key-value pair with the given key
    @SuppressWarnings({"unchecked"})
    public boolean containsKey( K key ){
        return get(key) != null;    

    }


    @SuppressWarnings({"unchecked"})
    public V put(K key, V value){

        int hash = Math.abs(key.hashCode());
        int index = hash% hashMap.length; //getting a new index for the key-value-pair
        KeyValuePair<K,V> pair = (KeyValuePair<K,V>) hashMap[index];

        while(pair != null && !pair.getKey().equals(key)){
            index = (index+1)% hashMap.length;
            pair = (KeyValuePair<K,V>)hashMap[index];
            collisions++;           
        }

        if (pair == null){
            //a null spot has been found, the key value pair will be added here.
            KeyValuePair<K,V> temp = new KeyValuePair<K,V>(key,value);
            hashMap[index] = temp;
            numItems++;
            if (numItems > hashMap.length / 2) {
                ensureCapacity();
            }
        return value;
        }
        else {
            //the key is the same as one already in the hashmap.
            //sets the value of the new key to the old key.
            V oldValue = pair.getValue();
            pair.setValue(value);
            return oldValue;
        }
    }

    @SuppressWarnings({"unchecked"})    
     public V get(K key){
        int hash = Math.abs(key.hashCode());
        int index = hash% hashMap.length;
        KeyValuePair<K,V> pair = (KeyValuePair<K,V>) hashMap[index];

        if(pair == null){
            return null;
        }

        else if(pair.getKey() == key){
            return pair.getValue();
        }
        else{
            index = (index + 1)% hashMap.length;
            int progress = 0;
                while(hashMap[index] != null){
                    progress++;
                    KeyValuePair<K,V> item = (KeyValuePair<K,V>) hashMap[index];
                    if(item.getKey().equals(key)) 
                        return item.getValue();
                    if (progress == hashMap.length) 
                        break;
                }       
            return null;
        }

     }

1 个答案:

答案 0 :(得分:0)

请参考此示例

import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
   public static void main(String args[])
   {
      {
      String s = "Any text for word word count";
      String[] splitted = s.split(" ");
      Map<String, Integer> hm = new HashMap<String, Integer>();
      int x;

      for (int i=0; i<splitted.length ; i++) {
         if (hm.containsKey(splitter[i])) {
            int cont = hm.get(splitter[i]);
            hm.put(splitter[i], cont + 1)
         } else {
            hm.put(splitted[i], 1);
         }
      }
   }
}