package com.shb.web; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @Describe HashMap存储数据,赋值javabean.
* @author xiaoshi
* @Date 2015-7-17
*/
public class TUsers {
public static void setAttribute(Person person,String key,String value){
if(key.equals("name")){
person.setName(value);
}
if(key.equals("sex")){
person.setSex(value);
}
if(key.equals("age")){
person.setAge(Integer.parseInt(value));
}
if(key.equals("weight")){
person.setWeight(Double.parseDouble(value));
} } public static void main(String[] args) {
Person person = new Person();
Map<String, String > map = new HashMap<String, String>();
map.put("name","zhangsan");
map.put("sex","male");
map.put("age","123");
map.put("weight", "456");
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
TUsers.setAttribute(person, key, value); }
System.out.println(person.toString());
}
}
class Person{
private String name;
private String sex;
private Integer age;
private double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String toString(){ return "[name="+name+"##"+"sex="+sex+"##"+"age="+age+"##"+"weight="+weight+"]";
} }