lzxcl
lzxcl

Reputation: 103

Trouble deleting an item from a hash map

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class mainClass
{
    static Scanner keyboard = new Scanner(System.in);
    static HashMap <ArrayList <Integer>, String> hMap;

    public static void createAHashMap()
    {
        System.out.print("Express the initial capacity: ");
        int initialCapacity = keyboard.nextInt();
        System.out.print("Express the load factor: ");
        float loadFactor = keyboard.nextFloat();
        hMap = new HashMap <ArrayList <Integer>, String> (initialCapacity, loadFactor);      
    }

    public static void insertProduct()
    {   
        ArrayList <Integer> informations = new ArrayList <Integer> ();
        System.out.print("\nEnter product's barcode number: ");
        int barcodeNumber = keyboard.nextInt();
        while (barcodeNumber < 0)
        {
            System.out.println("Wrong barcode number entry. Please try again: ");
        }
        informations.add(barcodeNumber);
        System.out.print("Enter product's amount: ");
        int productAmount = keyboard.nextInt();
        informations.add(productAmount);
        System.out.print("Enter product's price: ");
        int productPrice = keyboard.nextInt(); 
        informations.add(productPrice);
        System.out.print("Enter product's name: ");
        String productName = keyboard.next();   
        hMap.put(informations, productName);
    }   

    public static void displayProducts()
    {
        System.out.println("\nBarcode Number\tProduct Amount\tProduct Price\tProduct Name");
        for (Entry <ArrayList <Integer>, String> entry: hMap.entrySet()) 
        {           
            for(int itemNo: entry.getKey())
            {
                System.out.print(itemNo + "\t\t");
            }
            System.out.print(entry.getValue() + "\t\t");
            System.out.println();
        }
    }

    public static void removeAProduct()
    {
        System.out.print("Express product name that you want to remove: ");
        String productName = keyboard.next();
        hMap.remove(productName);
    }

  /*  public static void updateAProduct()
    {
        System.out.print("Express product name that you want to update: ");
        String productName = keyboard.next();
        hMap.remove(productName);
        System.out.println("Enter again the product informations");
        insertProduct();
    }*/

    public static void main(String args[]) 
    {
        createAHashMap();

        System.out.print("\nEnter '1' to add a product\n");
        System.out.print("Enter '2' to display products\n");
        System.out.print("Enter '3' to delete a product\n");
        System.out.print("Enter '4' to update a product\n");
        System.out.print("Enter your choice: ");

        int entry = keyboard.nextInt();     
        while (entry != -99)
        {
            if (entry == 1)
            {
                insertProduct();
            }
            if (entry == 2)
            {
                displayProducts();
            }
            if (entry == 3 && hMap.size() != 0)
            {
                removeAProduct();
            }
            if (entry == 4 && hMap.size() != 0)
            {
                //updateAProduct();
            }
            System.out.print("\nExpress your new choice (Exit: -99): ");
            entry = keyboard.nextInt();
        }       
    }
}

I've created a hash map which contains product informations in a supermarket. However, I can't remove the products what I entered as product name. Clearly, I can't use remove method. What is the problem, how can I correct it?

Upvotes: 1

Views: 63

Answers (2)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

In your case, you need to use an Iterator to iterate through the Map & delete the product. (Since the Map is indexed by an ArrayList of Integers)

System.out.print("Express product name that you want to remove: ");

String productName = keyboard.next();
ArrayList <Integer> rKey = null;

for (Entry <ArrayList <Integer>, String> entry: hMap.entrySet())  {
    if (productName.equals(entry.getValue())) {
        rKey = entry.getKey();
        break;
    }
}

if (rKey != null) {
    hMap.remove(rKey);
}

On the other hand, I think it's best if you change

static HashMap <ArrayList <Integer>, String> hMap;

...
hMap.put(informations, productName);

to

static HashMap <String, ArrayList <Integer>> hMap;

...
hMap.put(productName, informations);

This way, you could easily remove products from the Map by their names:

hMap.remove(productName);

Upvotes: 4

Aaditya Gavandalkar
Aaditya Gavandalkar

Reputation: 824

Firstly,

Ideally it is expected that the HashMap key should be something that uniquely identifies value. So in your case String should be the key and ArrayList <Integer>

Making it look like: static HashMap <String, ArrayList <Integer>> hMap;

Regarding the issue with remove operation, remove() expects the argument as key i.e productName in your case as remove() is defined as:

public V remove(Object key) {
    Entry<K,V> e = removeEntryForKey(key);
    return (e == null ? null : e.value);
}

So once you make this change by interchaning key and value, your code should work just fine.

Upvotes: 1

Related Questions