无法从哈希映射中删除项目

时间:2015-12-27 13:13:36

标签: java hashmap

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();
        }       
    }
}

我创建了一个哈希映射,其中包含超市中的产品信息。但是,我无法删除我输入的产品名称。显然,我不能使用删除方法。有什么问题,我该如何纠正?

2 个答案:

答案 0 :(得分:4)

在您的情况下,您需要使用Iterator迭代通过Map&amp;删除产品。 (由于MapArrayList的{​​{1}}索引了

Integer

另一方面,我认为如果你改变

是最好的
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);
}

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

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

这样,您可以通过名称轻松地从static HashMap <String, ArrayList <Integer>> hMap; ... hMap.put(productName, informations); 中删除产品:

Map

答案 1 :(得分:1)

首先,

理想情况下,HashMap key应该是唯一标识value的东西。因此,在您的情况下,String应该是关键字ArrayList <Integer>

看起来像: static HashMap <String, ArrayList <Integer>> hMap;

关于删除操作的问题,remove()期望参数为关键,即在您的情况下为remove()的productName定义为:

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

因此,一旦通过交换密钥和值进行此更改,您的代码应该可以正常工作。