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 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统