KEMBAR78
Collections in java in detail for easy understanding | PPTX
Collections in java
Definition
• A collection in java is represented as a
container that groups all the elements
into a single unit.
• For example, a mail folder (group of
emails), a telephone directory (mapping of
names to phone numbers).
• A framework is a basic foundation or layout
on which you start working by using the
different classes and interfaces provided.
Collections Framework in Java
• All objects are grouped into a single object along with an architecture that
represents and provides different methods for manipulating collections.
• So Collections framework in Java provides different data structures already
implemented for storing data and methods, to manipulate them with features such
as sorting, searching, deletion, and insertion.
• import java.util.*;
Benefits of Collections Framework in Java
It has the following benefits.
1.Reusability
2.Already Implemented (time-saving).
3.Performance Efficiency (speed and quality).
4.Reduces effort to learn and use new APIs.
Hierarchy of Collection Framework
Each collection class implements an interface from a hierarchy. Each
class is designed for a specific type of storage.
1.Interfaces
2.Classes (implementation)
3.Algorithms
• Abstract implementations -
Partial implementations of the collection interfaces to facilitate custom implementations
such as:
AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap.
• Algorithms -
Static methods that perform useful functions on collections, such as sorting a list.
The collections framework consists of:
• Collection interfaces
These interfaces represent different types of collections, such as Set, List, and Map. These
interfaces form the basis of the framework.
• General-purpose implementations
These are primary implementations of the collection interfaces such
as ArrayList, LinkedList, HashSet, LinkedHashSet, HashMap etc.
• Legacy implementations
The collection classes from earlier releases, Vector and Hashtable, were retrofitted to
implement the collection interfaces.
Collection Interfaces
• Collection Interface
• Iterable Interface
• Queue Interface
• Set Interface
• List Interface
• Deque Interface
• Map Interface
Collection Interfaces
• Present in java.util.package to define several utility methods like sorting,
searching for collection objects. E.g. Collections.Sort(x);
interface Collection<E>
Common Methods of these Interfaces
Methods Description
public boolean add(E e)
Used to insert an element into the
collection
public boolean remove(Object element)
Used to remove an element from the
collection
public int size()
Returns the number of elements in a
collection
public boolean contains(Object element) Used to search for an element
public boolean isEmpty() Checks if the collection is empty
public boolean equals(Object element) Checks for equality
Collection Interfaces
Interface Description
Collection Enables you to work with groups of objects; it is at the top of the collections
hierarchy.
Deque Extends Queue to handle a double-ended queue.
List Extends Collection to handle sequences (lists of objects).
NavigableSet Extends SortedSet to handle retrieval of elements based on closest-match
searches.
Queue Extends Collection to handle special types of lists in which elements are
removed only from the head.
Set Extends Collection to handle sets, which must contain unique elements.
SortedSet Extends Set to handle sorted sets.
List
• Child interface of collection.
• Extends Collection to handle sequences (lists of objects).
interface List<E>
• E specifies the type of objects that list will hold.
• Duplicates are allowed and insertion order preserved.
Lists and Sets
A list is a collection that maintains the order of its elements.
• Ordered Lists
• ArrayList
• Stores a list of items in a dynamically sized array
• LinkedList
• Allows speedy insertion and removal of items from the list
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 14
Set
• Extends Collection to handle sets, which must contain unique elements.
interface Set<E>
• Duplicates are not allowed and insertion order not preserved.
• Therefore, add() method returns false if an attempt to made to add duplicate
elements.
Lists and Sets
A set is an unordered collection of unique elements.
• Unordered Sets
• HashSet
• Uses hash tables to speed up finding, adding, and removing elements
• TreeSet
• Uses a binary tree to speed up finding, adding, and removing elements
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 16
SortedSet Interface
• Extends Set to handle sorted sets.
interface SortedSet<E>
• Duplicates are allowed but all objects should be inserted according to some
sorting order.
Stacks and Queues
• Another way of gaining efficiency in a collection is to reduce the number of operations
available
• Two examples are:
• Stack
• Remembers the order of its elements, but it does not allow you to insert
elements in every position
• You can only add and remove elements at the top
• Queue
• Add items to one end (the tail)
• Remove them from the other end (the head)
• Example: A line of people waiting for a bank teller
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 18
Collection Classes
• ArrayList
• LinkedList
• Abstarct List
• Hashset
• Linked HashSet
• ArrayDeque
• HashMap
Algorithms
1.Sorting
2.Searching
3.Shuffling
4.Routine Data Manipulation
5.Composition
6.Finding Extreme Values

Collections in java in detail for easy understanding

  • 1.
  • 2.
    Definition • A collectionin java is represented as a container that groups all the elements into a single unit. • For example, a mail folder (group of emails), a telephone directory (mapping of names to phone numbers). • A framework is a basic foundation or layout on which you start working by using the different classes and interfaces provided.
  • 3.
    Collections Framework inJava • All objects are grouped into a single object along with an architecture that represents and provides different methods for manipulating collections. • So Collections framework in Java provides different data structures already implemented for storing data and methods, to manipulate them with features such as sorting, searching, deletion, and insertion. • import java.util.*;
  • 4.
    Benefits of CollectionsFramework in Java It has the following benefits. 1.Reusability 2.Already Implemented (time-saving). 3.Performance Efficiency (speed and quality). 4.Reduces effort to learn and use new APIs.
  • 5.
    Hierarchy of CollectionFramework Each collection class implements an interface from a hierarchy. Each class is designed for a specific type of storage. 1.Interfaces 2.Classes (implementation) 3.Algorithms
  • 7.
    • Abstract implementations- Partial implementations of the collection interfaces to facilitate custom implementations such as: AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap. • Algorithms - Static methods that perform useful functions on collections, such as sorting a list.
  • 8.
    The collections frameworkconsists of: • Collection interfaces These interfaces represent different types of collections, such as Set, List, and Map. These interfaces form the basis of the framework. • General-purpose implementations These are primary implementations of the collection interfaces such as ArrayList, LinkedList, HashSet, LinkedHashSet, HashMap etc. • Legacy implementations The collection classes from earlier releases, Vector and Hashtable, were retrofitted to implement the collection interfaces.
  • 9.
    Collection Interfaces • CollectionInterface • Iterable Interface • Queue Interface • Set Interface • List Interface • Deque Interface • Map Interface
  • 10.
    Collection Interfaces • Presentin java.util.package to define several utility methods like sorting, searching for collection objects. E.g. Collections.Sort(x); interface Collection<E>
  • 11.
    Common Methods ofthese Interfaces Methods Description public boolean add(E e) Used to insert an element into the collection public boolean remove(Object element) Used to remove an element from the collection public int size() Returns the number of elements in a collection public boolean contains(Object element) Used to search for an element public boolean isEmpty() Checks if the collection is empty public boolean equals(Object element) Checks for equality
  • 12.
    Collection Interfaces Interface Description CollectionEnables you to work with groups of objects; it is at the top of the collections hierarchy. Deque Extends Queue to handle a double-ended queue. List Extends Collection to handle sequences (lists of objects). NavigableSet Extends SortedSet to handle retrieval of elements based on closest-match searches. Queue Extends Collection to handle special types of lists in which elements are removed only from the head. Set Extends Collection to handle sets, which must contain unique elements. SortedSet Extends Set to handle sorted sets.
  • 13.
    List • Child interfaceof collection. • Extends Collection to handle sequences (lists of objects). interface List<E> • E specifies the type of objects that list will hold. • Duplicates are allowed and insertion order preserved.
  • 14.
    Lists and Sets Alist is a collection that maintains the order of its elements. • Ordered Lists • ArrayList • Stores a list of items in a dynamically sized array • LinkedList • Allows speedy insertion and removal of items from the list Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 14
  • 15.
    Set • Extends Collectionto handle sets, which must contain unique elements. interface Set<E> • Duplicates are not allowed and insertion order not preserved. • Therefore, add() method returns false if an attempt to made to add duplicate elements.
  • 16.
    Lists and Sets Aset is an unordered collection of unique elements. • Unordered Sets • HashSet • Uses hash tables to speed up finding, adding, and removing elements • TreeSet • Uses a binary tree to speed up finding, adding, and removing elements Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 16
  • 17.
    SortedSet Interface • ExtendsSet to handle sorted sets. interface SortedSet<E> • Duplicates are allowed but all objects should be inserted according to some sorting order.
  • 18.
    Stacks and Queues •Another way of gaining efficiency in a collection is to reduce the number of operations available • Two examples are: • Stack • Remembers the order of its elements, but it does not allow you to insert elements in every position • You can only add and remove elements at the top • Queue • Add items to one end (the tail) • Remove them from the other end (the head) • Example: A line of people waiting for a bank teller Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 18
  • 19.
    Collection Classes • ArrayList •LinkedList • Abstarct List • Hashset • Linked HashSet • ArrayDeque • HashMap
  • 20.