Presentaion Anudip Gropu 3
Presentaion Anudip Gropu 3
1
Collection Interface
• Defines fundamental methods
» int size();
» boolean isEmpty();
» boolean contains(Object element);
» boolean add(Object element); // Optional
» boolean remove(Object element); // Optional
» Iterator iterator();
2
Iterator Interface
• Defines three fundamental methods
» Object next()
» boolean hasNext()
» void remove()
• These three methods provide access to the
contents of the collection
• An Iterator knows position within collection
• Each call to next() “reads” an element from the
collection
» Then you can use it or remove it
3
Iterator Position
4
List Interface
• The List interface adds the notion of order to a
collection
• The user of a list has control over where an element is
added in the collection
• Lists typically allow duplicate elements
• Provides a ListIterator to step through the elements in
the list.
5
ListIterator Interface
• Extends the Iterator interface
• Defines three fundamental methods
» void add(Object o) - before current position
» boolean hasPrevious()
» Object previous()
• The addition of these three methods defines the basic
behavior of an ordered list
• A ListIterator knows position within list
6
List Implementations
• ArrayList
» low cost random access
» high cost insert and delete
» array that resizes if need be
• LinkedList
» sequential access
» low cost insert and delete
» high cost random access
7
ArrayList methods
• The indexed get and set methods of the List interface are
appropriate to use since ArrayLists are backed by an array
» Object get(int index)
» Object set(int index, Object element)
• Indexed add and remove are provided, but can be costly if
used frequently
» void add(int index, Object element)
» Object remove(int index)
• May want to resize in one shot if adding many elements
» void ensureCapacity(int minCapacity)
8
LinkedList methods
• The list is sequential, so access it that way
» ListIterator listIterator()
• ListIterator knows about position
» use add() from ListIterator to add at a position
» use remove() from ListIterator to remove at a position
• LinkedList knows a few things too
» void addFirst(Object o), void addLast(Object o)
» Object getFirst(), Object getLast()
» Object removeFirst(), Object removeLast()
9
Queue Interface Intro
• Methods:
» add(E e): Inserts the specified element.
» remove(): Retrieves and removes the element at the front of the
queue, or throws an exception if the queue is empty.
» poll(): Retrieves and removes the head of the queue.
» peek(): Retrieves, but does not remove, the head of the queue.
• Behavior:
» Null Elements: Not allowed.
» Order Maintained: Yes, in FIFO (First-In-First-Out) order.
» Duplicates Allowed: Yes.
10
Queue Interface
• Same methods as Collection
» A queue interface maintains the FIFO(First In First
Out) order similar to a real-world queue line.
• Classes Available:
» Dequeue (Interface) : Supports element insertion and removal at
both ends. Null not allowed
» PriorityQueue (Class): A priority heap-based queue that orders
elements based on natural ordering or a provided comparator, without
allowing null elements.
» LinkedList (Class): Allows null elements and maintains insertion
order.
» ArrayDequeue (Class): Not allows null elements, and supports
insertion and removal at both ends.
11
Set Interface
• Same methods as Collection
» different contract - no duplicate entries
• Defines two fundamental methods
» boolean add(Object o) - reject duplicates
» Iterator iterator()
• Provides an Iterator to step through the elements
in the Set
» No guaranteed order in the basic Set interface
» There is a SortedSet interface that extends Set
12
HashSet
• Find and add elements very quickly
» uses hashing implementation in HashMap
• Hashing uses an array of linked lists
» The hashCode() is used to index into the array
» Then equals() is used to determine if element is in the
(short) list of elements at that index
• No order imposed on elements
• The hashCode() method and the equals() method
must be compatible
» if two objects are equal, they must have the same
hashCode() value
13
TreeSet
• Elements can be inserted in any order
• The TreeSet stores them in order
» Red-Black Trees out of Cormen-Leiserson-Rivest
• An iterator always presents them in order
• Default order is defined by natural order
» objects implement the Comparable interface
» TreeSet uses compareTo(Object o) to sort
• Can use a different Comparator
» provide Comparator to the TreeSet constructor
14
Map Interface
• Stores key/value pairs
• Maps from the key to the value
• Keys are unique
» a single key only appears once in the Map
» a key can map to only one value
• Values do not have to be unique
15
Map methods
Object put(Object key, Object value)
Object get(Object key)
Object remove(Object key)
boolean containsKey(Object key)
boolean containsValue(Object value)
int size()
boolean isEmpty()
16
HashMap and TreeMap
• HashMap
» The keys are a set - unique, unordered
» Fast
• TreeMap
» The keys are a set - unique, ordered
» Same options for ordering as a TreeSet
• Natural order (Comparable, compareTo(Object))
• Special order (Comparator, compare(Object, Object))
17
Bulk Operations
• In addition to the basic operations, a Collection may
provide “bulk” operations
boolean containsAll(Collection c);
boolean addAll(Collection c); // Optional
boolean removeAll(Collection c); // Optional
boolean retainAll(Collection c); // Optional
void clear(); // Optional
Object[] toArray();
Object[] toArray(Object a[]);
18
Utilities
• The Collections class provides a number of static
methods for fundamental algorithms
• Most operate on Lists, some on all Collections
» Sort, Search, Shuffle
» Reverse, fill, copy
» Min, max
• Wrappers
» synchronized Collections, Lists, Sets, etc
» unmodifiable Collections, Lists, Sets, etc
19
Comparable
» Intro:
• The Comparable interface is used for defining the natural ordering
of objects. It allows you to compare instances of the same class.
» Method:
• The compareTo() method compares the current object with
another object and returns:
• A negative value if the current object is less than the other object.
• Zero if they are equal.
• A positive value if the current object is greater than the other
object.
20
Comparator
» Intro:
• The Comparator interface is used for customized sorting of
objects. It allows you to compare instances of different classes or
define alternative sorting criteria.
» Method:
• The compare() method in a Comparator compares two objects and
returns:
• A negative value if the first object is less than the second.
• Zero if they are equal.
• A positive value if the first object is greater than the second.
21