1 package com.shb.web; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 /** 7 * @Describe HashMap存储数据,赋值javabean. 8 * @author xiaoshi 9 * @Date 2015-7-17 10 */ 11 public class TUsers { 12 public static void setAttribute(Person person,String key,String value){ 13 if(key.equals("name")){ 14 person.setName(value); 15 } 16 if(key.equals("sex")){ 17 person.setSex(value); 18 } 19 if(key.equals("age")){ 20 person.setAge(Integer.parseInt(value)); 21 } 22 if(key.equals("weight")){ 23 person.setWeight(Double.parseDouble(value)); 24 } 25 26 27 } 28 29 public static void main(String[] args) { 30 Person person = new Person(); 31 Map<String, String > map = new HashMap<String, String>(); 32 map.put("name","zhangsan"); 33 map.put("sex","male"); 34 map.put("age","123"); 35 map.put("weight", "456"); 36 Iterator<String> it = map.keySet().iterator(); 37 while(it.hasNext()){ 38 String key = it.next(); 39 String value = map.get(key); 40 TUsers.setAttribute(person, key, value); 41 42 43 } 44 System.out.println(person.toString()); 45 } 46 } 47 class Person{ 48 private String name; 49 private String sex; 50 private Integer age; 51 private double weight; 52 public String getName() { 53 return name; 54 } 55 public void setName(String name) { 56 this.name = name; 57 } 58 public String getSex() { 59 return sex; 60 } 61 public void setSex(String sex) { 62 this.sex = sex; 63 } 64 public Integer getAge() { 65 return age; 66 } 67 public void setAge(Integer age) { 68 this.age = age; 69 } 70 public double getWeight() { 71 return weight; 72 } 73 public void setWeight(double weight) { 74 this.weight = weight; 75 } 76 public String toString(){ 77 78 return "[name="+name+"##"+"sex="+sex+"##"+"age="+age+"##"+"weight="+weight+"]"; 79 } 80 81 82 83 }