import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class mainClass
{
static Scanner keyboard = new Scanner(System.in);
static HashMap <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 <Integer[], String> (initialCapacity, loadFactor);
}
public static void insertProductInformations()
{
Integer values[] = new Integer[3];
System.out.print("\nEnter product's barcode number: ");
values[0] = keyboard.nextInt();
System.out.print("Enter product's name: ");
String productName = keyboard.next();
System.out.print("Enter product's amount: ");
values[1] = keyboard.nextInt();
System.out.print("Enter product's price: ");
values[2] = keyboard.nextInt();
hMap.put(values, productName);
}
public static void displayList()
{
Set set = hMap.entrySet();
Iterator iterator = set.iterator();
System.out.println("\nBarcode Number\tProduct Amount\tProduct Price\tProduct Name");
while (iterator.hasNext())
{
Map.Entry mEntry = (Map.Entry) iterator.next();
System.out.print(mEntry.getKey() + "\t\t" + mEntry.getValue() + "\n");
}
}
public static void main(String[] args)
{
createAHashMap();
System.out.print("\nEnter to add product '1'\n");
System.out.print("Enter to display products '2'\n");
System.out.print("Enter your choice: ");
int entry = keyboard.nextInt();
while (entry != -99)
{
if (entry == 1)
{
insertProductInformations();
}
if (entry == 2)
{
displayList();
}
System.out.print("\nExpress your new choice (Exit: -99): ");
entry = keyboard.nextInt();
}
}
}
Hi!
I've created a hash map which contains product informations in a supermarket. However, I can't display my key values (which is an array) correctly. It shows me irrelevant things except product's name. How can I correct this?
static HashMap <Integer[], String> hMap;
An array is not suitable as a key in the HashMap
, since two different array instances having the exact same elements will be considered to be different keys. This behavior results from the fact that arrays don't override Object
's equals
method, so two arrays arr1
and arr2
are equal only if arr1==arr2
.
The printing of the keys is not your main problem. That can be solved easily by using Arrays.toString
to print the array key.
I suggest you change your HashMap
to HashMap<List<Integer>, String>
.
I agree with Eran's answer that you should not choose your key as the Integer array but I think your problem of displaying will not be completely solved with Arrays.toString as it will give the array in specified format and not in the one you need. You need to explicitly format your data as per your requirement. An example modification of display from above example could be :-
public static void displayList()
{
Set set = hMap.entrySet();
Iterator iterator = set.iterator();
System.out.println("\nBarcode Number\tProduct Amount\tProduct Price\tProduct Name");
while (iterator.hasNext())
{
Map.Entry mEntry = (Map.Entry) iterator.next();
Integer[] values = (Integer[]) mEntry.getKey();
System.out.print(values[0] + "\t" + values[1] + "\t" + values[2] + "\t" + mEntry.getValue() + "\n");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With