View
159
Download
8
Category
Tags:
Preview:
DESCRIPTION
Collection and Generics in java explained briefly
Citation preview
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.
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
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
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
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
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>();
ArrayList example
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>();
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
LinkedList Example
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
HashSet example
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.
TreeSet example
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.
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
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
Iterator and ListIterator example
User Defined Classes in Collection
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.
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
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
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
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
Example for HashMap class
Any queries?
Thank you
Recommended