问题:为什么ChangeListener ObservableMap使用“oldValue”调用newValue的副本?
import javafx.beans.property.SimpleMapProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
public class ObservableMapExample {
SimpleMapProperty map;
class MyChangeListener implements ChangeListener> {
@Override
public void changed(ObservableValue> changed,
ObservableMap oldValue, ObservableMap newValue) {
System.out.println("changed "+changed+" old:"+oldValue+" new:"+newValue);
}
}
ObservableMapExample() {
map = new SimpleMapProperty();
map.setValue(new SimpleMapProperty(FXCollections.observableHashMap()));
map.addListener(new MyChangeListener());
map.put("a", "1");
map.put("b", "2");
map.put("b", "3");
}
public static void main(String[] args) {
System.out.println("ObservableMapExample");
new ObservableMapExample();
}
}
输出:
ObservableMapExample
changed MapProperty [value: MapProperty [value: {a=1}]] old:MapProperty [value: {a=1}] new:MapProperty [value: {a=1}]
changed MapProperty [value: MapProperty [value: {a=1, b=2}]] old:MapProperty [value: {a=1, b=2}] new:MapProperty [value: {a=1, b=2}]
changed MapProperty [value: MapProperty [value: {a=1, b=3}]] old:MapProperty [value: {a=1, b=3}] new:MapProperty [value: {a=1, b=3}]