...
你放入两个map的是同一个对象的引用,改变了对象,当然都会反映出来变化。

解决方案 »

  1.   

    Map map=new HashMap();
    if(map.containsKey("123")){
    System.out.println("122");
    }else{
    System.out.println("000");
    }

     
    Map todayCustomerMap = new HashMap();
    HashMap todayCustomerActionMap=new HashMap();
    Collections.synchronizedMap(todayCustomerMap) ;
    Collections.synchronizedMap(todayCustomerActionMap) ;

    Dog dog=new Dog();
    dog.setAge(12);
    dog.setName("xiaoluo");

    Dog dog1=new Dog();
    dog1.setAge(15);
    dog1.setName("xiaoluo1");
    Dog dog2=new Dog();//新的对象
    BeanUtils.copyProperties(dog,dog2);
    Dog dog3=new Dog();//新的对象
    BeanUtils.copyProperties(dog1,dog3);
    todayCustomerMap.put("xiaoluo", dog2);
    todayCustomerMap.put("xiaoluo1", dog3);

    todayCustomerActionMap.put("xiaoluo", dog);
    todayCustomerActionMap.put("xiaoluo1", dog1);
    //这里从map中取出来,并作了修改
    Dog dog03=(Dog)todayCustomerActionMap.get("xiaoluo");
    //Dog dog03=new Dog();
    dog03.setAge(12);
    dog03.setName("aaa");
    todayCustomerActionMap.put("xiaoluo", dog03);


    Dog dog04=(Dog)todayCustomerActionMap.get("xiaoluo");//注意这个对象作了修改
    Dog dog05=(Dog)todayCustomerMap.get("xiaoluo");//注意这个对象没有作任何修改
    //这里发生了奇怪的事,dog05为什么也修改了?我不知道为什么?
    System.out.println(dog04.getAge()+dog04.getName());
    System.out.println(dog05.getAge()+dog05.getName());
    if(dog04.equals(dog05)){//判断两个对象值是否相等
    System.out.println("equal");
    }else{
    System.out.println("not equal");
    }