dhananjay dhananjay - 12 days ago 6
Java Question

HashMap with String Array and integer array value doesnt print the output

i want the value and key of the array to be printed using Hashmap...but i am getting Adress using this.

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

import java.util.Set;
import au.com.bytecode.opencsv.CSVReader;
import java.util.Arrays;
import java.util.Collection;

public class ArrayHash

{
public static void main(String args[]) throws IOException
{
int[] WorkingDay=new int[13];
int i=0;
String[] Name=new String[13];
String file="C:\\Users\\Dhananjay Kumar\\Empdetail\\Detail.csv";
HashMap<String[],int[]> hashfunc=new HashMap<String[],int[]>();
CSVReader reader=new CSVReader(new FileReader(file));
String[] read;
while((read = reader.readNext()) !=null)
{
WorkingDay[i]=Integer.parseInt(read[2]);
Name[i]=read[0];
i++;
}
hashfunc.put(Name,WorkingDay);
hashfunc.get(Name);
Set<Entry<String[], int[]>> entrySet = hashfunc.entrySet();
for (Entry entry : entrySet)
{
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
}
}

Answer
hashfunc

is holding the array reference as key hence its printing the address.

for (Entry entry : entrySet)
    {
        System.out.println("key: " + entry.getKey() + " value: " + entry.getValue()); 
       String[] names=entry.getKey();
       for(String name:names){
         System.out.println(name);
       }
       int[] workingDays=entry.getValue();
       for(int workingDay:workingDays){
         System.out.println(workingDay);
       } 
    }