You are on page 1of 26

COLLECTION FRAMEWORK IN JAVA

INTRODUCTION TO COLLECTIONS
 A collection — sometimes called a container — is
simply an object that groups multiple elements into
a single unit. Collections are used to store, retrieve,
manipulate, and communicate aggregate data.
WHAT IS COLLECTION FRAMEWORK
 The Collection Framework provides a well-designed
set of interface and classes for sorting and
manipulating groups of data as a single unit, a
collection.
 The Collection Framework provides a standard
programming interface to many of the most common
abstractions, without burdening the programmer
with too many procedures and interfaces.
 The Collection Framework is made up of a set of
interfaces for working with the groups of objects.
The different interfaces describe the different types
of groups.
 The entire collections framework is designed around
a set of standard interfaces. Several standard
implementations such as LinkedList, HashSet, and
TreeSet.
 A collections framework is a unified architecture for
representing and manipulating collections.
 All collections frameworks contain the following:

1. Interface
2. Classes
3. Algorithms
 Interfaces: These are abstract data types that
represent collections. Interfaces allow collections to
be manipulated independently of the details of their
representation. In object-oriented languages,
interfaces generally form a hierarchy.
 Implementations i.e. Classes: These are the concrete
implementations of the collection interfaces. In
essence, they are reusable data structures.
 Algorithms: These are the methods that perform
useful computations, such as searching and sorting,
on objects that implement collection interfaces. The
algorithms are said to be polymorphic: that is, the
same method can be used on many different
implementations of the appropriate collection
interface.
THE COLLECTION INTERFACES
 The core collection interfaces encapsulate different
types of collections, which are shown in the figure
below.
 Core collection interfaces are the foundation of the
Java Collections Framework.
 As you can see in the following figure, the core
collection interfaces form a hierarchy.
 A Set is a special kind of Collection, a SortedSet is a
special kind of Set, and so forth. Note also that the
hierarchy consists of two distinct trees — a Map is not
a true Collection.
 Note that all the core collection interfaces are
generic. For example, this is the declaration of the
Collection interface.
public interface Collection<E>...
 The <E> syntax tells you that the interface is generic.
THE FOLLOWING LIST DESCRIBES THE CORE COLLECTION
INTERFACES:
 Collection : The root of the collection hierarchy. A
collection represents a group of objects known as its
elements. For more click Collection
 Set : A collection that cannot contain duplicate
elements. This interface models the mathematical set
abstraction and is used to represent sets, such as the
cards comprising a poker hand, the courses making up
a student's schedule, or the processes running on a
machine. For more click Set
 List : An ordered collection (sometimes called a
sequence). Lists can contain duplicate elements. For
more click List
 Queue : A collection used to hold multiple elements
prior to processing
 Map : An object that maps keys to values. A Map
cannot contain duplicate keys; each key can map to at
most one value.
 SortedSet : A Set that maintains its elements in
ascending order. For more click SortedSet
 SortedMap : A Map that maintains its mappings in
ascending key order. This is the Map analog of
SortedSet. Sorted maps are used for naturally
ordered collections of key/value pairs, such as
dictionaries and telephone directories.
THE COLLECTION CLASSES
 Java provides a set of standard collection classes that
implement Collection interfaces. Some of the classes
provide full implementations that can be used as-is
and others are abstract class, providing skeletal
implementations that are used as starting points for
creating concrete collections.
 The standard collection classes are summarized in the
given table ( Click Table)
STANDARD COLLECTION
CLASSES
 The ArrayList Class
 The LinkedList Class
 The HashSet Class
 The LinkedHashSet Class
 The TreeSet Class
THE COLLECTION ALGORITHMS
 The collections framework defines several algorithms
that can be applied to collections and maps. These
algorithms are defined as static methods within the
Collections class.
 Several of the methods can throw a
ClassCastException, which occurs when an attempt is
made to compare incompatible types, or an
UnsupportedOperationException, which occurs
when an attempt is made to modify an unmodifiable
collection.
 Collections defines three static variables: EMPTY_SET,
EMPTY_LIST, and EMPTY_MAP. All are immutable.
 The methods defined in collection framework's
algorithm are summarized in the table. (Click
Table)
MORE UTILITY CLASSES
StringTokenizer
 The processing of text often consists of parsing a
formatted input string.
 Parsing is the division of text into a set of discrete
parts, or tokens, which in a certain sequence can
convey a semantic meaning.
 The StringTokenizer class provides the first step in
this parsing process, often called the lexer (lexical
analyzer) or scanner.
 StringTokenizer implements the Enumeration
interface. Therefore, given an input string, you can
enumerate the individual tokens contained in it
using StringTokenizer.
 To use StringTokenizer, you specify an input string
and a string that contains delimiters.
 Delimiters are characters that separate tokens.
 Each character in the delimiters string is
considered a valid delimiter—for example, ",;:"
sets the delimiters to a comma, semicolon, and
colon.
 The default set of delimiters consists of the
whitespace characters: space, tab, newline, and
carriage return.
 The StringTokenizer constructors are shown here:
StringTokenizer(String str)
StringTokenizer(String str, String delimiters)
StringTokenizer(String str, String delimiters,
boolean delimAsToken)
 In all constructors, str is the string that will be
tokenized.
 In the first version, the default delimiters are used.
 In the second and third versions, delimiters is a string
that specifies the delimiters.
 In the third version, if delimAsToken is true, then the
delimiters are also returned as tokens when the string
is parsed. Otherwise, the delimiters are not returned. 
 Once you have created a StringTokenizer object, the
nextToken( ) method is used to extract consecutive
tokens.
 The hasMoreTokens( ) method returns true while
there are more tokens to be extracted.
 Since StringTokenizer implements Enumeration, the
hasMoreElements( ) and nextElement( ) methods
are also implemented, and they act the same as
hasMoreTokens( ) and nextToken( ), respectively.
BitSet Class
 A BitSet class creates a special type of array that
holds bit values.
 This array can increase in size as needed. This
makes it similar to a vector of bits.
 The BitSet constructors are shown here:
BitSet( )
BitSet(int size)
 The first constructor creates a default object.
 The second constructor allows you to specify its
initial size (that is, the number of bits that it can
hold).
 All bits are initialized to zero. BitSet implements the
Cloneable interface.
Date Class
 Date and time, An essential factor in a program for
implementing many concepts at real-time.
 Java provides a special class – date Class in the
java.util Package (sort of catch-all for handy
utilities).
 Date provides methods for examining and
manipulating date and time.
 Time in java is measured in milliseconds since
January 1, 1970.
 Java attempts to handle time from the system with
which it is interfacing.
 UTC is coordinated Universal Time, which seems to
be the worldwide standard.
 Note : In the date class, dates before January 1, 1970
are generally not usable.
Method and Description
UTC(int, int, int, int, int) : Calculates a UTC value from YMDHMS
after(Date) : check whether this date comes after the specified date
before(Date) : Checks whether this date comes before the specified
date
equals(Object) : Compares this object against specified object
getDate() : Returns the day of the month
getDay() : Returns the day of the week
getHours() : Returns the hour
getMinutes() : Returns the minute
getMonth() : Returns the month
getYear() : Returns the year after 1900
setDate(int) : Sets the date
toString() : Converts a date to string object, using UNIX conversions.
hashCode() : Compares a number that is used when storing objects
in hash tables.
Date Comparison:
 There are following three ways to compare two dates:

1. You can use getTime( ) to obtain the number of


milliseconds that have elapsed since midnight,
January 1, 1970, for both objects and then compare
these two values.
2. You can use the methods before( ), after( ), and
equals( ). Because the 12th of the month comes
before the 18th, for example, new Date(99, 2,
12).before(new Date (99, 2, 18)) returns true.
3. You can use the compareTo( ) method, which is
defined by the Comparable interface and
implemented by Date.
Date Formatting using SimpleDateFormat:
SimpleDateFormat is a concrete class for formatting
and parsing dates in a locale-sensitive manner.
 SimpleDateFormat allows you to start by choosing
any user-defined patterns for date-time formatting.

Simple DateFormat format codes:


 To specify the time format use a time pattern string.
 In this pattern, all ASCII letters are reserved as pattern
letters, which are defined as the following table:
Character Description Example
G Era designator AD
y Year in four digits 2001
M Month in year July or 07
d Day in month 10

h Hour in A.M./P.M. (1~12) 12

H Hour in day (0~23) 22


m Minute in hour 30
s Second in minute 55
S Millisecond 234
E Day in week Tuesday
D Day in year 360
2 (second Wed. in
F Day of week in month
July)
w Week in year 40
W Week in month 1
Character Description Example
a A.M./P.M. marker PM
k Hour in day (1~24) 24
K Hour in A.M./P.M. (0~11) 10
Eastern Standard
z Time zone
Time
' Escape for text Delimiter
" Single quote `
Date and Time Conversion Characters:
Character Description Example
Mon May 04 09:51:52 CDT
c Complete date and time
2009
F ISO 8601 date 2004-02-09
U.S. formatted date
D 02/09/2004
(month/day/year)
T 24-hour time 18:05:19
r 12-hour time 06:05:19 pm
R 24-hour time, no seconds 18:05
Four-digit year
Y 2004
(with leading zeroes)
Last two digits of the year
y 04
(with leading zeroes)
First two digits of the year
C 20
(with leading zeroes)
Character Description Example
n Two-digit month (with leading zeroes) 02

d Two-digit day (with leading zeroes) 03

e Two-digit day (without leading zeroes) 9

A Full weekday name Monday

a Abbreviated weekday name Mon

P Uppercase morning or afternoon marker PM

p Lowercase morning or afternoon marker Pm

B Full month name February

b Abbreviated month name Feb

You might also like