JavaFX: ObsevableMap keySet as observable set

I want to convert the control set ObservableMap to read only ObservableSet. I don't want to copy the value, any modification of the ObservableMap should affect the Observable keySet. If I bind another set to the content contained in the observable key, its content will automatically update.

This is what I would like to write.

ObservableMap<String, Object> map = FXCollections.observableHashMap();
ObservableSet<String> keySet = FXCollections.observableKeySet(map);
Set<String> boundSet = new HashSet<String>();
Bindings.bindContent(boundSet, keySet);
map.put("v", new Object());
assert boundSet.contains("v");

      

Is there this feature in the JavaFX SDK?

+3


source to share


1 answer


The requested function does not require any special ObservableSet

. Its already part of the contract Map

:

ObservableMap<String, Object> map = FXCollections.observableHashMap();
Set<String> keySet = map.keySet();
map.put("v", new Object());
assert keySet.contains("v");

      

Key

A Map

s always reflects changes made to the support card.



http://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet--

Returns a Set

view of the keys contained in this map. The set is supported by the map, so changes to the map are reflected in the set and vice versa.

+1


source







All Articles