M8 - Collections

Embed Size (px)

Citation preview

  • 8/10/2019 M8 - Collections

    1/15

    Module 8 Core Java API: Collections

    Java Course

  • 8/10/2019 M8 - Collections

    2/15

    2

    Module Objectives

    At the end of this module, participants will beable to: Identify the core Collections interfaces and

    their behavior. Use implementations of the various

    Collections interfaces.

  • 8/10/2019 M8 - Collections

    3/15

    3

    Overview of the Java Collections API

    The Java Collections API is a set of interfaces andimplementations included in the Java standard library. A Collection or a Container is an object that groups together

    multiple elements into a single unit. Each collection implements different storage, retrieval and

    manipulate behaviors. Found under the java.util package.

  • 8/10/2019 M8 - Collections

    4/15

    4

    The API contains Interfaces that specifies the abstractbehavior of each collection. The API contains concrete implementations of the interfaces. Each concrete implementation uses different algorithms and

    behavior.

    Overview of the Java Collections API (cont.)

  • 8/10/2019 M8 - Collections

    5/15

    5

    Overview of the Java Collections API (cont.)

    Collection

    Set List Queue

    Sorted Set

    Map

    Sorted Map

  • 8/10/2019 M8 - Collections

    6/15

    6

    Collection Interface

    Root of the collections hierarchy. The Collections interface represents general purpose behavior

    for the interfaces in the hierarchy. Does not have a concrete implementation. Used in order to obtain a general reference to any kind of

    Collection.

    ** Refer to the CollectionsSample.java sample code

  • 8/10/2019 M8 - Collections

    7/157

    Set Interface

    A Set is a Collection that does not allow duplicate entries. A Set Has the same methods as the Collection interface. Can contain at least 1 null element. Commonly used implementations are HashSet, LinkedHashSet

    and TreeSet. Elements in a HashSet and LinkedHashSet are not sorted. Elements in a Treeset are sorted.

    ** Refer to the SetSample.java sample code

  • 8/10/2019 M8 - Collections

    8/158

    List Interface

    A List is an ordered collection of elements. May contain duplicates. An elements indexing is similar to arrays, with the first element

    in index 0. Contains methods that allow manipulation of elements based on

    its position in the sequence. Common implementations are ArrayList and LinkedList.

    ** Refer to the ListSample.java sample code

  • 8/10/2019 M8 - Collections

    9/159

    Queue Interface

    A Queue is a collection for holding elements typically in a First-In-First-Out order. Has versions for a set of methods inherited from the Collections

    interface that return special values instead returning anexception when failing.

    ** Refer to the QueueSample.java sample code

    Throws Exception Returns special value

    Insert add() offer()

    Remove remove() poll()

    Examine element() peek()

  • 8/10/2019 M8 - Collections

    10/1510

    Map Interface

    A Map is a collection that pairs a key to an element contained inthe collection. The key and the element can be any Object. A key is assigned to an element when it is added to the map

    using the put(, ). The key is used to retrieve an element from the Map using the

    get() method.

    ** Refer to the MapSample.java sample code

  • 8/10/2019 M8 - Collections

    11/15

    11

    Ordering

    A collection can be sorted using the Collections.sort() method. Elements in a collection implement the Comparable interface

    which defines the sorting order. Elements that do not implement the Comparable interface

    cannot be sorted and will throw an exception.

  • 8/10/2019 M8 - Collections

    12/15

    12

    Comparable Interface

    The Comparable Interface can be implemented by any classthat wants to define its natural ordering. Classes that implement the Comparable interface will need to

    define the compareTo(Object) method which defines how aninstance of the class orders against the specified object.

    The compareTo(Object) method should return: 0 if the instance and the specified object are equal. A negative integer if the instance is less than the specified object. A positive integer if the instance is greater than the specified object.

  • 8/10/2019 M8 - Collections

    13/15

    13

    Comparator Interface

    The Comparator interface is similar to the Comparable interfaceexcept that it accepts two objects to be compared as parameters The Comparator interface can be implemented to compare two

    objects that are not Comparable Classes implementing the Comparator interface will need to

    override the compare(Object A, Object B) method to return thefollowing:

    0 if A and B are equal. A negative number if A is less than B.

    A positive number if A is greater than B.

    ** Refer to the Car.java and ComparableSample.java sample code

  • 8/10/2019 M8 - Collections

    14/15

    14

    Questions and Comments

  • 8/10/2019 M8 - Collections

    15/15

    15

    Activity1) Open your SEF workspace in Eclipse.2) In the package explorer, navigate to the

    following package sef.module9.activity.3) Review the interfaces Radar and

    RadarContact and note the requirementsof the interfaces.

    4) Complete the implementation ofRadarContactImpl based on theRadarContact interface.

    5) Test the implementation using theRadarContactTest JUnit driver.

    6) Complete the implementation of theDistanceComparator class. 7) Complete the implementation of

    RadarImpl based on the Radar interface.8) Test the implementation using RadarTest

    JUnit driver.