程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> hashmap-map中存儲一個普通javabean和存儲普通類型

hashmap-map中存儲一個普通javabean和存儲普通類型

編輯:編程解疑
map中存儲一個普通javabean和存儲普通類型

代碼如下

        User user=new User();
        user.setAge(24);
        Integer count=new Integer(1);
        String str=new String("I am a boy");
        Map<String, Object> map=new HashMap<>();
        map.put("count",count);
        map.put("str", str);
        count=(Integer) map.get("count");
        count=new Integer(count+1);
        str=(String) map.get("str");
        str="我是一個男孩!";
        map.put("user",user);
        user=(User) map.get("user");
        user.setAge(44);
        System.out.println(map.get("count")+"\t"+map.get("str")+"\t"+user);

輸出結果為:
1 I am a boy User [username=null, password=null, age=44]
user的類

 package com.yd.action;

import java.io.Serializable;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

public class User implements Serializable,RequestAware,SessionAware,ApplicationAware{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private Integer age;
    private Map<String, Object> request,session,application=null;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((username == null) ? 0 : username.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + ", age=" + age + "]";
    }
    /*
     * 處理登陸請求的方法
     * */
    public String loginControl(){
        String result="fail";
        if (this.getUsername()==null||this.getPassword()==null||this.getAge()==null) {
            return result;
        }
        if("yd".equals(this.getUsername())&&"a".equals(this.getPassword())){
            //在這個裡面代表是登陸成功
            session.put("user", this);
            //如果application中有了count  那麼就直接加一
            if(application.get("count")==null){
                application.put("count", 1);
            }else{
                Integer count=(Integer) application.get("count");
                count=count+1;
                application.put("count",count );
            }
            result="success";
        }
        return result;
    }
    /*
     * 處理退出登陸的請求
     * */
    public String loginOut(){
        Integer count=(Integer) application.get("count");
        count=count-1;
        /*
         * ?
         * */
        application.put("count", count);
        SessionMap<String, Object> sessionMap=(SessionMap<String, Object>) session;
        //使得session失效
        sessionMap.invalidate();
        return "success";
    }
    @Override
    public void setApplication(Map<String, Object> application) {
        this.application=application;
    }
    @Override
    public void setSession(Map<String, Object> session) {
        this.session=session;
    }
    @Override
    public void setRequest(Map<String, Object> request) {
        this.request=request;
    }
}

為什麼普通類型包裝類就需要重新put進入才能改變map裡面的值?

最佳回答:


你理解錯了,count和str都指向了新的對象,並沒有改變原值

xiaowenK
nihaowoshiyudong
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved