lec03 - CSE 12 The Java Collections Framework The Java...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
4-1/38 The Java Collections Framework The Collection interface Iterators and the Iterator Pattern Developing a test plan for a Collection class Towards implementing a Collection class CSE 12 The Java Collections Framework
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4-2/38 Data structures, collections, containers A data structure is. .. an object that contains data Another word for a data structure is a “collection” or “container” Different programming language libraries provide various data structures that can be used by application programmers In Java, this is the Java Collections Framework (JCF) The JCF is a collection of interfaces, abstract and concrete classes providing a standard set of collection (container) types, in the java.util package
Background image of page 2
4-3/36 Interfaces in JCF
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4-4/36 List implementations in JCF
Background image of page 4
4-5/36 Set implementations in JCF
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4-6/36 The Collection interface A very important interface in the JCF is Collection • Collection specifies 13 instance methods which cover most general data structure operations A class implementing Collection must provide definitions for all the methods, or it will not compile However, in the documentation of Collection , some of these methods are listed as “optional operations” These must be defined, but the definition can just consist of a single line that creates and throws an UnsupportedOperationException
Background image of page 6
4-7/36 Collection<E> Required methods boolean contains(Object target) Returns true if this collection contains the target element. boolean containsAll(Collection<?> c) Returns true if this collection contains all of the elements in the collection c . boolean isEmpty() Returns true if this collection contains no elements. int size() Returns the number of elements stored in this collection. Iterator<E> iterator() Returns an iterator over the elements in this collection. Object[] toArray() Returns an array containing all of the elements in this collection. <T> T[] toArray(T[] a) Returns an array containing all of the elements in this collection with runtime type T .
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4-8/36 Collection<E> Optional Methods boolean add(E element) Adds element to this collection. Returns true if the collection is modified. boolean addAll(Collection<? extends E> c) Adds all of the elements from the collection c to this collection. Returns true if the collection is modified. boolean remove(Object element) Removes a single instance of element from this collection, if it is present. Returns true if the collection is modified. This method relies on the equals() method defined in the runtime class of element . boolean removeAll(Collection<?> c) Removes all the elements found in the collection c from this collection. Returns true if the collection is modified. boolean retainAll(Collection<?> c) Retains only the elements in this collection that are also contained in the collection c . Returns true if the collection is modified.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

lec03 - CSE 12 The Java Collections Framework The Java...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online