collections and generics

28
Collections and Generics

Upload: muthukumaran-subramanian

Post on 11-Nov-2014

159 views

Category:

Engineering


8 download

DESCRIPTION

Collection and Generics in java explained briefly

TRANSCRIPT

Page 1: Collections and generics

Collections and Generics

Page 2: Collections and generics

Collection framework Overview

• The collection is an object that represents a group of objects.

• A collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently.

Page 3: Collections and generics

Cont’d

The collection framework consists of

• Collection interfaces

• General purpose implementations

• Legacy implementations

• Special purpose implementations

• Concurrent implementations

• Wrapper implementations

• Convenience implementations

• Abstract implementations

• Algorithms

• Infrastructure

• Array utilities

Page 4: Collections and generics

Advantages of collection framework

• Reduces programming effort

• Increases performance

• Provides interoperability between unrelated APIs

• Reduces the effort required to learn APIs

• Reduces the effort required to design and implement APIs

• Fosters software reuse

Page 5: Collections and generics

Collection interfaces

• The collection interface is the primary which is implemented by all the collection classes.

• There are totally 9 collection interfaces.

• Some of them are• List interface

• Set interface

• Sortedset interface

Page 6: Collections and generics

Collection classes

Class Description

AbstractCollection Implements most of the collections interface

AbstractList Extends AbstractCollection and implements most of the List interface

AbstractSequentialList Extends AbstractList for use by a collection that uses sequential rather than random access of its elements

LinkedList Implements a linked list by extending AbstractSequentialList

ArrayList Implements a dynamic array by extending AbstractList

AbstractSet Extends AnstractCollection and Implements most of the Set interfaces

HashSet Extends AbstractSet for use with a hash table

TreeSet Implements a set stored in a tree. Extends AbstractSet

Page 7: Collections and generics

ArrayList

• The ArrayList class extends AbstractList and implements the List interface.

• The constructors provided by the ArrayList class is• ArrayList() – empty constructor

• ArrayList(Collection c) - initialized with the elements of the collection c

• ArrayList(int capacity) – specifying the initial capacity

• ArrayList<String> stringList = new ArrayList<String>();

Page 8: Collections and generics

ArrayList example

Page 9: Collections and generics

LinkedList

• LinkedList class implements the List interface.

• The LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list.

• LinkedList also can be used as stack, queue, double ended queue using these operations.

• The LinkedList() and LinkedList(collection c) are the constructors used in this class.

• LinkedList<type> obj = new LinkedList<type>();

Page 10: Collections and generics

Methods used in LinkedList

Method Description

Void addFirst(Object ob) To add elements to the start of the list

Void addLast(Object ob) To add elements to the end of the list

Object getFirst() To obtain the first element

Object getLast() To obtain the last element

Object removeFirst() To remove the first element in the list

Object removeLast() To remove the last element in the list

Page 11: Collections and generics

LinkedList Example

Page 12: Collections and generics

HashSet Class

• This class implements the set interface, backed by a hash table.

• It provides constant time performance for operations like add, remove, contains and size

Page 13: Collections and generics

HashSet example

Page 14: Collections and generics

TreeSet Class

• The TreeSet class implements the set interface, backed by a TreeMapinstance.

• TreeSet is an excellent choice when storing large amounts of sorted information that must be found quickly.

Page 15: Collections and generics

TreeSet example

Page 16: Collections and generics

Iterator and ListIterator interface

• The iterator enables you to cycle through a collection obtaining or removing elements.

• The ListIterator extends Iterator to allow multidirectional traversal of a list and the modification of the element.

Page 17: Collections and generics

Methods of iterator interface

Method Description

Boolean hasNext() Returns true if the iteration has more elements

Object next() Returns the next element in the iteration

Void remove() Removes the underlying collection the last element returned by the iterator

Page 18: Collections and generics

Methods of ListIterator interface

Method Description

Void add(Object e) Inserts the specified element into the list

Boolean hasNext() Returns true if this list iterator has more elements when traversing the list in the forward direction

Boolean hasPrevious() Returns true if this list iterator has more elements when traversing the list in the reverse direction

Object next() Returns the next element in the list

Int nextIndex() Returns the index of the element that would be returned by a subsequent call to next

Object previous() Returns the previous element in the list

Int previousIndex() Returns the index of the element that would e returned by a subsequent call to previous

Void remove() Removes from the list the last element that was returned by next or previous

Void set(Object o) Replaces the last element returned by next or previous with the specified element

Page 19: Collections and generics

Iterator and ListIterator example

Page 20: Collections and generics

User Defined Classes in Collection

Page 21: Collections and generics

Random Access Interface

• The RandomAccess interface identifies List implementations which are faster to iterate using the List.get() method rather than using the Iterator.next() method.

Page 22: Collections and generics

Working with maps

• Map interface:• Map interface is used to map keys to value.

• A map cannot contain duplicate keys, each key can map to at most one value.

• A key is an object that is used to retrieve a value later.

• Given a key and a value, one can store the value in a map object.

• The value can be retrieved using the key.

• The get() and put() are two basic operations in map interface

• The put() is used to put a value into the map

• The get() is used to obtain a value passing the key as an argument

Page 23: Collections and generics

Methods provided by map interface

Method Description

Void clear() Removes all mappings from this map

Boolean containsKey(Object key) Returns true if the map contains a mapping for the specified key

Boolean containsValue(Object key) Returns true if this map maps one or more keys to the specified value.

Set entrySet() Returns a set view of the mappings contained in thismap

Boolean equals(Object o) Compares the specified object with this map for equality

Object get(Object key) Returns the value to which this map mpas the specified key

Page 24: Collections and generics

Cont’d

Method Description

Int hashCode() Returns the hash code value for this map

Boolean isEmpty() Returns true if this map contains no key-value mappings

Set keyset() Returns a set view of the keys contained in this map

Object put(Object key, Object value) Associates the specified value with the specified key in this map

Void putAll(Map t) Copies all of the mappings from the specified map to this map

Object remove(Object key) Removes the mappings for this key from this map if it is present

Int size() Returns number of key-value mappings in this map

Collection values() Returns a collection view of the values contained in the map

Page 25: Collections and generics

HashMap class

• HashMap class is used for hash table based implementation of the map interface.

• This implementation provides all the optional map operations and permits null values and the null key.

• The HashMap class has two parameters• Capacity – the capacity is the number of buckets in the hash table

• Load factor – is the measure of how full the hash table is allowed to get before its capacity is automatically increased

Page 26: Collections and generics

Example for HashMap class

Page 27: Collections and generics

Any queries?

Page 28: Collections and generics

Thank you