Java 类singleton实例,一个Hashmap上的clear函数清除另一个Hashmap

Java 类singleton实例,一个Hashmap上的clear函数清除另一个Hashmap,java,android,hashmap,singleton,Java,Android,Hashmap,Singleton,我有一个单例类,它有两个Hashmaps,都是私有的,有各自的(经过测试和工作的)getter和setter private static Property property = new Property(); public static Property getInstance(){ return property; } private HashMap<String, Object> roomReturnedObj = new HashMap<>(); pr

我有一个单例类,它有两个Hashmaps,都是私有的,有各自的(经过测试和工作的)getter和setter

private static Property property = new Property();

public static Property getInstance(){
    return property;
}

private HashMap<String, Object> roomReturnedObj = new HashMap<>();
private HashMap<String, Object> roomTasks = new HashMap<>();

public void setRoomObjectFromResult(HashMap<String, Object> roomHashMap) {
    roomReturnedObj.putAll(roomHashMap);
}

public HashMap<String, Object>getRoomObjectFromResult(){
    return roomReturnedObj;
}


public HashMap<String, Object>getRoomTasks(){
    return roomTasks;
}

public void setRoomTasks(HashMap<String, Object> tasksHashMap){
    roomTasks.putAll(tasksHashMap);
}
上面的函数清除roomReturnedObj哈希映射

设置类

saveBtn.setOnClickListener(View ->{
            Bundle resultFinal = new Bundle();
            String roomName = Property.getInstance().getRoomCount()+ ": " + Property.getInstance().getRoomName();

            HashMap<String, Object> room = new HashMap<>();
            room.put(roomName, Property.getInstance().getRoomTasks());

            Property.getInstance().setRoomObjectFromResult(room);

            Property.getInstance().setRoomCount(Property.getInstance().getRoomCount());

            Log.d("Test", Property.getInstance().getRoomObjectFromResult()+ " : " + Property.getInstance().getRoomTasks());

            Intent returnedResultIntent = new Intent(getBaseContext(), NewJobStart.class);
            returnedResultIntent.putExtra("addRoomResult", "Success");
            startActivity(returnedResultIntent, resultFinal);
        });

这里的问题是Java是对象上的一个按引用传递,尽管它看起来像是创建了一个新的HashMap和值,但在传递对象时只调用一个引用。简单修复我的onClick

saveBtn.setOnClickListener(View ->{
            Bundle resultFinal = new Bundle();
            String roomName = Property.getInstance().getRoomCount()+ ": " + Property.getInstance().getRoomName();
            //Create a new roomTasks Hashmap locally here!!
            HashMap<String, Object> roomTasks = new HashMap<>();
            roomTasks.putAll(Property.getInstance().getRoomTasks());
            //Then pass it back to the singleton class
            HashMap<String, Object> room = new HashMap<>();
            room.put(roomName, roomTasks);

            Property.getInstance().setRoomObjectFromResult(room);

            Property.getInstance().setRoomCount(Property.getInstance().getRoomCount());

            Log.d("Test", Property.getInstance().getRoomObjectFromResult()+ " : " + Property.getInstance().getRoomTasks());

            Intent returnedResultIntent = new Intent(getBaseContext(), NewJobStart.class);
            returnedResultIntent.putExtra("addRoomResult", "Success");
            startActivity(returnedResultIntent, resultFinal);
        });
saveBtn.setOnClickListener(视图->{
Bundle resultFinal=新Bundle();
字符串roomName=Property.getInstance().getRoomCount()+“:”+Property.getInstance().getRoomName();
//在本地创建一个新的roomTasks Hashmap!!
HashMap roomTasks=新建HashMap();
putAll(Property.getInstance().getRoomTasks());
//然后将其传递回singleton类
HashMap房间=新建HashMap();
room.put(roomName,roomTasks);
Property.getInstance().setRoomObjectFromResult(房间);
Property.getInstance().setRoomCount(Property.getInstance().getRoomCount());
Log.d(“Test”,Property.getInstance().getRoomObjectFromResult()+”:“+Property.getInstance().getRoomTasks());
Intent returnedResultIntent=newintent(getBaseContext(),NewJobStart.class);
returnedResultent.putExtra(“addRoomResult”、“Success”);
开始触觉(returnedResultent,resultFinal);
});

由于应用程序生命周期的原因,您确定没有重新启动您的单身汉吗?你确定你在运行你认为是的代码?问题不太可能出现在这段代码中。@DaveNewton我添加了一个编辑来显示设置和清除类,希望能解释我为什么感到困惑
if (bundle != null) {
            tradeTitles = bundle.getStringArrayList("TradeTitleArray");
            Log.d("Test", "NewJobStart, tradeTitles received from bundle" + tradeTitles + ", Room count: " + Property.getInstance().getRoomCount());

            if(Property.getInstance().getRoomCount() > 0) {
                String test = bundle.getString("addRoomResult");
                Log.d("Test", "NewJobStart, result from jobRoomDetails received from bundle" + test);

                if (test.equals("Success")) {
                    Log.d("Test", "NewJobStart - return, before clear: " + Property.getInstance().getRoomObjectFromResult());
                    Property.getInstance().getRoomTasks().clear();
                    putToList();
                }
            }
        } 
saveBtn.setOnClickListener(View ->{
            Bundle resultFinal = new Bundle();
            String roomName = Property.getInstance().getRoomCount()+ ": " + Property.getInstance().getRoomName();
            //Create a new roomTasks Hashmap locally here!!
            HashMap<String, Object> roomTasks = new HashMap<>();
            roomTasks.putAll(Property.getInstance().getRoomTasks());
            //Then pass it back to the singleton class
            HashMap<String, Object> room = new HashMap<>();
            room.put(roomName, roomTasks);

            Property.getInstance().setRoomObjectFromResult(room);

            Property.getInstance().setRoomCount(Property.getInstance().getRoomCount());

            Log.d("Test", Property.getInstance().getRoomObjectFromResult()+ " : " + Property.getInstance().getRoomTasks());

            Intent returnedResultIntent = new Intent(getBaseContext(), NewJobStart.class);
            returnedResultIntent.putExtra("addRoomResult", "Success");
            startActivity(returnedResultIntent, resultFinal);
        });