Collections and generics

256 views

Published on

Collection and Generics in java explained briefly

Published in: Engineering, Technology, Education
  • Be the first to comment

  • Be the first to like this

Collections and generics

  1. 1. Collections and Generics
  2. 2. 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.
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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>();
  8. 8. ArrayList example
  9. 9. 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>();
  10. 10. 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
  11. 11. LinkedList Example
  12. 12. 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
  13. 13. HashSet example
  14. 14. TreeSet Class • The TreeSet class implements the set interface, backed by a TreeMap instance. • TreeSet is an excellent choice when storing large amounts of sorted information that must be found quickly.
  15. 15. TreeSet example
  16. 16. 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.
  17. 17. 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
  18. 18. 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
  19. 19. Iterator and ListIterator example
  20. 20. User Defined Classes in Collection
  21. 21. 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.
  22. 22. 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
  23. 23. 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 this map 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
  24. 24. 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
  25. 25. 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
  26. 26. Example for HashMap class
  27. 27. Any queries?
  28. 28. Thank you

×