0

I have a class MapEntry to implement Hashmap. I need to implement a method where I can get key from value and method delete. I know that HashMap does not implement such method as getValue but my prof asked to do this. I'm new to programming how it's a little bit hard for me right now. I'll appreciate any help.

   public class MapEntry<K,V> {

   MapEntry<K,V> next;

    K key;

    V value;

    public MapEntry(K key, V value) {
         this.setKey(key);
         this.setValue(value);
    }
    public void setKey( K key){
        this.key=key;
    }
    public void setValue(V value){
        this.value=value;
    }
    public K getKey(){
        return key;
    }
    public V getValue(){
        return value;
    }
    public void setNext(MapEntry<K,V> next) {
        this.next = next;
    }

    public MapEntry<K, V> getNext() {
        return next;
    }
}
public class HashMap{

    private int DEFAULT_CAPACITY = 10;
    private MapEntry<String,Double>[] Hash;
    private int size;

    public HashMap() {
        Hash = new MapEntry[DEFAULT_CAPACITY];
    }

    public boolean isEmpty(){
        if(size!= 0){
            return false;
        }
        else{
            return true;
        }
    }



    public int getHashCode(String key){
        int bucketIndex = key.hashCode()%Hash.length;
        return bucketIndex;
    }

    public Double get(String key){
        if(key == null){
            try {
                throw new IllegalAccessException("Null key");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }


        MapEntry<String,Double> entry = Hash[getHashCode(key)];




        while (entry != null && !key.equals(entry.getKey()))
            entry = entry.getNext();
        if(entry != null)
            return entry.getValue();
        else
            return null;

   }
    }
    public void put(String key, double value){
        int keyBucket =hash(key);
        MapEntry<String,Double> temp = Hash[keyBucket];
        while (temp !=null){
            if((temp.key == null && key == null)
                || (temp.key != null && temp.key.equals(key))){
                temp.value = value;
                return;
            }
            temp = temp.next;
        }
        Hash[keyBucket] = new MapEntry<String, Double>(key,value);
        size++;
    }
    public void delete (String key) throws IllegalAccessException {
        if(key == null){
            throw new IllegalAccessException("Null key");
        }
    }


    private int hash(String key){
        if(key == null){
            return 0;
        }else {
            return Math.abs(key.hashCode()% this.Hash.length);
        }
    }

   public static void main(String[] args) {
 HashMap hashMap = new HashMap();
 hashMap.put("value", 2.2);
 hashMap.put("bob", 2.3);
     System.out.println(hashMap.get("value"));
     System.out.println(hashMap.get("bob"));
     System.out.println(hashMap.size);
     System.out.println(hashMap.getHashCode("value"));
     System.out.println(hashMap.getHashCode("bob"));
     System.out.println(hashMap.isEmpty());






    }
}
sprinter
  • 27,148
  • 6
  • 47
  • 78
  • HashMap has a function to retrieve value based on key, Check hashmap.get(key) and hashmap.getValues() https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object- – www.hybriscx.com Nov 01 '19 at 03:58

1 Answers1

1

I think the basic algo will be :

  • Iterate through all the values one by one
  • If your value matches the desired result, retrieve the key from that entry.
  • To delete that entry, simply remove that entry

Note : Doesn't work properly if you have multiple entries of same value.

ASK
  • 1,136
  • 1
  • 10
  • 14