-1

Does views over maps map specifically only refer to returning key sets or more so returning the entire map collection. Bit confused on views over map. Would love a more concise explanation. I'm looking at the Map API but I'm still not getting where "view" is coming from?

https://www.geeksforgeeks.org/map-interface-java-examples/

mojojojo47
  • 13
  • 6
  • basically the methods that has view in their description returns some sort of set with objects from the map, usually so you can iterate over them. which would mean that you get a sample of the *same* objects in the map, therefor change the object will be represented in the map. you just see (view) them differently - as a list of keys or entries – Gabriel H Jul 19 '20 at 17:39
  • What do *you* mean by views over maps? Can you edit the question to show an example of a usage of that term? – Joni Jul 19 '20 at 18:24

1 Answers1

-1

If you read the documentation, i.e. the javadoc of Map, it says:

The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings.

It you then look at the javadoc of e.g. method entrySet():

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

The word "view" is in contrast to "copy". If the entrySet() method returned a copy, changes made to the returned Set would not affect the Map, but since the returned Set is a view of the Map, changes made to the Set will affect the Map.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you. The word “view” in itself is confusing because what is returned can be modified but it’s more so used here in “viewing” your collection differently. – mojojojo47 Jul 19 '20 at 20:00
  • @mojojojo47 A `Map` is an object. You can *view* it from multiple angles. From one angle you can only see the keys (`keySet()`). From another angle you can only see the values (`values()`). From a third angle you can see both at the same time (`entrySet()`). If it is a `NavigableMap`, you can "zoom" in and see just a section of the map (`subMap()`, `headMap()`, `tailMap()`), or upside-down (`descendingMap()`). In all cases, you can still modify the map, e.g. even if you can only see the keys of the first half of the map (view of view of map), you can remove a key/value pair you're looking at. – Andreas Jul 20 '20 at 02:45