collections by kamalakar dandu

Upload: kamalakar-dandu

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Collections by Kamalakar Dandu

    1/14

    Collections Framework

  • 8/2/2019 Collections by Kamalakar Dandu

    2/14

    Benefits of a Collections Framework

    It reduces programming effort

    It increases program speed and quality

    It allows interoperability among unrelated APIs

    It reduces the effort to learn and use new APIs

    It reduces effort to design new APIs

    It fosters software reuse:

  • 8/2/2019 Collections by Kamalakar Dandu

    3/14

    Collection Interfaces

  • 8/2/2019 Collections by Kamalakar Dandu

    4/14

    Collection Interface

    public interface Collection {

    // Basic Operationsint size();boolean isEmpty();boolean contains(Object element);boolean add(Object element); // Optional

    boolean remove(Object element); // OptionalIterator iterator();// Bulk Operations

    boolean containsAll(Collection c);boolean addAll(Collection c); // Optionalboolean removeAll(Collection c); // Optionalboolean retainAll(Collection c); // Optionalvoid clear(); // Optional

    // Array OperationsObject[] toArray();Object[] toArray(Object a[]);

    }

  • 8/2/2019 Collections by Kamalakar Dandu

    5/14

    Iterators

    The object returned by the iterator method deserves specialmention. It is an Iterator, which is very similar to an Enumeration,

    but differs in two respects:

    1. Iterator allows the caller to remove elements from the underlyingcollection during the iteration with well-defined semantics.

    2. Method names have been improved.

    The first point is important: There was no safe way to removeelements from a collection while traversing it with an Enumeration.The semantics of this operation were ill-defined, and differed from

    implementation to implementation.public interface Iterator {

    boolean hasNext();Object next();

    void remove(); // Optional}

  • 8/2/2019 Collections by Kamalakar Dandu

    6/14

    Bulk Operations

    containsAll addAll

    removeAll retainAll

    clear

    Array Operations

    The toArray methods allow the contents of a Collection to be translated intoan array

    Object[] a = c.toArray();

    String[] a = (String[]) c.toArray(new String[0]);

  • 8/2/2019 Collections by Kamalakar Dandu

    7/14

    The Set Interface

    Cannot contain duplicate elementscontains nomethods other than those inherited from Collection

    adds a stronger contract on the behavior of the equals andhashCode operations, allowing Set objects with differentimplementation types to be compared meaningfully. Two Set

    objects are equal if they contain the same elements

    Here's a simple but useful Set idiom. Suppose you have aCollection, c, and you want to create another Collection containing

    the same elements, but with all duplicates eliminated. Thefollowing one-liner does the trick:

    Collection noDups = new HashSet(c);

  • 8/2/2019 Collections by Kamalakar Dandu

    8/14

    The List Interface

    public interface List extends Collection { // Positional AccessObject get(int index);Object set(int index, Object element); // Optionalvoid add(int index, Object element); // OptionalObject remove(int index); // Optional

    abstract boolean addAll(int index, Collection c); // Optional// Search

    int indexOf(Object o);int lastIndexOf(Object o);

    // IterationListIterator listIterator();ListIterator listIterator(int index); // Range-viewList subList(int from, int to);

    }

    Lists can containduplicate elements

  • 8/2/2019 Collections by Kamalakar Dandu

    9/14

    The Map Interfacepublic interface Map {

    // Basic OperationsObject 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();

    // Bulk Operationsvoid putAll(Map t);void clear();

    // Collection Viewspublic Set keySet();public Collection values();public Set entrySet();

    // Interface for entrySet elementspublic interface Entry {

    Object getKey();Object getValue();

    Object setValue(Object value);

  • 8/2/2019 Collections by Kamalakar Dandu

    10/14

    SortedSet Interface

    Range-view: Performs arbitrary range operationson the sorted set.

    Endpoints: Returns the first or last element in the sorted set.

    Comparator access: Returns the Comparator used to sort the set (if any).

    The SortedSet interface is shown below:

    public interface SortedSet extends Set {// Range-view

    SortedSet subSet(Object fromElement, Object toElement);SortedSet headSet(Object toElement);SortedSet tailSet(Object fromElement);

    // Endpoints

    Object first();Object last();

    // Comparator accessComparator comparator();

    }

  • 8/2/2019 Collections by Kamalakar Dandu

    11/14

    SortedMap Interface

    Range-view: Performs arbitrary range operationson the sorted map.

    Endpoints: Returns the first or last key in the sorted map.

    Comparator access: Returns the Comparator used to sort the map (if any).

    The SortedMap interface is shown below:

    public interface SortedMap extends Map {Comparator comparator();SortedMap subMap(Object fromKey, Object toKey);

    SortedMap headMap(Object toKey);SortedMap tailMap(Object fromKey);Object firstKey();Object lastKey();

    }

  • 8/2/2019 Collections by Kamalakar Dandu

    12/14

    General Purpose Implementations

    Implementations

    HashTable

    ResizableArray

    BalancedTree

    LinkedList

    Interface

    s

    Set HashSet TreeSet

    List ArrayList LinkedList

    Map

    HashMap TreeMap

    The general-purpose implementations are summarized in the table below.The table highlights their regular naming pattern: names are all of the form

    , where is the core collectioninterfaceimplemented by the class, and signifies the datastructure underlying the implementation.

  • 8/2/2019 Collections by Kamalakar Dandu

    13/14

    Exercise (contd)

    The number of data elements in thestudent marks details string is volatile,therefore store the StudentArray details in

    a dynamic collection object. Use a map setto store the details so that it would berelatively easy to search for records

  • 8/2/2019 Collections by Kamalakar Dandu

    14/14

    Exercise (contd)

    The number of data elements in thestudent marks details string is volatile,therefore store the StudentArray details in

    a dynamic collection object. Use a map setto store the details so that it would berelatively easy to search for records