Core Java

Debasish Pratihari

Collection:


A collection is a generic term for any object that
represents a set of objects grouped together by
some means or mechanism inside the memory.



Collection Framework is a specification by which
we can store and manage objects of
heterogeneous kinds in memory.



 Collection Classes
are also called
Container classes

We can define collection as a java data structure
capable of storing & managing different kinds of
objects.



Note :

A collection has a capacity to grow dynamically.

Collection Framework Provides:


Interface



Implementation



Algorithms

Core Interfaces :

Collection

Set

List

Map

SortedMap

Queue

SortedSet

Lecture/core/collection/24

Page #1

feel the Technology…
Core Java

Debasish Pratihari

Types of Collection:


The way a collection (group of objects) is
implemented in memory, accordingly it is
categorized into following categories.

 Set
 List
 Map

Traversing Collections:


The Followings are the ways to traverse a
collection:





For-each loop
Iterator
ListItetor
Using Methods taking index as
Parameter

Collection Interfaces and Classes :
Interface Implementation
HashSet
Set
TreeSet
ArrayList
List
LinkedList
HashMap
Map
TreeMap

Lecture/core/collection/24

Historical

Vector
Stack
Hashtable
Properties

Page #2

feel the Technology…
Core Java

Debasish Pratihari

Collection Interface:
Methods
public
public
public
public
public
public
public
public
public











Iterator iterator ()
boolean add (Object obj)
boolean addAll (Collection other)
boolean remove (Object obj)
void clear( )
boolean isEmpty ( )
boolean contains (Object obj)
boolean containsAll (collection other)
int size ( )

Iterator interface :


Used for traverse a Collection in forward
direction.

Methods





public
public
public
public

boolean hasNext( )
Object next( )
Object next( )
Object remove ( )

ListIterator interface :


ListIterator interface in extended from Iterator
interface.
 In contrast to Iterator interface ListIterator
interface is used to traverse a collection in
forward or backward.
Method





public
public
public
public

Object next( )
 public boolean hasPrevious( )
boolean hasNext( )  public Object remove( )
Object remove( )
 public boolean add(Object obj)
Object previous( )  public boolean set(Object obj)

Lecture/core/collection/24

Page #3

feel the Technology…
Core Java

Debasish Pratihari

List Interface

Methods







public
public
public
public
public
public

ListIterator listIterator( )
void add(int index, Object obj)
void addAll(int i, Collection list)
Object remove(int indx)
Object set(int indx, Object obj)
int indexOf (Object obj)

Using Set :
25% 

Support basic operation of Collection Interface



Can’t contain duplicate elements.



Implementations





HasSet
TreeSet
LinkedHashSet

Example :
import java.util.*;
class SetDemo{
public static void main(String args[]){
HashSet <String>set= new HashSet<String>();
set.add("one");
set.add("two");
set.add("three");
set.add("four");
set.add("five");
System.out.println(set);
}
}

Lecture/core/collection/24

Page #4

feel the Technology…
Core Java

Debasish Pratihari

Using List :


Supports basic operations of Collection
Interface.



Facilitate positional access and Searching.



Supports range operation.



Implementations:



ArrayList
LinkedList

Example:

import java.util.*;
class ListDemo{
public static void main(String args[]){
LinkedList<Integer> list= new LinkedList<Integer>();

list.add(10);
list.add(20);
list.add(30);
System.out.println(list);
ListIterator itr= list.listIterator();
while(itr.hasNext())
System.out.println(itr.next());
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}

Lecture/core/collection/24

Page #5

feel the Technology…
Core Java

Debasish Pratihari

Using Map :


Elements are stored as key-value pair



A map can’t contain duplicate keys



Accept null as key value.



Implementations:




HashMap
TreeMap
LinkedHashMap

Example :
import java.util.*;
class MapDemo{
public static void main(String args[]){
HashMap<String,String> map
= new HashMap<String,String>();

map.put("one","java");
map.put("two","j2ee");
map.put("three","struts");
map.put("four","hibernate");
map.put("","EJB");
System.out.println(map);
}
}
HashTable vs HashMap :
HashTable
 Synchronized
 Doesn’t accept null
 Order is maintained

Lecture/core/collection/24

vs

HashMap




Un-synchronized
Accept null
No guarantee of
order

Page #6

feel the Technology…

Lecture 24

  • 1.
    Core Java Debasish Pratihari Collection:  Acollection is a generic term for any object that represents a set of objects grouped together by some means or mechanism inside the memory.  Collection Framework is a specification by which we can store and manage objects of heterogeneous kinds in memory.   Collection Classes are also called Container classes We can define collection as a java data structure capable of storing & managing different kinds of objects.  Note : A collection has a capacity to grow dynamically. Collection Framework Provides:  Interface  Implementation  Algorithms Core Interfaces : Collection Set List Map SortedMap Queue SortedSet Lecture/core/collection/24 Page #1 feel the Technology…
  • 2.
    Core Java Debasish Pratihari Typesof Collection:  The way a collection (group of objects) is implemented in memory, accordingly it is categorized into following categories.  Set  List  Map Traversing Collections:  The Followings are the ways to traverse a collection:     For-each loop Iterator ListItetor Using Methods taking index as Parameter Collection Interfaces and Classes : Interface Implementation HashSet Set TreeSet ArrayList List LinkedList HashMap Map TreeMap Lecture/core/collection/24 Historical Vector Stack Hashtable Properties Page #2 feel the Technology…
  • 3.
    Core Java Debasish Pratihari CollectionInterface: Methods public public public public public public public public public          Iterator iterator () boolean add (Object obj) boolean addAll (Collection other) boolean remove (Object obj) void clear( ) boolean isEmpty ( ) boolean contains (Object obj) boolean containsAll (collection other) int size ( ) Iterator interface :  Used for traverse a Collection in forward direction. Methods     public public public public boolean hasNext( ) Object next( ) Object next( ) Object remove ( ) ListIterator interface :  ListIterator interface in extended from Iterator interface.  In contrast to Iterator interface ListIterator interface is used to traverse a collection in forward or backward. Method     public public public public Object next( )  public boolean hasPrevious( ) boolean hasNext( )  public Object remove( ) Object remove( )  public boolean add(Object obj) Object previous( )  public boolean set(Object obj) Lecture/core/collection/24 Page #3 feel the Technology…
  • 4.
    Core Java Debasish Pratihari ListInterface Methods       public public public public public public ListIterator listIterator( ) void add(int index, Object obj) void addAll(int i, Collection list) Object remove(int indx) Object set(int indx, Object obj) int indexOf (Object obj) Using Set : 25%  Support basic operation of Collection Interface  Can’t contain duplicate elements.  Implementations    HasSet TreeSet LinkedHashSet Example : import java.util.*; class SetDemo{ public static void main(String args[]){ HashSet <String>set= new HashSet<String>(); set.add("one"); set.add("two"); set.add("three"); set.add("four"); set.add("five"); System.out.println(set); } } Lecture/core/collection/24 Page #4 feel the Technology…
  • 5.
    Core Java Debasish Pratihari UsingList :  Supports basic operations of Collection Interface.  Facilitate positional access and Searching.  Supports range operation.  Implementations:   ArrayList LinkedList Example: import java.util.*; class ListDemo{ public static void main(String args[]){ LinkedList<Integer> list= new LinkedList<Integer>(); list.add(10); list.add(20); list.add(30); System.out.println(list); ListIterator itr= list.listIterator(); while(itr.hasNext()) System.out.println(itr.next()); while(itr.hasPrevious()) System.out.println(itr.previous()); } } Lecture/core/collection/24 Page #5 feel the Technology…
  • 6.
    Core Java Debasish Pratihari UsingMap :  Elements are stored as key-value pair  A map can’t contain duplicate keys  Accept null as key value.  Implementations:    HashMap TreeMap LinkedHashMap Example : import java.util.*; class MapDemo{ public static void main(String args[]){ HashMap<String,String> map = new HashMap<String,String>(); map.put("one","java"); map.put("two","j2ee"); map.put("three","struts"); map.put("four","hibernate"); map.put("","EJB"); System.out.println(map); } } HashTable vs HashMap : HashTable  Synchronized  Doesn’t accept null  Order is maintained Lecture/core/collection/24 vs HashMap    Un-synchronized Accept null No guarantee of order Page #6 feel the Technology…