java collections framework
play

Java Collections Framework 6 May 2019 OSU CSE 1 Overview The - PowerPoint PPT Presentation

Java Collections Framework 6 May 2019 OSU CSE 1 Overview The Java Collections Framework (JCF) is a group of interfaces and classes similar to the OSU CSE components The similarities will become clearly evident from examples See


  1. Java Collections Framework 6 May 2019 OSU CSE 1

  2. Overview • The Java Collections Framework (JCF) is a group of interfaces and classes similar to the OSU CSE components – The similarities will become clearly evident from examples – See Java libraries package java.util • There are some important differences, too, however, that deserve mention (at the end) 6 May 2019 OSU CSE 2

  3. Overview of Interfaces Iterable Collection Map Sorted- Set List Queue Map Sorted- Navigable- Deque Set Map Navigable- Set 6 May 2019 OSU CSE 3

  4. Overview of Interfaces Iterable Collection Map Sorted- Set List Queue Map Sorted- Navigable- Note: Map does not Deque Set Map extend Collection ; but it is a “collection”. Navigable- Set 6 May 2019 OSU CSE 4

  5. Overview of Interfaces Iterable Collection Map Sorted- Set List Queue Map Iterable is in java.lang Sorted- Navigable- Deque (because of its intimate Set Map connection to for-each loops), but Iterator is in java.util . Navigable- Set 6 May 2019 OSU CSE 5

  6. Overview of Interfaces Subsequent slides discuss only certain interfaces. Iterable Collection Map Sorted- Set List Queue Map Sorted- Navigable- Deque Set Map Navigable- Set 6 May 2019 OSU CSE 6

  7. The Collection<E> Interface • Essentially a finite multiset of E • No direct/efficient way to ask how many “copies” of a given element there are • Two interesting methods to create arrays of the elements • Many methods (including add , remove , clear ) are “optional” 6 May 2019 OSU CSE 7

  8. The Set<E> Interface • Essentially a finite set of E • No removeAny or similar method, so you must use iterator to iterate over a Set – Recall (from Iterator ): “The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress [except using Iterator.remove ].” • Many methods (including add , remove , clear ) are “optional” 6 May 2019 OSU CSE 8

  9. The List<E> Interface • Essentially a string of E • Access by position (similar to Sequence from OSU CSE components) • Many methods (including add , remove , clear ) are “optional” • Two interesting additional features: – Sublist “views” of a List – A special two-way ListIterator 6 May 2019 OSU CSE 9

  10. The List<E> Interface • Essentially a string of E • Access by position (similar to Sequence How do you move forward from OSU CSE components) and backward through a List • Many methods (including add , remove , from OSU CSE components? clear ) are “optional” • Two interesting additional features: – Sublist “views” of a List – A special two-way ListIterator 6 May 2019 OSU CSE 10

  11. The Queue<E> Interface • Essentially a string of E • Access at ends (similar to Queue from OSU CSE components) • Here, add and remove are not “optional” – add is similar to enqueue for OSU CSE components’ Queue – remove is similar to dequeue • Curious names for other methods, e.g., offer , peek , poll 6 May 2019 OSU CSE 11

  12. The Map<K,V> Interface • Essentially a finite set of (K,V) with the function property • No removeAny or similar method, so you must use iterator (somewhat indirectly) to iterate over a Map • Many methods (including put , remove , clear ) are “optional” • Like List , a Map supports “views” of its elements 6 May 2019 OSU CSE 12

  13. Views in the JCF • A view is a “subcollection” of a collection – Not a copy of some of the elements, but rather “a collection within a collection” that is manipulated “in place” • Views for Map : – Keys: Set<K> keySet() – Values: Collection<V> values() – Pairs: Set<Map.Entry<K,V>> entrySet() 6 May 2019 OSU CSE 13

  14. Views in the JCF • A view is a “subcollection” of a collection – Not a copy of some of the elements, but rather Map.Entry<K,V> in the JCF is “a collection within a collection” that is very similar to Map.Pair<K,V> manipulated “in place” in the OSU CSE components. • Views for Map : – Keys: Set<K> keySet() – Values: Collection<V> values() – Pairs: Set<Map.Entry<K,V>> entrySet() 6 May 2019 OSU CSE 14

  15. Example: Map<String, Integer> m Code State m = {("PB", 99), ("BK", 42), ("SA", 42)} Set<String> s = m.keySet(); m = {("PB", 99), ("BK", 42), ("SA", 42)} s = {"SA", "BK", "PB"} 6 May 2019 OSU CSE 15

  16. Example: Map<String, Integer> m Note all the aliases here! Code State There is no problem in this case m = {("PB", 99), because String is immutable, ("BK", 42), but consider the potential ("SA", 42)} problems if it were not. Set<String> s = m.keySet(); m = {("PB", 99), ("BK", 42), ("SA", 42)} s = {"SA", "BK", "PB"} 6 May 2019 OSU CSE 16

  17. Example: Map<String, Integer> m Code State m = {("PB", 99), ("BK", 42), ("SA", 42)} Collection<Integer> c = m.values(); m = {("PB", 99), ("BK", 42), ("SA", 42)} c = {42, 99, 42} 6 May 2019 OSU CSE 17

  18. Example: Map<String, Integer> m Code State m = {("PB", 99), ("BK", 42)} Set<Map.Entry<String, Integer>> s = m.entrySet(); m = {("PB", 99), ("BK", 42)} s = {("BK", 42), ("PB", 99)} 6 May 2019 OSU CSE 18

  19. View “Backed By” Collection • A view is backed by the underlying collection, which means that if the view is modified then the underlying (“backing”) collection is also modified, and vice versa – See Javadoc for supported modifications – Be especially careful when iterating over a view of a collection and trying to modify it 6 May 2019 OSU CSE 19

  20. Example: List<Integer> s Code State s = <10, 7, 4, –2> s.subList(1,3).clear(); s = <10, –2> 6 May 2019 OSU CSE 20

  21. Example: Map<String, Integer> m Code State m = {("PB", 99), ("BK", 42), ("SA", 42)} m.values().remove(42); m = {("PB", 99), ("SA", 42)} 6 May 2019 OSU CSE 21

  22. Example: Map<String, Integer> m Because remove for Code State Collection (assuming it is available for m.values !) m = {("PB", 99), removes one copy, we do not ("BK", 42), know which pair remains in m . ("SA", 42)} m.values().remove(42); m = {("PB", 99), ("SA", 42)} 6 May 2019 OSU CSE 22

  23. Could remove Cause Trouble? • The object (dynamic) type of m.values() in the above code might be an implementation of List or of Queue – But not of Set ; why not? • Could the optional remove method not be implemented by the object type of m.values() ? 6 May 2019 OSU CSE 23

  24. Could remove Cause Trouble? No! The informal Javadoc for the values method says: • The object (dynamic) type of “The collection supports element removal, which removes the corresponding mapping from the map, via m.values() in the above code might be the Iterator.remove , Collection.remove , an implementation of List or of Queue removeAll , retainAll and clear operations. It does not support the add or addAll operations.” – But not of Set ; why not? • Could the optional remove method not be implemented by the object type of m.values() ? 6 May 2019 OSU CSE 24

  25. Iterating Over a Map • Because Map does not extend Iterable , but Collection (hence Set ) does extend Iterable , you can (only) iterate over a Map using one of its three views: – Keys: Set<K> keySet() – Values: Collection<V> values() – Pairs: Set<Map.Entry<K,V>> entrySet() 6 May 2019 OSU CSE 25

  26. Overview of Collection Classes Iterable Collection Object Abstract- Collection There are no classes that directly and fully implement Collection . 6 May 2019 OSU CSE 26

  27. AbstractCollection • Has code for many methods (shared, and possibly overridden, by all later implementations of Collection ) : – add – remove – clear – ... 6 May 2019 OSU CSE 27

  28. AbstractCollection • Has code for many methods (shared, and possibly overridden, by all later implementations of Collection ) : – add This method’s implementation here, for – remove example, “always throws an – clear UnsupportedOperationException ”. – ... 6 May 2019 OSU CSE 28

  29. Overview of Set Classes Iterable Collection Object Abstract- Set Collection AbstractSet HashSet TreeSet 6 May 2019 OSU CSE 29

  30. AbstractSet • Has code for these methods (shared, and possibly overridden, by all later implementations of Set ): – equals – hashCode – removeAll 6 May 2019 OSU CSE 30

  31. HashSet • Uses hashing in the Set representation • Has code for these methods (overriding those in AbstractSet ): – add – remove – clear – clone 6 May 2019 OSU CSE 31

  32. HashSet • Uses hashing in the Set representation • Has code for these methods (overriding those in AbstractSet ): – add The first three methods, though “optional”, are – remove implemented here and do what – clear you should expect. – clone 6 May 2019 OSU CSE 32

Recommend


More recommend