0

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());           
        }   
    }
}
7
  • its an array.....entry.getValue gives reference of that array not array elements Commented Nov 24, 2016 at 9:05
  • can you give a sample entry and how do you expect it to be ? Commented Nov 24, 2016 at 9:07
  • You would probably get easier of when mapping the different keys to the same map, or does this give you problems? Or is it really a HashMap<String,Integer> you are looking for? Commented Nov 24, 2016 at 9:45
  • i am getting Adress of these values so where i should make change?? @Prasanna Kumar Commented Nov 24, 2016 at 9:52
  • yes i am looking for HashMap<String[],Integer[]> ....i want like each corresponding value of string array will be the key for each value of integer array @patrik. Commented Nov 24, 2016 at 10:01

5 Answers 5

1

Use:

System.out.println("key: " + entry.getKey() + " value: " + Arrays.toString(entry.getValue()));  
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.