Docsity

Java Collections Framework: Types, Benefits, and Operations, Slides of Java Programming

Partial preview of the text

Download Java Collections Framework: Types, Benefits, and Operations and more Slides Java Programming in PDF only on Docsity!

Collections

Overview

  • A Collection is a container that groups similar elements into

an entity.

  • Examples would include a list of bank accounts, set of

students, group of telephone numbers.

  • The Collections framework in Java offers a unified approach to

store, retrieve and manipulate a group of data

  • Java’s Collection framework has an intuitive approach when

compared to other complex frameworks such as C++

Core Collections Framework

• The Collection framework forms a suitable

hierarchy:

Collection

Set List Queue

SortedSet

Map

SortedMap

Core Collections Framework

(Cont’d)

  • The core Collections framework is very generic in nature and provides a standard interface to all operations that can be performed.
  • For example the root interface Collection is declared as public interface Collection
    • E represents a generic object.
  • Collection: This interface is pretty much used in declarations and method signatures so any type of Collections can be passed around.
  • Set: A Collection interface that cannot take any duplicate elements.
  • List: An ordered Collection interface that allows duplicate elements and provides object retrieval based on an integer index.
  • Queue: Apart from following the FIFO (First In First Out) principles this Collection offers variety of implementation.
  • Map: Supports Collection of key and value pairs. Design does not allow duplicate keys.
  • Sorted Set: Ordered version of the set interface.
  • Sorted Map: Maintains ascending order of keys.

Iteration

  • Two ways to iterate over Collections:
    • Iterator static void loopThrough(Collection col){ for (Iterator iter = col.iterator();iter.hasnext()) { Object obj=iter.next(); } }
    • For-each static void loopThrough(Collection col){ for (Object obj: col) { //access object } }

Set

  • Set Interface: It offers inherited services from the root

Collection interface with the added functionality of restricting

duplicate elements.

  • Implementations:

Set

HashSet TreeSet LinkedHashSet

Set Implementation Comparisons

  • Idioms for using bulk operations on sets:
  • Copy a set when doing bulk operations to retain the original set.
  • Example: Set unionSet = new TreeSet(set1); unionSet.addAll(set2);

HashSet TreeSet Linked HashSet

Storage Type Hash Table Red-Black Tree Hash Table with a Linked List

Performance Best performance Slower than HashSet

Little costly than HashSet

Order of Iteration No guarantee of order of iteration

Order based Orders elements based on insertion

List

  • In addition to the core services inherited from the root collection interface, the

list interface offers

  • Positional access
  • Search
  • Customized Iteration
  • Range-view
  • List Implementation
  • ArrayList offers better performance compared to Linkedlist.

List

Arraylist Vector Linkedlist

List Iterator

• Customized Iteration: ListIterator

– Offers iteration in both directions, forward and

backward

– Example:

for(ListIterator lIter=strList.listIterator(strList.size());lIter.hasPrevious()) { String str = lIter.previous(); }

List Operations

• Range-View:

– subList(int from, int to) returns a view portion of

the sublist starting from to exclusive.

Queue

  • In addition to the inherited core services offered by

Collection, queue offers following methods in two flavors:

Purpose Throw Exception

Return Special Value

Insert Inserts an elements to the queue

add(obj) offer(obj)

Remove Remove head of the queue and return it.

remove() poll()

Examine Return the head of the queue

element() peek()

Map

  • Basic operations:
    • Val put (Object key);
    • Val get (Object key);
    • Val remove (Object key);
    • boolean containsKey (Object key);
    • boolean containsValue (Object value);
    • int size();
    • boolean isEmpty();
  • Bulk services
    • Void putAll(Map<? extends Key,? extends Val> map);
    • void clear();

Map Implementations

Performance Increases

Map

HashMap TreeMap Linkedlist HashTable

SortedSet

  • SortedSet stores elements in ascending order as per the natural ordering or

as defined by a comparator provided at instantiation time.

  • Also the iterator and toArray methods iterate and return array in sorted

order.

  • Additional Services provided:
    • SortedSet subSet (Element fromElement, Element toElement);
    • SortedSet headSet (Element toElement);
    • SortedSet tailSet (Element fromElement);
    • element first();
    • element last();
    • Comparator <? Super Element> comparator();