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();
}
}
}
我创建了一个包含超市产品信息的 HashMap 。但是,我无法删除我作为产品名称输入的产品。显然,我不能使用 remove 方法。问题是什么,我该如何解决?
最佳答案
在您的情况下,您需要使用 Iterator
来迭代 Map
并删除产品。 (因为 Map
由 Integer
的 ArrayList
索引)
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
中删除产品:
hMap.remove(productName);
关于java - 无法从 HashMap 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34480736/