如何获取Map值

时间:2013-04-22 18:31:47

标签: java map hashmap

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class clsWarehouse {

public static void main(String[] args){

    class Items {
        private String iItemID;
        private String strItemName;
        private String strItemDescription;
        private String iItemPrice;
        private String strSize;
        private String strSex;


    public Items (String id, String name, String description, String price, String size, String sex){
        iItemID = id;
        strItemName = name;
        strItemDescription = description;
        iItemPrice = price;
        strSize = size;
        strSex = sex;
        }
    }   


Map<Integer, Items> ItemMap = new HashMap<Integer, Items>();
ItemMap.put(1, new Items("3", "test1", "test2", "30", "20", "Male")); 

for (Map.Entry<Integer, Items> entry : ItemMap.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

        Items test = entry.getValue();
        String test2 = test.toString();
        System.out.println(test2);
    }  
}
}

您好,

我想获取存储在地图中的值,但我只得到内存位置,Key = 1,Value = clsWarehouse $ 1Items @ c3c749。有人可以帮我这个。

谢谢!

2 个答案:

答案 0 :(得分:0)

覆盖Items类中的toString()方法,以便在调用它时控制输出。 Object中的默认实现会产生您正在观察的行为(来自链接的Javadoc)

getClass().getName() + '@' + Integer.toHexString(hashCode())

hashCode()方法,默认为uses the memory address

答案 1 :(得分:0)

Items

中添加所需的getter

Ex(对于ItemID):

public String getItemID()
{
    return iItemID;
}

并在for loop

Items test = entry.getValue();
System.out.println(test.getItemID());