UserAtHome
UserAtHome

Reputation: 3

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

Upvotes: 0

Views: 2127

Answers (5)

patrik
patrik

Reputation: 4558

It appears as if you are really needing an object of type HashMap<String,Integer>. Also, there is a written coding convention in Java which states that the beginning { should be at the same line as the declaration of the statement. I am not too found of it either, but since this is convention I follow it.

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

Upvotes: 0

Prasanna Kumar H A
Prasanna Kumar H A

Reputation: 3431

entry.getValue() gives reference of that array not array elements

Use Arrays.toString((int[]) entry.getValue()) so that it will print values. or use a for loop to array length and print each value

     for(Entry entry : entrySet) {
        int[] arrInt = (int[]) entry.getValue();
        String[] arrString = (String[]) entry.getKey();
        for(int j=0; j<arrInt.length; j++){
            System.out.println(arrInt[j]);
        }
     }

Upvotes: 0

Farwatch
Farwatch

Reputation: 46

You are attempting to print the entire array for the value of each entry in entrySet.

The default toString() method of an Array just returns the address of the object. Therefore you need to use another method, such asArrays.toString(entry.getValue()) , or create your own method to print the array in the format you require.

Upvotes: 0

snofty
snofty

Reputation: 70

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

Upvotes: 0

C. L.
C. L.

Reputation: 571

Use:

System.out.println("key: " + entry.getKey() + " value: " + Arrays.toString(entry.getValue()));  

Upvotes: 1

Related Questions