Some of Java's Collection Framework - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Some of Java's Collection Framework

Description:

Some of Java's Collection Framework – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 15
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: Some of Java's Collection Framework


1
Some of Java's Collection Framework
  • Rick Mercer

2
Java's Collection Framework
  • Collection framework
  • Unified architecture for representing and
    manipulating collections
  • Java's collection framework contains
  • Interfaces (ADTs) specification not
    implementation
  • Concrete implementations as classes
  • Polymorphic Algorithms to search, sort, find,
    shuffle, ...
  • The algorithms are polymorphic
  • the same method can be used on many different
    implementations of the appropriate collection
    interface. In essence, algorithms are reusable
    functionality.

3
Collection interfaces in java.util
Image from the Java Tutorial
4
Abstract Data Type
  • Abstract data type (ADT) is a specification of
    the behaviour (methods) of a type
  • Specifies method names to add, remove, find
  • Specifies if elements are unique, indexed,
    accessible from only one location, mapped,...
  • An ADT shows no implementation
  • no structure to store elements, no implemented
    algorithms
  • What Java construct nicely specifies ADTs?

5
Collection Classes
  • A collection class the can be instantiated
  • implements an interface as a Java class
  • implements all methods of the interface
  • selects appropriate instance variables
  • Java has concrete collection classes
  • ArrayListltEgt, LinkedListltEgt, VectorltEgt
  • StackltEgt
  • LinkedBlockingQueueltEgt, ArrayBlockingQueueltEgt
  • HashSetltEgt, TreeSetltEgt
  • TreeMapltK,Vgt, HashMapltK,Vgt

6
Common Functionality
  • Collection classes often have methods for
  • Adding objects
  • Removing an object
  • Finding a reference to a particular object find
  • can then send messages to the object still in the
    collection

7
ListltEgt, an ADT written as a Java interface
  • ListltEgt a collection with a first element, a
    last element, distinct predecessors and
    successors
  • The user of this interface has precise control
    over where in the list each element is inserted
  • duplicates that "equals" each other are allowed
  • The List interface is implemented by these three
    collection classes
  • ArrayListltEgt
  • LinkedListltEgt
  • VectorltEgt

8
  • import java.util. // For List, ArrayList,
    Linked ...
  • import static org.junit.Assert.
  • import org.junit.Test
  • public class ThreeClassesImplementList
  • _at_Test
  • public void showThreeImplementationsOfList()
  • // Interface name List
  • // Three classes that implement the List
    interface
  • ListltStringgt bigList new ArrayListltStringgt()
  • ListltStringgt littleList new
    LinkedListltStringgt()
  • ListltStringgt sharedList new
    VectorltStringgt()
  • // All three have an add method
  • bigList.add("in array list")
  • littleList.add("in linked list")
  • sharedList.add("in vector")

9
Can't add the wrong type
  • Java generics checks the type at compile time
  • See errors early--a good thing
  • "type safe" because you can't add different types
  • ArrayListltGregorianCalendargt dates
  • new ArrayListltGregorianCalendargt()
  • dates.add(new GregorianCalendar()) // Okay
  • dates.add("String not a GregorianCalendar") //
    Error
  • ArrayListltIntegergt ints new ArrayListltIntegergt()
  • ints.add(1) // Okay. Same as add(new
    Integer(1))
  • ints.add("Pat not an int")) // Error

10
Algorithms
  • Java has polymorphic algorithms to provide
    functionality for different types of collections
  • Sorting (e.g. sort)
  • Shuffling (e.g. shuffle)
  • Routine Data Manipulation (e.g. reverse, addAll)
  • Searching (e.g. binarySearch)
  • Composition (e.g. frequency)
  • Finding Extreme Values (e.g. max)
  • Demo a few with ArrayList
  • Override toString and equals for DayCounter

11
The Map Interface (ADT)
  • Map describes a type that stores a collection of
    elements that consists of a key and a value
  • A Map associates (maps) a key the it's value
  • The keys must be unique
  • the values need not be unique
  • put destroys one with same key

12
Map Operations
  • Java's TreeMapltK, Vgt
  • public V put(K key, V value)
  • associates key to value and stores mapping
  • public V get(Object key)
  • associates the value to which key is mapped or
    null
  • public boolean containsKey(Object key)
  • returns true if the Map already uses the key
  • public V remove(Object key)
  • Returns previous value associated with specified
    key, or null if there was no mapping for key.
  • CollectionltVgt values()
  • get a collection you can iterate over

13
Using a Map
  • _at_Test public void testContainsKey()
  • MapltString, BankAccountgt m
  • new TreeMapltString, BankAccountgt()
  • BankAccount acct1 new BankAccount("Jessica",
    500.00)
  • BankAccount acct2 new BankAccount("Alex",
    400.00)
  • BankAccount acct3 new BankAccount("Anthony",
    300.00)
  • BankAccount acct4 new BankAccount("Danny",
    200.00)
  • BankAccount acct5 new BankAccount("Patrick",
    100.00)
  • m.put(acct1.getID(), acct1)
  • m.put(acct2.getID(), acct2)
  • m.put(acct3.getID(), acct3)
  • m.put(acct4.getID(), acct4)
  • m.put(acct5.getID(), acct5)
  • BankAccount current m.get("Jessica")
  • assertEquals("Jessica", current.getID())

14
Demo other map ops
Write a Comment
User Comments (0)
About PowerShow.com