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

Java Collections Framework Overview

The document discusses the Java Collections Framework, which provides a unified interface for managing groups of objects with pre-defined methods. It covers various collection types, iteration methods, and sorting mechanisms using Comparable and Comparator interfaces. Additionally, it highlights the differences in performance between LinkedLists and ArrayLists, as well as the functionalities of Queues and Deques.

Uploaded by

summykumar236
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 views21 pages

Java Collections Framework Overview

The document discusses the Java Collections Framework, which provides a unified interface for managing groups of objects with pre-defined methods. It covers various collection types, iteration methods, and sorting mechanisms using Comparable and Comparator interfaces. Additionally, it highlights the differences in performance between LinkedLists and ArrayLists, as well as the functionalities of Queues and Deques.

Uploaded by

summykumar236
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

Java Collections: Page1

Framework because it helps us to manage these group of object by pre-defined methods, which was already there and
we don’t have to write all those things again.

Earlier foe every collection, there was different for each of them like array or vector, they have different way and method
to set and get the data.

Java Collection Framework provide a common interface to the objects class.


There are 3 ways to iterate over the collection: 1)Iterator() 2)foreach loop 3)foreach() method
Collections are not interface like collection, but it is a class which have static methods because it is a utility class.

It provide the methods, which are shown in the above fig.


Queue is an interface which have exposed these 6 methods (mentioned below).

Use Priority Queue whenever you want heap properties.

If you don’t provide anything in the priority Queue constructor then it’s min heap, if you provide comparator to the constructor then it’s a max heap.
It uses heapify for min heap. Above fig show how it’s done.
Both helps in sorting. But does not sort.

Object level sorting is only possible, if you provide comparable and comparator to it.

Object need to decide while comparing that if it really want to swap or not for sorting the value, and for that we require
comparable and comparator.

In above fig, it is throwing error, because car class is not able to find a method which can help in comparing and sorting.

Comparator is a functional interface which has this abstract method “int compare(T obj1, T obj2)”

Comparator are not actually sorting, it will call the sort method, in which there will be some logic will be there for
quicksort or merger sort or any other sort, in which it will check with the compare method, whether to swap or not.
sorting in ascending order: val1-val2

sorting in desc order: val2-val1


This example, is without lambda expression. Where we have to implement Comparator Interface, and implement
compare method and then use our own class which implemented the comparator interface as out comparator logic in
the [Link]()

This is another way to implement: this is functional interface way.


You can add and remove item from both the side.

We can also create Stack using dequeue, by using addFirst() for adding in front of the value, and removeFirst() which will
remove the last inserted item, which was placed in the front.
These below methods are of queue interface which can also be used in dequeue, and showing their behaviour for it.

For insertion or removal of item at first or last will have the time complexity O(1).

But, in some case when the queue is full, then the re-sizing of queue will happen, in which a new queue get created and
the value get transferred from old to new queue, and then the time complexity can be like O(n).

Space complexity is O(n) because you may required ‘n’ element.


For priorityqueue the thread safe version isPriority Blocking Queue.

For ArrayDequeue the thread safe version is CurrentLinkedDequeue


The list Iterator is child of Iterator method of Iterable.
Iterator has 3 method, which helps in iterating in forward direction. But with List Iterator, it will have the methods of
Iterator for forward iterating and also their own methods for iterating in backward direction.

So, with List Iterator we can iterate in both the direction, forward and backward.
LinkedList is faster than arrayLists, because you don’t have to do shifting.

You might also like