[英]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: 首先,您必须将一行longValues
矩阵添加到地图中,而不是整个矩阵:
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
然后,迭代时将值提取为Object[]
而不是String
,并键入为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: 您的代码有一些修复/改进:
screen_dataz
was always adding the same object 向screen_dataz
添加元素的循环总是添加相同的对象 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()
存储在地图中的值是一个数组,这意味着它不会像您期望的那样通过简单调用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 您可能想要更新for循环
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
循环以填充到这样的screen_dataz
。
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
. 地图当前正在存储Map<Long,Object[]>
但是Object[][]
类型的Object[][]
被添加到Map
。 I assume you want to enter each Object[]
as a separate Entry
in the Map
. 我假设您想要将每个Object[]
作为Map
的单独Entry
。
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. 要看的第二部分是对Map
条目的迭代。 Instead of using the Iterator
use a for..each
loop. 而不是使用Iterator
使用for..each
循环。
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]);
我想使用screen_dataz.put(k,longValues[k]);
in a loop will help you. 在一个循环中将帮助你。 You could also use an Iterator for this. 你也可以使用Iterator。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.