DOC PREVIEW
Penn CIT 594 - Introduction to Collections

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to CollectionsCollectionsTypes of CollectionThe Collections hierarchyCollections are ADTsThe Collection interfaceThe Iterator interfaceThe Set interfaceThe List interfaceThe SortedSet interfaceThe Map interfaceThe SortedMap interfaceSome implementationsLearn these slides well!The EndJan 14, 2019Introduction to Collections2CollectionsA collection is a structured group of objectsJava 1.2 introduced the Collections FrameworkCollections are defined in java.utilThe Collections framework is mostly about interfacesThere are a number of predefined implementationsJava 5 introduced generics and “genericized” all the existing collectionsVectors have been redefined to implement CollectionTrees, linked lists, stacks, hash tables, and other classes are implementations of CollectionArrays do not implement the Collection interfaces3Types of CollectionJava supplies several types of Collection:Set: cannot contain duplicate elements, order is not importantSortedSet: like a Set, but order is importantList: may contain duplicate elements, order is importantJava also supplies some “collection-like” things:Map: a “dictionary” that associates keys with values, order is not importantSortedMap: like a Map, but order is importantWhile you can get all the details from the Java API, you are expected to learn (i.e. memorize):The signatures of the “most important” methods in each interfaceThe most important implementations of each interface4The Collections hierarchy5Collections are ADTsCollections are one of the best-designed parts of Java, becauseThey are elegant: they combine maximum power with maximum simplicityThey are uniform: when you know how to use one, you almost know how to use them allYou can easily convert from one to another6The Collection interfaceMuch of the elegance of the Collections Framework arises from the intelligent use of interfacesThe Collection interface specifies (among many other operations):boolean add(E o)boolean contains(Object o)boolean remove(Object o)boolean isEmpty()int size()Object[] toArray()Iterator<E> iterator()You should learn all the methods of the Collection interface--all are important7The Iterator interfaceAn iterator is an object that will return the elements of a collection, one at a timeinterface iterator<E>boolean hasNext() Returns true if the iteration has more elements E next()Returns the next element in the iteration void remove()Removes from the underlying collection the last element returned by the iterator (optional operation)8The Set interfaceA set is a collection in which:There are no duplicate elements (according to equals), andOrder is not importantinterface Set<E> implements Collection, IterableThe methods of Set are exactly the ones in CollectionThe following methods are especially interesting:boolean contains(Object o) // membership testboolean containsAll(Collection<?> c) //subset testboolean addAll(Collection<? extends E> c) // unionboolean retainAll(Collection<?> c) // intersectionboolean removeAll(Collection<?> c) // differenceaddAll, retainAll, and removeAll return true if the receiving set is changed, and false otherwise9The List interfaceA list is an ordered sequence of elementsinterface List<E> implements Collection, IterableSome important List methods are:void add(int index, E element)E remove(int index)boolean remove(Object o)E set(int index, E element)E get(int index)int indexOf(Object o)int lastIndexOf(Object o)ListIterator<E> listIterator()A ListIterator is like an Iterator, but has, in addition, hasPrevious and previous methods10The SortedSet interfaceA SortedSet is a Set for which the order of elements is importantinterface SortedSet<E> implements Set, Collection, IterableTwo of the SortedSet methods are:E first()E last()More interestingly, only Comparable elements can be added to a SortedSet, and the set’s Iterator will return these in sorted orderThe Comparable interface is covered in a separate lecture11The Map interfaceA map is a data structure for associating keys and valuesInterface Map<K,V>The two most important methods are:V put(K key, V value) // adds a key-value pair to the mapV get(Object key) // given a key, looks up the associated valueSome other important methods are:Set<K> keySet() Returns a set view of the keys contained in this map.Collection<V> values()Returns a collection view of the values contained in this map12The SortedMap interfaceA sorted map is a map that keeps the keys in sorted orderInterface SortedMap<K,V>Two of the SortedMap methods are:K firstKey()K lastKey()More interestingly, only Comparable elements can be used as keys in a SortedMap, and the method Set<K> keySet() will return a set of keys whose iterator will return them sorted orderThe Comparable interface is covered in a separate lecture13Some implementationsclass HashSet<E> implements Setclass TreeSet<E> implements SortedSetclass ArrayList<E> implements Listclass LinkedList<E> implements Listclass Vector<E> implements Listclass Stack<E> extends VectorImportant methods: push, pop, peek, isEmptyclass HashMap<K, V> implements Mapclass TreeMap<K, V> implements SortedMapAll of the above provide a no-argument constructor14Learn these slides well!I hate to ask students to memorize lists of methods, but this time I willYou should know the signatures of all the methods in this set of slidesOnce you have a better feel for how these collections are used, the methods will make sense and be easier to remember15The


View Full Document

Penn CIT 594 - Introduction to Collections

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Introduction to Collections
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Collections and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Collections 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?