首页 > 编程语言 > 详细

HashMap存储数据赋值javabean简单示例

时间:2015-07-17 13:50:29      阅读:333      评论:0      收藏:0      [点我收藏+]

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+"]";
    }
    
    
    
}

HashMap存储数据赋值javabean简单示例

原文:http://www.cnblogs.com/assassin666/p/4654108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!