0% found this document useful (0 votes)
11 views66 pages

Collections

Uploaded by

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

Collections

Uploaded by

manojsathe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

Collections

Example: Collections.sort(al); //al is ArrayList object


1. Duplicates are allowed.
2. Insertion order is preserved.

Implementation Classes for List Interface Vector and Stack are know as Legacy Classes.
I don’t want
Duplicate
elements & I Solution is
also don’t
want to
preserve
insertion Set
order. Interface
Implementation Classes for Set are
HashSet and LinkedHashSet
Implementation Class for NavigableSet is TreeSet
Implementation Classes for Queue are PriorityQueue and BlockingQueue
Map is child
interface of
Collection
?
NO
Rahul
Kishan
Abhishek
Implementation Classes for Map are hashMap, WeakHashMap ….
vii. SortedMap (I)
Implementation Class for
NavigableMap is TreeMap
Overview of
Collection (I)
Interface
Methods:
Is there any get() method available to
retrieve object from collection?
List Methods:

By default all methods


of Collection Interface
will be available to List

Replacing an element
object
ArrayList Constructors:

1. ArrayList al = new ArrayList()

It creates an empty Array List object with default capacity........?

Once ArrayList reaches its map capacity a new Array List will be created with new capacity

New capacity = (Current Capacity * 3/2) +1


ArrayList Constructors:

2. ArrayList al = new ArrayList(int initial capacity)


E.g.: To create AL of capacity 1000

3. ArrayList al = new ArrayList(Collection c)

To create equivalent ArrayList for any


collection like LinkedList, Vector, Stack
Program: ArraylistDemo.java
What is Collection?

In Java In C++
Collection Container

1. To hold our object and transfer to other place over the


network
2. To transfer object, the object must be serializable object.
3. To provide this functionality/support, every collection
class (Arraylist,LinkedList,Vector,Stack,Tree..) already
implement Serializable interface
What is Collection?
S2
S1

Cloned S5

ArrayList
S3

Every Collection class implements Cloneable interface S4

S2
S2 S1
S1
S5
S5 Network
S3
S3 S4
S4

ArrayList (Serializable) ArrayList


Program: Clonable.java
Difference between ArrayList and Vector
A Tricky
Question in
Interview
Room
Q. Can you make ArrayList Object Synchronized?

Interviewer

Yes

You
1. Best Choice:

If we want to retrieve elements from ArrayList, the operation is very fast


because ArrayList implements RandomAccess Interface which helps for
fast retrieval of elements.

Where ArrayList is Best


Choice and Where ArrayList 2. Worst Choice:
is worst Choice? If we want to insert or delete specific element middle of the ArrayList,
the performance will be very slow.
(E.g. If we have 1 Crore elements in ArrayList, and if we want to insert or
delete element at second place, shifting operation will be done for 1
Crore Times.)
LinkedList:

1. The underlying data structure is Double Linked List

2. Insertion order is preserved.

3. Duplicates are allowed.

4. Heterogenous objects are allowed,

5. Null insertion is possible.


Source: https://akcoding.com/dsa/linear-data-structures/exploring-the-doubly-linked-lists/
1. Best Choice:

If we want to insert or delete specific element middle of the ArrayList,


the performance will be very fast.

Where LinkedList is Best


Choice and Where it is 2. Worst Choice:
worst Choice?
If we want to retrieve elements from ArrayList, the operation will be very
slow because every element stores the address of another element and
so on.
LinkedList Constructors:

2. LinkedList al = new LinkedList()


E.g.: To create Empty LinkedList Object

3. LinkedList al = new LinkedList(Collection c)

To create equivalent ArrayList for any


collection like LinkedList, Vector, Stack
Program: LinkedListDemo.java
Difference between ArrayList and LinkedList
Vectors:

1. The underlying data structure for Vector is resizable array or growable array.

2. Insertion order is preserved.

3. Duplicates are allowed.

4. Heterogenous objects are allowed,

5. Null insertion is possible.

6. Vector class implements Serializable, Clonable and RandomAccess Interfaces.

7. Most of the methods present in Vector are synchronized. Hence, Vector object is Thread Safe.

8. Best to use when we prefer retrieval operation


Vector Specific Methods:
Vector Specific Methods:
Vector Specific Methods:

//To get object one by one from Vector


Vector Constructors:
Program: VectorDemo.java

With Default Capacity -10


Program: VectorDemo1.java

With Initial Capacity -25


Program: VectorDemo3.java

With Initial Capacity -10 &


Incremental Capacity - 5

You might also like