这个是个VO对像
[quote="java"]
package com.lihui.VO;
public class AddListVO {
private int goodId;
private int buyNum;
private int orderId;
public AddListVO() {
super();
// TODO Auto-generated constructor stub
}
public AddListVO(int goodId, int buyNum, int orderId) {
super();
this.goodId = goodId;
this.buyNum = buyNum;
this.orderId = orderId;
}
public int getGoodId() {
return goodId;
}
public void setGoodId(int goodId) {
this.goodId = goodId;
}
public int getBuyNum() {
return buyNum;
}
public void setBuyNum(int buyNum) {
this.buyNum = buyNum;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
}
[/quote]
然后是主方法:
[quote="java"]
public class map {
private AddListVO addListVO;
public static void main(String args[])
{
AddListVO listVO=new AddListVO(1, 2, 3);
HashMap hashmap = new HashMap();
hashmap.put("Item0", listVO);
hashmap.put("Item1", listVO);
hashmap.put("Item2", listVO);
hashmap.put("Item3", listVO);
Set set = hashmap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry mapentry = (Map.Entry) iterator.next();
System.out.println(mapentry.getKey() + "/" + mapentry.getValue());
}
}
public AddListVO getAddListVO() {
return addListVO;
}
public void setAddListVO(AddListVO addListVO) {
this.addListVO = addListVO;
}
}
[/quote]
我得到的是
Item1/com.lihui.VO.AddListVO@c17164
Item2/com.lihui.VO.AddListVO@c17164
Item0/com.lihui.VO.AddListVO@c17164
Item3/com.lihui.VO.AddListVO@c17164
我想得到这个对象里的元素的值 应该咋办?
[color=blue][b]mapentry.getKey() 获得的是 item1等key
而:
mapentry.getValue()); 获得的是你放入的listVO这个对象。
当你用System.out.println();输出对象时,输出的是该对象的toString方法的返回值。
而你的AddListVO没有复写toString 方法,所以输出的就是com.lihui.VO.AddListVO@c17164 。[/b][/color]
[b]你可以这样:[/b]
code="java"mapentry.getValue()).getGoodId();[/code]
[b]也可以在AddListVO中Override复写 toString 方法[/b]
BeanUtils类在apache的commons-beanutils.jar包中,一般项目都会使用。
[code="java"]
public String toString()
{
try{
return BeanUtils.describe(this).toString();
}catch(Exception ex){
return "";
}
}
[/code]