nihaowoshiyudong 2016-12-30 02:30 采纳率: 100%
浏览 1744
已采纳

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里面的值?

  • 写回答

3条回答 默认 最新

  • 「已注销」 2016-12-30 04:48
    关注

    你理解错了,count和str都指向了新的对象,并没有改变原值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现