Java中Bean对象根据属性名称获取和设置其值和Map的性能

ETL优化时的参考

package com.test;
 
import java.util.HashMap;
import java.util.Map;
 

public class UserBean {
    private String id;
    private String name;
    private int age;
    private String address;
    private String email;
 
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    private int addressHash="address".hashCode();
    private int emailHash="email".hashCode();
    private int nameHash="name".hashCode();
    private int ageHash="age".hashCode();
 
    @SuppressWarnings("unchecked")
    public T getAttr(int attrHash){
        if(attrHash==addressHash){
            return (T) address;
        }else if(attrHash==emailHash){
            return (T)email;
        }else if(attrHash==nameHash){
            return (T)name;
        }else if(attrHash==ageHash){
            return (T)new Integer(age);
        }
        return null;
    }
    public void setAttr(int attrHash,Object value){
        if(attrHash==addressHash){
            this.address=(String) value;
        }else if(attrHash==emailHash){
            this.email=(String) value;
        }else if(attrHash==nameHash){
            this.name=(String) value;
        }else if(attrHash==ageHash){
            this.age=(int) value;
        }
    }
    
    @SuppressWarnings("unchecked")
    public T getAttr3(int attrHash){
        switch (attrHash) {
        case -1147692044:
            return (T)address;
        case 96619420:
            return (T)email;
        case 3373707:
            return (T)name;
        case 96511:
            return (T)new Integer(age);
        default:
            return null;
        }
    }
    public void setAttr3(int attrHash,Object value){
        switch (attrHash) {
        case -1147692044:
            this.address=(String)value;
            break;
        case 96619420:
            this.email=(String)value;
            break;
        case 3373707:
            this.name=(String)value;
            break;
        case 96511:
            this.age=(int)value;
            break;
        default:
            break;
        }
    }
    
    @SuppressWarnings("unchecked")
    public T getAttr(String attr){
        if("address".equals(attr)){
            return (T) address;
        }else if("email".equals(attr)){
            return (T)email;
        }else if("name".equals(attr)){
            return (T)name;
        }else if("age".equals(attr)){
            return (T)new Integer(age);
        }
        return null;
    }
    public void setAttr(String attr,Object value){
        if("address".equals(attr)){
            this.address=(String) value;
        }else if("email".equals(attr)){
            this.email=(String) value;
        }else if("name".equals(attr)){
            this.name=(String) value;
        }else if("age".equals(attr)){
            this.age=(int) value;
        }
    }
    
    @SuppressWarnings("unchecked")
    public T getAttr2(String attr){
        switch (attr.length()) {
        case 7:
            return (T) address;
        case 5:
            return (T)email;
        case 4:
            return (T)name;
        case 3:
            return (T)new Integer(age);
        default:
            return null;
        }
    }
    public void setAttr2(String attr,Object value){
        switch (attr.length()) {
        case 7:
            this.address=(String) value;
        case 5:
            this.email=(String)value;
            break;
        case 4:
            this.name=(String)value;
            break;
        case 3:
            this.age=(int)value;
            break;
        default:
            break;
        }
    }
    
 
    @SuppressWarnings("unchecked")
    public T getAttr4(String attr){
        int attrl=attr.length();
        if(attrl==7){
            return (T) address;
        }else if(attrl==5){
            return (T)email;
        }else if(attrl==4){
            return (T)name;
        }else if(attrl==3){
            return (T)new Integer(age);
        }
        return null;
    }
    public void setAttr4(String attr,Object value){
        int attrl=attr.length();
        if(attrl==7){
            this.address=(String) value;
        }else if(attrl==5){
            this.email=(String) value;
        }else if(attrl==4){
            this.name=(String) value;
        }else if(attrl==3){
            this.age=(int) value;
        }
    }
    
    public static void main(String[] args) {
        UserBean ub=new UserBean();
        MapubMap=new HashMap<>();
        MapubMap2=new HashMap<>();
        MapubMap3=new HashMap<>();
        
        long limit=1000000000l;
        int addressAttr="address".hashCode();
        int emailAttr="email".hashCode();
        int nameAttr="name".hashCode();
        int ageAttr="age".hashCode();
 
        long addressAttrL=(long)addressAttr<<32;
        long emailAttrL=(long)emailAttr<<32;
        long nameAttrL=(long)nameAttr<<32;
        long ageAttrL=(long)ageAttr<<32;
        
        long t;
        
        t=System.currentTimeMillis();
        for(long i=0;i

运行结果

耗时5673ms ==_ATTRHASH
耗时2236ms SWITCH_ATTRHASH
耗时1581ms STRING_EQUALS
耗时1564ms SWITCH_ATTRL
耗时1595ms IF_ATTRL
耗时1579ms GET_SET
耗时27894ms STRING_MAP
耗时53701ms INTEGER_MAP
耗时58305ms LONG_MAP

你可能感兴趣的:(Java中Bean对象根据属性名称获取和设置其值和Map的性能)