0% found this document useful (0 votes)
0 views

Collections

The document provides an overview of the Java Collections Framework, detailing its purpose, structure, and advantages. It explains various collection types such as List, Set, and Map, along with their implementations like ArrayList, HashSet, and HashMap. Additionally, it covers key concepts such as iterators, the Comparable interface, and the differences between collection types.

Uploaded by

saikumarvenkat60
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Collections

The document provides an overview of the Java Collections Framework, detailing its purpose, structure, and advantages. It explains various collection types such as List, Set, and Map, along with their implementations like ArrayList, HashSet, and HashMap. Additionally, it covers key concepts such as iterators, the Comparable interface, and the differences between collection types.

Uploaded by

saikumarvenkat60
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

COLLECTIONS

R.S NANDHINI
WHAT IS A FRAMEWORK
• A framework is a set of classes and interfaces which provide a readymade
architecture.
• An optimal object-oriented design always includes a framework with a collection
of classes such that all the classes perform the same kind of task.
• java.util – contains a large assortment of classes and interfaces.
• Generate pseudo random numbers
• Manage date and time
• Manipulate sets of bits
• Handle formatted data
• Utility class – random, scanner
• They also contain collection framework
COLLECTIONS IN
JAVA/COLLECTIONS FRAMEWORK
• The Java collections framework provides a set of interfaces and classes to
implement various data structures and algorithms.
• Java collections can achieve all the operations that you perform on a data such as
searching, sorting, insertion, manipulation and deletion.
WHAT IS COLLECTION IN JAVA
• A collection is a group of objects.
• Collections framework provide a set of standard utility classes to manage
collections .
• Collections framework consists of three parts:
• Core Interfaces
• Concrete Implementation
• Algorithms such as searching and sorting
HIERARCHY OF COLLECTION
FRAMEWORK
ADVANTAGES OF COLLECTIONS
• Reduces programming effort: by providing useful data structures and algorithms
so you don’t have to write them yourself.
• Increases performance: by providing high-performance implementation of
useful data structures and algorithms. Because the various implementations of
each interface are interchangeable, programs can be easily tuned by switching
implementations.
• Provides interoperability between unrelated APIs: by establishing a common
language to pass collections back and forth.
• Reduces the effort required to learn APIs: by eliminating the need to learn
multiple ad hoc collection APIs.
• Reduces the effort required to design and implement APIs: by eliminating the
need to produce ad hoc collections APIs.
• Fosters software reuse: by providing a standard interface for collections and
algorithms to manipulate them.
COLLECTIONS - LIST
LIST
• List interface extends from collection interface.
• It stores elements in a sequential manner.
• Elements in the list can be accessed or inserted based on their position
• Starts with zero based index.
• Can contain duplicate elements
• An Iterator can be used to access the elements of the List
LIST INTERFACE
• To store the ordered collection of objects. It can have duplicate values.
• List interface is implemented by the classes ArrayList, LinkedList, Vector and
Stack.
• Example:
• List<data-type> list1 = new ArrayList();
• List<data-type> list2 = new LinkedList();
• List<data-type> list3 = new Vector();
ArrayList
• ArrayList class implements List interface
• It supports dynamic array that can grow dynamically
• Standard arrays are of fixed size. After arrays are created they cannot grow or
shrink.
• It provides more powerful insertion and search mechanisms than arrays
• Gives faster Iterator and fast random access
• Ordered collection (by index), but not Sorted.
JAVA ITERATORS
• To access every item in a collection of items – we call this traversing or iterating.
• Example: array for
(int i=0; i<array.length; i++)
// lines of code involving array a[i]
• Java provides an interface for stepping through all elements in any collection,
called an iterator.
ITERATING THROUGH AN ArrayList
• Iterating through an ArrayList – Iterating through an ArrayList of Strings:
For(int i=0; i<list.size; i++)
{
String s = list.get(i); //lines of code with s
}
Alternative:
Iterator<String> itr = list.iterator();
While(itr.hasNext())
{
String s=list.next();
}
ITERATOR INTERFACE
• Iterator<T>: a generic interface with the following methods.
• public booleanhasNext(); returns true if there are more elemets to iterate over
• public T next(); returns the next element
• throws a NoSuchElement Exception if a next element does not exist.
• public void remove(); removes the last element returned by the iterator(optional
operator)
• It is in the java.util package
ArrayList
LinkedList
• LinkedList implements the collection interface.
• It uses a doubly linked list internally to store the elements.
• It can store the duplicate elements. It maintains the insertion order and is not
synchronized.
• In LinkedList, the manipulation is fast because no shifting is required.
COLLECTIONS - SET
SET
• Set interface extends from collection interface
• Set is an unordered collection
• It doesn’t allow duplicates
• If you add duplicates, it will replace the existing one
• It allows you to add only a single null value
• An iterator can be used to traverse through the list
• List interface has one legacy class called Vector, but Set doesn’t have any
legacy class.
• Implementation classes for Set are TressSet, HashSet and LinkedHashSet
DIFFERENCE BETWEEN LIST AND SET
• A list can contain duplicate elements whereas Set contains unique elements only.
TreeSet
• No duplicates are allowed
• Iterates in sorted order
• Sorted Collection: By default elements will be in ascending order
• Not synchronized: If more than one thread wants to access it at the same time then it must be
synchronized externally

• TreeSet implements the Set interface, backed by a TreeMap instance. This class guarantees that
the sorted set will be in ascending element order, sorted according to the natural order of the
elements or by the comparator provided at set creation time, depending on which constructor is
used.
COMPARABLE INTERFACE
• This interface can be used to order the user defined objects.
• It is found in java.lang package
• The method found in Comparable interface is
• public int compareTo(Object obj)
• This method will compare the current object with the previous object
OUTPUT
The HashSet Class
• No duplicates are allowed
• A Hashset is an unsorted, unordered set
• Can be used when you want a collection with no duplicates and you don’t care
about the order when you iterate through it
• Uses HashTable for storage
• Remember that Sets are used when you don’t want any duplicates in your
collection. If you attempt to add an element to a set that already exists in the set,
the duplicate elements will not be added, and the add() method will return false.
• HashSets tend to be very fast because they use hashcodes.
HashSet
• To create a collection that uses a hash table for storage.
• It inherits the AbstractSet class and implements set interface.
• It stores the elements by using a mechanism called hashing.
• It contains unique elements only.
• It allows null value.
• HashSet class is non synchronized.
• It doesn’t maintain the insertion order. Here, elements are inserted on the basis
of their hashcode.
• It is the best approach for search operations.
HashSet EXAMPLE IGNORING
DUPLICATE ELEMENTS
COLLECTIONS - MAP
JAVA COLLECTIONS - MAP
• A Map is an object that maps keys to values.
• A map cannot contain duplicate keys.
• There are three main implementations of Map interfaces: HashMap,
TreeMap and LinkedHashMap.
• HashMap: it makes no guarantees concerning the order of iteration.
• TreeMap: It stores its elements in a red-black tree, orders its elements
based on their values; it is substantially slower than HashMap.
• LinkedHashMap: It orders its elements based on the order in which
they were inserted into the set(insertion-order).
HashMap
• HashMap is a Map based collection class that is used for storing key &
value pairs.
• It is denoted as HashMap<Key, Value> or HashMap<K, V>.
• This class makes no guarantees as to the order of the map.
• It is similar to the Hashtable class except that it is unsynchronized and
permits nulls (null value and null key).
HashMap
• It is not an ordered collection.
• Which means it does not return the keys and values in the same order
in which they have been inserted into the HashMap.
• It does not sort the stored keys and values.
• Import java.util.HashMap or its super class in order to use the
HashMap class and methods.
MAP INTERFACE - SUMMARY
• Map is an interface that stores data in the form of key-value pair.
• All the keys in the map will be unique
• We can retrieve the value stored in a map by providing the key value
• A map cannot contain duplicate values
• Each key can map to at most one value
• For basic operations it uses the following methods
• put() – for adding elements
• Get() – for retrieving an element
• Remove() – to remove an element
• Size() – to check the size of the collection
THE HashMap class
• HashMap uses the hashcode value of an object to determine how the object
should be stored in the collection.
• Hashcode is used again to help locate the object in the collection
• Gives you an unsorted and unordered map
• Allows only one null key and multiple null values in a collection
• HashMap are not synchronized.
• The program first populates the HashMap object. Then the contents of the map
are displayed using a set-view obtained by calling entrySet().
• The keys and values are displayed by calling getKey() and getValue() methods of
the Map entry interface.
• TreeMap instead of HashMap would have given a sorted output.
Hashtable class
• Part of the java.util package
• It implements Map interface and extends Dictionary class
• It can contain only unique elements
• The key cannot have a null value
• It is a synchronized class
TreeMap
• Implements Map interface
• Provides efficient means of storing key/value pair in sorted order
• Allows rapid retrieval
• Guarantees that its elements will be sorted in ascending key order
• The key cannot be null but it can contain multiple null values

You might also like