0% found this document useful (0 votes)
4 views59 pages

Java Collection Framework Overview

This is the pdf of core java collections.that helps student to understand the collection and framework in java

Uploaded by

anujanalawade08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views59 pages

Java Collection Framework Overview

This is the pdf of core java collections.that helps student to understand the collection and framework in java

Uploaded by

anujanalawade08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

City Computers Karad

7. Collection Framework

Objectives
Collections

Types of Collections

Types of Lists

Types of Sets

Types of Map

Collection Algorithms

Legacy Classes

Interview Questions
City Computers Karad
Collection Framework

Collection: It is single entity or object which can store multiple data.


Framework: It represents library. It is set of predefined classes, interfaces which
Is used to store multiple data.
There are two parts: 1. [Link]; 2. [Link];
City Computers Karad
Collection Framework

Data Structure: It is the way by which we can store the data in efficient way.
It focus on time and space complexibilty

Data
Structure

Primitive Non- Primitive

boolean Linear DS Non -Linear


char
byte, short, int ,long
String, Array,
float, double Graph, Tree
List, Set, Queue
City Computers Karad
Collection Framework
City Computers Karad
Types of Collections
City Computers Karad
Types of Collections
City Computers Karad
Types of Cursors

There are three types of cursors:


1. Iterator
1. Iterator used to access all elements from all types of collection. Using iterator()
2. There are following types of operations i.e. hasNext, next,
2. ListIterator
1. ListIterator Only perform operation of List type of collection.
3. Enumeration:
1. Enumeration is the cursor which is used to retrieve collection object one by one
From only legacy classes i.e. vector and stack.
2. Enumeration introduced in JDK 1.0 version
3. Enumeration cursor used only with legacy classes
4. Enumeration cursor can be get by elements() method.
Enumeration e=[Link]();
5. Methods of Enumeration cursor are:
hasMoreElements(),nextElement()
6. Enumeration can access data in forward direction only.
7. Enumeration cursor only used for reading purpose.
City Computers Karad
Difference between Array and Collection Framework

1. Arrays can store primitive and non 1. Collection Framework can store
primitive type of data only non- primitive type data.
2. Arrays can store only homogeneous 2. Collection store hetrogenous
type of data type of data
3. Array size is fixed and cannot change in 3. We can increase or decrease the
runtime size of collection at run time.
4. Arrays are in built feature in JAVA. We 4. Collection framework is API
develop algorithms. which provides predefined
classes, interfaces and methods.
City Computers Karad
ArrayList
City Computers Karad
ArrayList
City Computers Karad
ArrayList

What is the difference between Iterator and ListIterator?


City Computers Karad
LinkedList
City Computers Karad
LinkedList

What is the difference between ArrayList and LinkedList?


City Computers Karad
LinkedList
LinkedList are index-based data structure
LinkedList can store heterogenous elements or different data types
LinkedList can store duplicate data elements
LinkedList can store multiple null elements.
LinkedList follows the insertion order.
LinkedList does not follow sorting order.
LinkedList are non synchronized collection.
City Computers Karad
Queue
A collection designed for holding elements prior to processing. Besides
basic Collection operations, queues provide additional insertion, extraction, and
inspection operations. Each of these methods exists in two forms: one throws an
exception if the operation fails, the other returns a special value
(either null or false, depending on the operation). The latter form of the insert
operation is designed specifically for use with capacity-
restricted Queue implementations; in most implementations, insert operations
cannot fail.

Summary of Queue methods


Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out)


manner. Among the exceptions are priority queues, which order elements
according to a supplied comparator, or the elements' natural ordering
City Computers Karad
Queue
Implemented classes form Queue Interface:
1. LikedList
2. PriorityQueue
3. Dqueue → ArrayDqueue, LinkedList,ConcurrentLinkedDqueue
4. BlockingQueue
5. ArrayBlockingQueue
6. LinkedBlockingQueue
7. PriorityBlockingQueue
8. SynchronousQueue
9. DelayQueue
City Computers Karad
PriorityQueue

PriorityQueue implements as a mean-heap by default for natural ordering


City Computers Karad
ArrayDeque
City Computers Karad
HashSet
City Computers Karad
HashSet
Properties of HashSet:
1. HashSet is not index based data Structure. They are store elements according
to their “HashCode” values.
2. HashSet does not store duplicate values.
3. HashSet cannot store multiple null values.
4. HashSet can store different data types i.e. heterogenous elements.
5. HashSet does not follow insertion order.
6. HashSet does not follow sorting order.
7. HashSet are non synchronized data structure.
8. HashSet underlined data structure is “HashTable”.
9. “HashSet” is backed by “Map”.
City Computers Karad
TreeSet
City Computers Karad
TreeSet
What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface. The differences
between the both are listed below.
• HashSet maintains no order whereas TreeSet maintains ascending order.
• HashSet impended by hash table whereas TreeSet implemented by a Tree
structure.
• HashSet performs faster than TreeSet.
• HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.
• HashSet follows the sorting order.
• HashSet stores homogeneous elements.
What is the difference between List and Set?
The List and Set both extend the collection interface. However, there are some
differences between the both which are listed below.
• The List can contain duplicate elements whereas Set includes unique items.
• The List is an ordered collection which maintains the insertion order whereas
Set is an unordered collection which does not preserve the insertion order.
• The List interface contains a single legacy class which is Vector class whereas
Set interface does not have any legacy class.
• The List interface can allow n number of null values whereas Set interface only
allows a single null value.
City Computers Karad
Map
City Computers Karad
Map
City Computers Karad
Map
Why Map doesn’t extend the Collection Interface?
The Map interface in Java follows a key/value pair structure whereas the Collection
interface is a collection of objects which are stored in a structured manner with a
specified access mechanism. The main reason Map doesn’t extend the Collection
interface is that the add(E e) method of the Collection interface doesn’t support the
key-value pair like Map interface’s put(K, V) method. It might not extend the Collection
interface but still is an integral part of the Java Collections framework.

Properties of Map
1. Map store data in Key-Value Pair
2. In Map keys should be unique but values may be duplicate.
3. In Map we store maximum one null value in Key but number of duplicate null in
values.
4. Map does not follow insertion order.
5. Map does not follow sorting order.
6. Key and Value are always object.
7. Method put
City Computers Karad
HashMap

What is the difference between HashMap and TreeMap?


The differences between the HashMap and TreeMap are given below.
• HashMap maintains no order, but TreeMap maintains ascending order.
• HashMap is implemented by hash table whereas TreeMap is implemented by a Tree
structure.
• HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.
• HashMap may contain a null key with multiple null values whereas TreeMap cannot hold
a null key but can have multiple null values.
City Computers Karad
HashMap
What is the difference between HashSet and HashMap?
The differences between the HashSet and HashMap are listed below.
• HashSet contains only values whereas HashMap includes the entry (key, value).
HashSet can be iterated, but HashMap needs to convert into Set to be iterated.
• HashSet implements Set interface whereas HashMap implements the Map interface
• HashSet cannot have any duplicate value whereas HashMap can contain duplicate
values with unique keys.
• HashSet contains the only single number of null value whereas HashMap can hold a
single null key with n number of null values.

What is the difference between Set and Map?


The differences between the Set and Map are given below.
• Set contains values only whereas Map contains key and values both.
• Set contains unique values whereas Map can contain unique Keys with duplicate
values.
• Set holds a single number of null value whereas Map can include a single null key
with n number of null values.
City Computers Karad
HashMap

The Map interface provides 3 views of key-value pairs which are:


1. key set view
2. value set view
3. entry set view
All these views can be easily navigated through using the iterators.
City Computers Karad
TreeMap
TreeMap underlined data-structure is “Red-Black-Tree”.

Properties of Red-Black Tree


1. Every node is either Red or Black color.
2. The root node is always black.
3. Every leaf node(NULL node) is black.
4. If a node is red , then both its children are black.
5. For each node, all paths from the node to its descendent leaf nodes
contain the same number of black nodes.
Properties of TreeMap
1. TreeMap store data in Key-Value Pair
2. In TreeMap keys should be unique but values may be duplicate.
3. In TreeMap cannot store null values.
4. TreeMap does not follow insertion order.
5. TreeMap follow sorting order with respect to key.
6. TreeMap can store homogeneous and heterogenous type of values.
7. TreeMap follow non-synchronized data structure.
City Computers Karad
ConcurrentHashMap
ConcurrentHashMap is a Java class that implements ConcurrentMap as well as
Serializable interfaces. This class is the enhanced version of HashMap as it doesn’t
perform well in the multithreaded environment. It has a higher performance rate
compared to the HashMap.
Below is a small example demonstrating the implementation of ConcurrentHashMap:
City Computers Karad
Comparators
City Computers Karad
Comparable
City Computers Karad
Comparable
City Computers Karad
Collections Algorithms
public static final <T> Set<T> emptySet() City Computers Karad
Collections Algorithms
The [Link]() is used to return
the empty immutable Set while removing the
null elements. The set returned by this method
is serializable.

What is the difference between Collection and Collections?


The differences between the Collection and Collections are given below.
• The Collection is an interface whereas Collections is a class.
• The Collection interface provides the standard functionality of data structure to
List, Set, and Queue. However, Collections class is to sort and synchronize the
collection elements.
• The Collection interface provides the methods that can be used for data structure
whereas Collections class provides the static methods which can be used for
various operation on a collection.
City Computers Karad
Legacy Classes
City Computers Karad
Legacy Classes
Vector:
1. It is index based data structure.
2. Vector can store different data type i.e. heterogenous
data type.
3. We can store duplicate value.
4. We can store duplicate null values.
5. It follows insertion order.
6. It does not follow sorting order.
7. It is synchronized collection.

Methods:
1. It contains list and collection interface.
2. addElement(obj); [Link]();
4. lastElement(); 5. removeElement(Objectobj)
6. removeElementAt(int index); 7. removeAllElement();
8. capacity();
City Computers Karad
Legacy Classes
What is the difference between ArrayList and Vector?

5) ArrayList does not contains data 5) Vector contains data consistency.


consistency.
6) In case of ArrayList capacity is 6) In case of vector capacity is
new capacity=(old capacity*3)/2+1 new capacity=(old capacity*2)
7) ArrayList not provide any method for 7) It provide a method
Find capacity int capacity();
8) High speed 8) Low speed
City Computers Karad
Legacy Classes
What are the main differences between array and collection?
Array and Collection are somewhat similar regarding storing the references of objects
and manipulating the data, but they differ in many ways.
• Arrays are always of fixed size, i.e., a user can not increase or decrease the length of the
array according to their requirement or at runtime, but In Collection, size can be
changed dynamically as per need.
• Arrays can only store homogeneous or similar type objects, but in Collection,
heterogeneous objects can be stored.
• Arrays cannot provide the ready-made methods for user requirements as sorting,
searching, etc. but Collection includes readymade methods to use.

Stack:
1. It is child class of vector class.
2. Syntax:
class stack extends vector{

}
3. It follows all properties of stack i.e. LIFO (Last In First Out)
4. Stack can implemented on the basis of arrays, ArrayList, LinkedList or vector.
5. Stack is index based data structure.
City Computers Karad
Legacy Classes
City Computers Karad
Legacy Classes
City Computers Karad
Legacy Classes
Differentiate between Queue and Stack.
City Computers Karad
Legacy Classes
What is the difference between HashMap and Hashtable?

What is the difference between Iterator and Enumeration?


City Computers Karad
Legacy Classes
City Computers Karad
Interview Questions
• What is the Collection framework in Java?
• What is Java Collections Framework? List out some benefits of Collections framework?
• What do you understand by Collection Framework in Java?
• What are the advantages of the Collection Framework in Java?
• What is the advantage of the generic collection?
• What are Collection related features in Java 8?
• What is the benefit of Generics in Collections Framework?
• Describe the Collection hierarchy in Java.
• Explain various interfaces used in Collection framework?
• What are the basic interfaces of Java Collections Framework?
• List down the primary interfaces provided by Java Collections Framework?
• What are the main differences between array and collection?
• Why Collection doesn’t extend the Cloneable and Serializable interfaces?
• What is the difference between Array and ArrayList?
• What is the difference between the length of an Array and size of ArrayList?
• How to convert ArrayList to Array and Array to ArrayList?
• How would you convert an ArrayList to Array and an Array to ArrayList?
• How to make Java ArrayList Read-Only?
• How to remove duplicates from ArrayList?
City Computers Karad
Interview Questions
• How to reverse ArrayList?
• How to sort ArrayList in descending order?
• How to synchronize ArrayList?
• What do you understand by Iterator in the Java Collection Framework?
• When to use ArrayList and LinkedList?
• What is an Iterator?
• What do you understand by Iterator in the Java Collection Framework?
• Why there is not a method like [Link]() to add elements to the collection?
• Why Iterator don’t have a method to get next element directly without moving the
cursor?
• What is different between Iterator and ListIterator?
• What are different ways to iterate over a list?
• Why there are no concrete implementations of Iterator interface?
• What is difference between Array and ArrayList? When will you use Array over ArrayList?
• What is difference between ArrayList and LinkedList?
• Which collection classes provide random access of its elements?
• How to avoid ConcurrentModificationException while iterating a collection?
• What do you understand by LinkedList in Java? How many types of LinkedList does Java
support?
City Computers Karad
Interview Questions
• What is a priority queue in Java?
• What are the various methods provided by the Queue interface?
• What is the difference between List and Set?
• Differentiate between List and Map.
• Differentiate between Queue and Deque.
• What is the difference between HashSet and TreeSet?
• What is the difference between Set and Map?
• Can you add a null element into a TreeSet or HashSet?
• What is the HashSet class in Java and how does it store elements?
• What is Set in Java Collections framework and list down its various implementations?
• What is LinkedHashSet in Java Collections Framework?
• What is the difference between HashSet and HashMap?
• What is the difference between HashMap and TreeMap?
• What is the difference between HashMap and Hashtable?
• What is the difference between Collection and Collections?
• What is the difference between Comparable and Comparator?
• How to synchronize List, Set and Map elements?
• Differentiate between HashMap and HashTable.
City Computers Karad
Interview Questions
• Differentiate between the Singly Linked List and Doubly Linked List.
• Differentiate between PriorityQueue and TreeSet.
• Differentiate between Queue and Stack.
• Differentiate between Collection and Collections.
• Can you use any class as a Map key?
• What is the ConcurrentHashMap in Java and do you implement it?
• List down the different Collection views provided by the Map interface in the Java
Collection framework?
• Why Map doesn’t extend the Collection Interface?
• What is Map interface in Java?
• How HashMap works in Java?
• What is difference between HashMap and Hashtable?
• Explain the emptySet() method in the Collections framework?
• What are similarities and difference between ArrayList and Vector?
• Differentiate between ArrayList and Vector.
• Differentiate between Iterator and Enumeration.
• What is the difference between Iterator and Enumeration?
• What is the Dictionary class?
• What is the advantage of Properties file?
City Computers Karad
Interview Questions
• What is the main benefit of using the Properties file?
• What do you understand by BlockingQueue?
• What is hash-collision in Hashtable and how it is handled in Java?
• What is the default size of load factor in hashing based collection?
• What do you understand by fail-fast?
• Differentiate between failfast and failsafe.
• What is the Stack class in Java and what are the various methods provided by it?
• What is a Vector in Java?
• How the Collection objects are sorted in Java?
• What is the difference between Enumeration and Iterator interface?
• What do you understand by iterator fail-fast property?
• What is difference between fail-fast and fail-safe?
• What is EnumSet?
• Which collection classes are thread-safe?
• What are concurrent Collection Classes?
• What is BlockingQueue?
• What is Queue and Stack, list their differences?
• What is Collections Class?
City Computers Karad
Interview Questions
• How can we sort a list of Objects?
• While passing a Collection as argument to a function, how can we make sure the
function will not be able to modify it?
• How can we create a synchronized collection from given collection?
• What are common algorithms implemented in Collections Framework?
City Computers Karad
End of Lecture
In this lecture we have seen one of the very important subsystem of Java Language called
as Collection Framework. It is extremely helpful in all kinds of application development..

Visit [Link] for more information about Java


City Computers Karad
Courses @ City Computers
As one of the leading Programming Institute and Software Development Firm, We provide
all types of programming courses -

College Level/Educational Courses

Industrial Training for College Students

100% Job Oriented Specialization Courses

Final Year Project Training

Software Development Courses

Interview Preparation Courses


City Computers Karad
Courses @ City Computers
Why need to join courses @ City Computers, Karad
✓ Teaching faculty has more than 15 years of experience in Teaching + Software
Development.
✓ Well designed power point presentation for each lecture.
✓ Develop your concepts, logic and programming skill.
✓ Affordable fees.
✓ Lectures on Big LED TV/Projector.
✓ Motivational Environment.
✓ Limited Students Batch.
✓ Till date, many students are placed in different IT Companies.
✓ Live Examples.
✓ Covers Interview questions.
✓ Student level attention.
City Computers Karad
Courses @ City Computers
As well as we provide a complete ready made project + documentation required to
different years of students such as
Ask for Project List

Mini Project for College Students

Final Year Projects

Industrial Projects with Company Project Completion Certificate

Final Year Project Training

M.E, M. Tech and Ph. D Projects

We develop project in any technology or in any programming language or in any domain,


based on your ideas or our ideas.
City Computers Karad

About Specialization Courses


City Computers Karad
City Computers Karad
Other Courses @ City Computers
Courses @ City Computers Karad
C C++ DS Core Java Adv. Java
[Link] C# [Link] Python PHP
Web Designing Web Development RDBMS/Oracle SQL Server Android
iOS Embedded C IOT [Link] MVC Angular

React JS WordPress MATLAB HADOOP Apache Spark

Java Certification Oracle Certification Microsoft Azure AWS Machine Learning

Microsoft Certification R Language NS3 PHP Certification Python Certification

Spring Hibernate DevOps Struts Selenium Testing


Linux Certification Data Science Software Testing Block Chain Full Stack Developer

Node JS MongoDB
City Computers Karad
Interview Preparation Courses
1. Selection Process in IT Companies
2. Aptitude Test Preparation
3. Interview and Written Test Preparation
❑ C programming
❑ C++ Programming
❑ Data Structure
❑ SQL / Oracle
4. Resume Preparation
5. Preparation of English Communication Skill

• Course covers all basic of C,C++,DS and SQL with their most commonly used interview
and written test questions.

• Well Designed Notes and PowerPoint presentation.

• Learn from Experts.


City Computers Karad

You might also like