0% found this document useful (0 votes)
39 views22 pages

Collection Classes: Object Oriented Programming Using Java

The document discusses Java collection classes and frameworks. It describes that the Java collection framework provides interfaces like Set, List, Queue and classes like ArrayList, LinkedList, HashSet and TreeSet to store and manipulate groups of objects. It provides details about List interfaces and classes like ArrayList and LinkedList. It also discusses concepts like iterators, enumeration and properties class in Java.

Uploaded by

Ridham Vyas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
39 views22 pages

Collection Classes: Object Oriented Programming Using Java

The document discusses Java collection classes and frameworks. It describes that the Java collection framework provides interfaces like Set, List, Queue and classes like ArrayList, LinkedList, HashSet and TreeSet to store and manipulate groups of objects. It provides details about List interfaces and classes like ArrayList and LinkedList. It also discusses concepts like iterators, enumeration and properties class in Java.

Uploaded by

Ridham Vyas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

Collection Classes

OBJECT ORIENTED PROGRAMMING USING JAVA

LECTURER: Dr. KRUNAL PATEL


MBIT ENGGINEERING COLLEGE
NEW VALLABH VIDYANAGAR
DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT
Java.util package
• Java.util package contains the

• collections framework,
• legacy collection classes,
• event model,
• date and time facilities,
• internationalization, and
• miscellaneous utility classes.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Introduction
• Collections in java is a framework that provides an architecture to store and
manipulate the group of objects.

• All the operations that you perform on a data such as searching, sorting,
insertion, manipulation, deletion etc. can be performed by Java Collections.

• Java Collection simply means a single unit of objects.

• Java Collection framework provides many


• interfaces (Set, List, Queue, Deque etc.)
• classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT
Collection Hierarchy

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


List
• A List is an ordered Collection (sometimes called a sequence).
• Lists may contain duplicate elements.
• Elements can be inserted or accessed by their position in the list, using a zero-
based index.

Types of List:

1. ArrayList
2. LinkedList
3. Vector

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


AbstractList
• AbstractList class is an abstract class which extends the class AbstractCollection
and implements the interface List.
• In an unmodifiable list the programmers just required to extend the class
AbstractList class and allows the implementation for the methods get(int) method
and size() method i.e. these methods will be implemented into their appropriate
class.
• But in a modifiable list there will must be required to override the extra methods
additionally.
• AbstractList is a part of Java collection framework and is available
in java.util package.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


AbstractList

public abstract class AbstractList<E> extends AbstractCollection<E>


implements List<E>

protected AbstractList() : The only constructor of this class which is


implicitly invoked by the subclass constructor.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


AbstractList
Sr. Methods with Description
1 add() : public boolean add(E e)

2 clear() : public void clear()

3 iterator(): public Iterator<E> iterator()

4 subList(): public List<E> subList(int fromIndex, int toIndex)

5 remove(): public E remove(int index)

6 get(): public abstract E get(int index)

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


ArrayList class

• ArrayList class extends AbstractList class and implements the List interface.

• ArrayList supports dynamic array that can grow as needed.

• ArrayLists are created with an initial size, when this size is exceeded, it gets
enlarged automatically.

• It can contain Duplicate elements and maintains the insertion order.

• ArrayLists are not synchronized.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


ArrayList class
vArrayList has three constructors.
ArrayList()
ArrayList( Collection C )
ArrayList( int capacity )

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


LinkedList class
• LinkedList class extends AbstractSequentialList and implements List,
Deque and Queue interface.
• It can be used as List, stack or Queue as it implements all the related
interfaces.
• It can contain duplicate elements and is not synchronized.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Accessing a Collection
• To access, modify or remove any element from any collection we
need to first find the element, for which we have to cycle through the
elements of the collection.
• There are three possible ways to cycle through the elements of any
collection.

• Using Iterator interface


• Using ListIterator interface
• Using for-each loop

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Accessing elements using Iterator
• Iterator Interface is used to traverse a list in forward direction,
enabling you to remove or modify the elements of the collection.
Each collection classes provide iterator() method to return an iterator.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Accessing element using ListIterator
• ListIterator Interface is used to traverse a list in both forward and
backward direction. It is available to only those collections that
implement the List Interface.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Using for-each loop
• for-each version of for loop can also be used for traversing each
element of a collection.
• But this can only be used if we don't want to modify the contents of a
collection and we don't want any reverse access.
• for-each loop can cycle through any collection of object that
implements Iterable interface.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Enumeration
• The Enumeration interface defines the methods by which you can
enumerate (obtain one at a time) the elements in a collection of
objects.
• This legacy interface has been superseded by Iterator.
• Although not deprecated, Enumeration is considered obsolete for
new code.
• However, it is used by several methods defined by the legacy classes
such as Vector and Properties, is used by several other API classes,
and is currently in widespread use in application code.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Enumeration

Sr. Methods with Description


1 boolean hasMoreElements( )
When implemented, it must return true while there are still more elements to extract,
and false when all the elements have been enumerated.

2 Object nextElement( )
This returns the next object in the enumeration as a generic Object reference.

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Vector class
• Vector is similar to ArrayList which represents a dynamic array.
• The only difference between Vector and ArrayList is that Vector is
synchronised while Array is not.

• Vector class has following four constructor:


§ Vector()
§ Vector(int size)
§ Vector(int size, int incr)
§ Vector(Collection< ? extends E> c)

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Vector class
Method Description

addElement() ü add element to the Vector

elementAt() ü return the element at specified index

elements ü return an enumeration of element in vector

firstElement() ü return first element in the Vector

lastElement() ü return last element in the Vector

removeAllElement() ü remove all element of the Vector

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Properties class
• Properties is a subclass of Hashtable.
• It is used to maintain lists of values in which the key is a String and
the value is also a String.
• The Properties class is used by many other Java classes.
• Properties define the following instance variable.
• This variable holds a default property list associated with a Properties
object.

Properties defaults;

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Properties class

Sr. Constructors and Description

1 Properties( )
This constructor creates a Properties object that has no default values

2 Properties(Properties propDefault)
creates an object that uses propDefault for its default values. In both cases, the property list is
empty

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT


Properties class
Sr. Methods with Description
1 String getProperty(String key)
Returns the value associated with key. A null object is returned if key is neither in the list nor in the
default property list.

2 String getProperty(String key, String defaultProperty)


Returns the value associated with key. defaultProperty is returned if key is neither in the list nor in the
default property list.

3 Enumeration propertyNames( )
Returns an enumeration of the keys. This includes those keys found in the default property list, too.

4 Object setProperty(String key, String value)


Associates value with key. Returns the previous value associated with key, or returns null if no such
association exists

DR. KRUNAL N. PATEL(IT DEPARTMENT),MBIT

You might also like