简体   繁体   中英

How to retrieve key & values from hashmap

I am trying to insert the values to hash map through object, and i want to check if the values are inserted in to hash map. so i am using this code but in runtime i am not able to get any output.

How to resolve this?

Code:

import java.util.*;
import java.io.*;
import java.lang.*;

public class TaskList
{   

    private static HashMap<Integer, Object[]> dataz = new HashMap<Integer,Object[]>();
    private static  HashMap<Integer, Object[]> screen_dataz = new HashMap<Integer,Object[]>();
    public final static Object[][] longValues = {{"10", "kstc-proc", "10.10.10.10.10.","5","O"},{"11", "proc-lvk1", "12.1.2.","4","O"},{"13", "trng-lvk1", "4.6.1.","3","O"}};
    private static String sl,pid,tid,mval,status;

    public static void main(String args[])
    {
        addTask();
    }

    public static void addTask()
    {  
        for (int k=0; k<longValues.length; k++)
        {
        screen_dataz.put(k,longValues);
        }
        Set mapSet = (Set) screen_dataz.entrySet();
        Iterator mapIterator = mapSet.iterator();
        while (mapIterator.hasNext()) 
        {
        Map.Entry mapEntry = (Map.Entry) mapIterator.next();
        String keyValue = (String) mapEntry.getKey();
        String value = (String) mapEntry.getValue();
        System.out.println(value);
        }
    }
}

First, you must add a row of the longValues matrix to the map, and not the whole matrix:

 for (int k=0; k<longValues.length; k++)
 {
     screen_dataz.put(k,longValues[k]);
 }

Then, while iterating extract the value as Object[] and not String , and key as Integer

 while (mapIterator.hasNext()) 
 {
     Map.Entry mapEntry = (Map.Entry) mapIterator.next();
     Integer keyValue = (Integer) mapEntry.getKey();
     Object[] value = (Object[]) mapEntry.getValue();
     //iterate over the array and print each value
     for (int i=0; i<value.length; i++) {
         System.out.print(value[i] + " ");
     }
     System.out.println();
 }

Your code with a few fixes/improvements:

  • do not use casting when using generics
  • the loop adding elements to screen_dataz was always adding the same object
  • the value stored in the map is an array which means it will not be printed as you expect with a simple call to toString()

     public class TaskList { private static HashMap<Integer, String[]> dataz = new HashMap<Integer, String[]>(); private static HashMap<Integer, String[]> screen_dataz = new HashMap<Integer, String[]>(); public final static String[][] longValues = { { "10", "kstc-proc", "10.10.10.10.10.", "5", "O" }, { "11", "proc-lvk1", "12.1.2.", "4", "O" }, { "13", "trng-lvk1", "4.6.1.", "3", "O" } }; private static String sl, pid, tid, mval, status; public static void main(String args[]) { addTask(); } public static void addTask() { for (int k = 0; k < longValues.length; k++) { screen_dataz.put(k, longValues[k]); } Set<Entry<Integer, String[]>> mapSet = screen_dataz.entrySet(); Iterator<Entry<Integer, String[]>> mapIterator = mapSet.iterator(); while (mapIterator.hasNext()) { Entry<Integer, String[]> mapEntry = mapIterator.next(); Integer keyValue = mapEntry.getKey(); String[] value = mapEntry.getValue(); System.out.println(Arrays.toString(value)); } } } 

One correction in your code:

You may want to update your for loop

as

 for (int k=0; k<longValues.length; k++)
    {
    screen_dataz.put(k,longValues[k]);
    }

First, change your for loop to populate to screen_dataz like this.

for (int k=0; k<longValues.length; k++)
{
    screen_dataz.put(k,longValues[k]);
}

Next, make the below change:-

String keyValue = mapEntry.getKey().toString();
String value = Arrays.asList((Object[])mapEntry.getValue()).toString();

This will print your value properly.

Quite a few things to comment about this code.

First, the generic arguments of the parameterized type are incorrect. The map is currently storing Map<Long,Object[]> however objects of type Object[][] are added to the Map . I assume you want to enter each Object[] as a separate Entry in the Map .

    for (int k=0; k<longValues.length; k++)
    {
    screen_dataz.put(k,longValues[k]);
    }

The second piece to look at is the iteration over the Map entries. Instead of using the Iterator use a for..each loop.

    for(Entry<Integer,Object[]> entry: screen_dataz.entrySet()){
       //repetitive task
    }

Final Output

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TaskList
{   

    private static HashMap<Integer, Object[]> dataz = new HashMap<Integer,Object[]>();
    private static  HashMap<Integer, Object[]> screen_dataz = new HashMap<Integer,Object[]>();
    public final static Object[][] longValues = {{"10", "kstc-proc", "10.10.10.10.10.","5","O"},{"11", "proc-lvk1", "12.1.2.","4","O"},{"13", "trng-lvk1", "4.6.1.","3","O"}};
    private static String sl,pid,tid,mval,status;

    public static void main(String args[])
    {
        addTask();
    }

    public static void addTask()
    {  
        for (int k=0; k<longValues.length; k++)
        {
        screen_dataz.put(k,longValues[k]);
        }

        for(Entry<Integer,Object[]> entry: screen_dataz.entrySet()){
            System.out.println(entry.getKey());
            for(Object obj: entry.getValue()){
                    System.out.println(obj.toString());
            }
        }
    }

}

I think using screen_dataz.put(k,longValues[k]); in a loop will help you. You could also use an Iterator for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM