A simple example of HashMap storing data assignment javabean, hashmapan

Source: Internet
Author: User

A simple example of HashMap storing data assignment javabean, hashmapan

Package com. shb. web;

Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Map;
/**
* @ Describe HashMap stores data and assigns a value to 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 +"] ";
}



}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.