2. SYLLABUS
⢠Java.util.package
â˘The collections framework(Interfaces in the collections framework).
â˘Traversing collections with iterators
â˘General purpose implementations
â˘Arrays as collections
3. JAVA.UTIL.PACKAGE
ď§ Java.util package contains the collections framework, legacy
collection classes, event model, date and time facilities,
internationalization, and miscellaneous utility classes.
Following are the Important Classes in Java.util package :
ď§AbstractCollection: This class provides a skeletal implementation of
the Collection interface, to minimize the effort required to implement
this interface.
ď§AbstractList: This class provides a skeletal implementation of the List
interface to minimize the effort required to implement this interface
backed by a ârandom accessâ data store (such as an array).
ď§AbstractMap<K,V>: This class provides a skeletal implementation of
the Map interface, to minimize the effort required to implement this
4. CONTâŚ
ď§AbstractMap.SimpleEntry<K,V>: An Entry maintaining a key and a value.
ď§AbstractMap.SimpleImmutableEntry<K,V>: An Entry maintaining an
immutable key and value.
ď§AbstractQueue: This class provides skeletal implementations of some Queue
operations.
ď§AbstractSequentialList: This class provides a skeletal implementation of the
List interface to minimize the effort required to implement this interface
backed by a âsequential accessâ data store (such as a linked list).
ď§AbstractSet: This class provides a skeletal implementation of the Set
interface to minimize the effort required to implement this interface.
ď§ArrayDeque: Resizable-array implementation of the Deque interface.
ď§ArrayList: Resizable-array implementation of the List interface.
ď§Arrays: This class contains various methods for manipulating arrays (such as
sorting and searching).
5. CONTâŚ
ď§BitSet: This class implements a vector of bits that grows as needed.
Calendar: The Calendar class is an abstract class that provides methods for
converting between a specific instant in time and a set of calendar fields
such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for
manipulating the calendar fields, such as getting the date of the next week.
Collections: This class consists exclusively of static methods that operate on
or return collections.
Currency: Represents a currency.
ď§Date: The class Date represents a specific instant in time, with millisecond
precision.
ď§Dictionary<K,V>: The Dictionary class is the abstract parent of any class,
such as Hashtable, which maps keys to values.
ď§EnumMap,V>: A specialized Map implementation for use with enum type
keys.
ď§EnumSet: A specialized Set implementation for use with enum types.
6. THE COLLECTIONS
FRAMEWORK(INTERFACES IN THE
COLLECTIONS FRAMEWORK)
The Collection in Java is a framework that provides an architecture to store
and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a data
such as searching, sorting, insertion, manipulation, and deletion.
Java Collection means a single unit of objects.
Java Collection framework provides many interfaces (Set, List, Queue, Deque)
and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet).
A Collection represents a single unit of objects, i.e., a group.
The Collection framework represents a unified architecture for storing and
manipulating a group of objects. It has:
Interfaces and its implementations, i.e., classes
Algorithm
8. TRAVERSING COLLECTIONS WITH
ITERATORS
Iterator enables you to cycle through a collection, obtaining or removing
elements. ListIterator extends Iterator to allow bidirectional traversal of a
list, and the modification of elements.
Before you can access a collection through an iterator, you must obtain one.
Each of the collection classes provides an iterator( ) method that returns an
iterator to the start of the collection. By using this iterator object, you can
access each element in the collection, one element at a time.
In general, to use an iterator to cycle through the contents of a collection,
follow these steps â
Obtain an iterator to the start of the collection by calling the collection's
iterator( ) method.
Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as
hasNext( ) returns true.
Within the loop, obtain each element by calling next( ).
For collections that implement List, you can also obtain an iterator by calling
ListIterator.
9. THE METHODS DECLARED BY
ITERATOR
Sr.No. Method & Description
1 boolean hasNext( )
Returns true if there are more elements. Otherwise, returns false.
2 Object next( )
Returns the next element. Throws NoSuchElementException if there is not a next
element.
3 void remove( )
Removes the current element. Throws IllegalStateException if an attempt is made to
call remove( ) that is not preceded by a call to next( ).
10. GENERAL PURPOSE
IMPLEMENTATIONS
Implementations are the data objects used to store collections, which
implement the interfaces described in the Interfaces lesson.
The Java Collections Framework provides several general-purpose
implementations of the core interfaces:
For the Set interface, HashSet is the most commonly used implementation.
For the List interface, ArrayList is the most commonly used implementation.
For the Map interface, HashMap is the most commonly used implementation.
For the Queue interface, LinkedList is the most commonly used
implementation.
For the Deque interface, ArrayDeque is the most commonly used
implementation.
Each of the general-purpose implementations provides all optional
operations contained in its interface.
11. ARRAYS AS COLLECTIONS
Arrays Collections
1. Size is fixed 1. Size is not fixed, size is growable
2. Can hold both primitive
types(boolean, byte, short, int, long
âŚetc) and object types.
2. Can hold only object types
3. There is no underlying data
structures in arrays. The array itself
used as data structure in java.
3. Every Collection class there is
underlying data structure.
4. There is no utility methods in
arrays
4. Every Collection provides utility
methods
12. WHY COLLECTIONS ?
Arrays are fixed in size, in development always itâs not possible to
define size of an array. We need to increase size based on
requirement. Collections are not fixed in size, they are growable (size
will be increased when itâs reached max size).
Arrays can only holds one type of elements. Collections can hold
different type of elements.
There is no default method support from arrays for sorting,
searching, retrieving etc⌠but collections have default utility method
support, itâs handy for for developer. It will reduce the coding time.
13. WHAT IS AN ARRAY IN JAVA ?
An Array is collection of indexed and fixed number of homogeneous (same
type) elements.
Indexed : Arrays are stored elements in index based. The stored element
position starts from âzeroâ and second element position â1Ⲡand so on.
For example if we take String array to represent Student names [âSatishâ,
âSunilâ, âSureshâ, âRaviâ] position of âSatishâ element is â0â , position of âSunilâ
is 1, position of âRaviâ is 3.
Fixed : When we are defining array we have to specify the size of array.
Example : String[] students = new String[10]; . Once we created an Array we
canât increase the size, size is fixed in arrays.
Homogeneous : Arrays elements are homogeneous, it means once we
created array we can store same type of elements. For example in point 4
student array allow to store only String type elements, because the array
type is String.
14. WHAT IS THE DIFFERENCE BETWEEN
ARRAY AND COLLECTIONS IN JAVA?
Collections are dynamically growable nature,if we add extra element over its size, JVM will not raise
any exception, where as array size is fixed, so it raises ArrayIndexOutofBound exception when you try
to add more than the size.
Collection allows heterogeneous elements where as array allows homogeneous, so when we try to add
different type, it raises error as incompatible type .
Collection are having predefined library to perform operations like sorting or so, where as array
doesn't so developer has to provide own logic.
Array can hold both primitive as well as objects where as collection stores and manages objects alone.
Collections are more flexible than arrays.
Collections are more API dependant (testing and debugging is difficult) where as arrays are less API
dependant
Collections are said to be heavy weight and arrays are lightweight.
When we know the number and data type of elements before hand we can opt arrays where as
collections can be used at any time.
Collections reduces the typedness (due to different type of data) where as arrays improve typedness (
similar type of data).