management books

Introduction

A collection is a group of objects or it is a single entity that represents multiple objects. 

Collection Framework consists of classes and interfaces by using these classes and interface developers can represent a group of objects in a single entity. collection framework is present in package java.util

What is a need for the Collection Framework?

Suppose, A variable is created to store a data and 10 value is assigned (Example, int a =10). Now the programmer wants to store another data of the same datatype. So, the programmer needs to create another variable and assign a new value (Example, int b= 20). 

If the programmer wants to store 100 values then the disadvantage of this is the programmer has to create multiple variables with a unique name and it is very time-consuming also.

In this case array concept is introduced. Programmer declare an array with specific size and store elements.

For example,

int  arr[] = new int[100]; // 100 is size of array 
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
.
.
.
arr[100] = 90;

This is the way of store multiple values of the same datatype.

But there are certain limitations 

Also Read: Strings in Java

  1. Array stores the values of the same datatype i.e., Array is homogeneous but it can overcome by creating an array of object classes but this is not a good option.


The main limitation is an array has a fixed size (not growable) i.e., 

In the above example array is created with a size of five which means the array store only five data values. 

If the size of the array is five and the user store only four values then memory is wasted.

To overcome this limitation, the Collection Framework was used.

In the collection framework, there are classes and interfaces are defined which are List, Queue, Set, etc. 

Sr.noArrayCollection Framework
1Fixed-size (not growable)Growable in nature
2If the size is 10 and only 5 elements store then it is a waste of memory.It adjusts size according to elements.
3Arrays can hold only homogeneous data elements.Collection can hold homogeneous as well as heterogeneous data elements.
4Memory management is poor.Memory management is effective.
  • Difference between collection and collections

The collection is the root interface of the collection framework and provides several classes and interfaces to represent a group of individual objects as a single unit.

List, Set, and Queue are the main child-interfaces of the collection interface.

The Map interface is also part of the java collection framework but it does not inherit the collection interface. The map interface is preferred when values are stored in the form of keys and value pairs.

Map Interface implemented using following classes:-

  • Hashmap
  • LinkedHashmap
  • HashTable

Methods present in the collection interface

Sr.noMethodDescription
1add(Object o)To insert an element in the collection.
2addAll(Collection c)To insert another collection in the present collection.
3remove(Object o)To remove an element in the collection.
4removeAll(Collection c)To remove another collection from the present collection if another inserted.
5retainAll(collection c)To remove all the collection elements that are not contained in the specified collection.
6clear()It removes all the elements from the collection.
7isEmpty()It checks collection is empty or not and provides true or false.
8size()It gives the total number of elements present in the collection in form of a numeric value.
9equals(collection c)It is used to check if the two collections are the same or not.
10toArray(collection c)It converts collection into an array.
11contains(Object o)It is used for searching.If an element present in the collection it returns true or false.
12containsall(collection c)It is used for searching.If elements of another collection are present in the collection or not. If present returns true or false.
  • List Interface
  • The list is a child interface of Collection.
  • Insertion order preserved i.e., They appear in the same order in which we inserted.
  • Duplicate elements are allowed.

     List Interface is implemented by using ArrayList, LinkedList, and Vector class.

  1. ArrayList
  • ArrayList is a class present in java. util package.
  • It provides a dynamic array for storing the element.
  • It is an array but there is no size limit.
  • We can add or remove elements easily.
  • It is more flexible than a traditional array.


    How to create ArrayList

For example,

1. This is way is to store values of the same datatype

  1. This is way is to store values of different datatype

Methods in ArrayList:-

Sr.noMethodDescription
1get(object o)It prints the value at a specific index.
2set(index, object o)It updates the value. In that, we need to provide an index.
3add(index, object o)It adds an element at a specific index.
4remove(object o)It removes elements at specific indexes.
5sort()It sorts an array depending upon data type.
6addAll(collection c)It is used to add another collection.
7removeAll(collection c)It is used to remove another collection.

The common methods in the elements are shown below.

toArray() method

Ways to reading elements from any list

  • For loop
  • For …. Each loop
  • Iterator


  1. LinkedList
  • LinkedList class uses a doubly LinkedList to store element. i.e., the user can add data at the first position as well as the last position.
  • The dequeue interface is implemented using the LinkedList class.
  • Null insertion is possible.
  • If we need to perform insertion /Deletion operation the LinkedList is preferred.
  • LinkedList is used to implement Stacks and Queues.
  • How LinkedList works?

Consider LinkedList contains 3 elements,

LinkedList element is not stored at the consecutive address they stored at any address but they internally connected using the address of previous and next element address.

PA :-Previous Element address  NA:- Next Element Address      index:0,1,2,….

How to create a LinkedList

For example,

  1. This is way is to store values of the same datatype
  1. This is way is to store values of different datatype

Methods in LinkedList:-

Some methods in LinkedList are the same as ArrayList. Refer program no. 4, 5, 6, 7. change is to replace ArrayList with LinkedList.

Another methods in LinkedList are:

  • addFirst()
  • addLast()
  • removeFirst()
  • removeLast()
  • getFirst()
  • getLast()
  1. Vector
  • Every method is synchronized.
  • The vector object is Thread safe.
  • At a time only one thread can operate on the Vector object.
  • performance is low because Threads are needed to wait.

      How to create a list using vector 

Methods in vector:-

Some methods in Vector is same as Arraylist. Refer program no.4, 5, 6, 7  . change is replace ArrayList to Vector.

Another methods are:

addElement()

firstElement()

lastElement()

  1. Stack
  • It is the child class of Vector.
  • It is based on LIFO (Last In First Out) i.e., Element inserted in last will come first.

  • Set Interface
  •   Set is a child interface of Collection.
  • Insertion order not preserved i.e., They appear in the different order in which we inserted. 
  • Duplicate elements are not allowed.
  • Heterogeneous objects are allowed.

     Set Interface is implemented by using LinkedHashSet and HashSet class.

  1. Hashset
  • HashSet stores the elements by using Hashing mechanism.
  • HashSet contains unique elements only.
  • HashSet allows null values.
  • HashSet does not maintain insertion order. It inserted elements based on their hashcode.
  • HashSet is the best approach for the search operation.

There are three different ways to create HashSet:


Here, Hashset default capacity to store elements is 16 with a default load factor/fill ratio of 0.75.

Load factor is if HashSet stores 75% element then it creates a new Hashset with increased capacity.

  1.  

     Here 100 is an initial capacity and the thdefaultlt load factor is 0.75.


Here capacity is 100 with a load factor of 0.90. The load factor may be decided by the user but it should be >=0.75.

4.

Method in HashSet

Some methods are common in HashSet and Arraylist refer to program no. 4, 5, 6, 7. 

In HashSet get() and set() method not present because forget and set method index is required and in HashSet elements stores at a random address

Problem Statement:-

Write a program to remove duplicate elements.

  1. LinkedHashSet
  • The LinkedHashSet class extends the HashSet class.
  • The basic data structure is a combination of LinkedList and Hashtable.
  • Insertion order is preserved.
  • Duplicates are not allowed.
  • LinkedHashSet is non synchronized.
  • LinkedHashSet is the same as HashSet except the above two differences are present.

for example

  1. SortedSet
  • SortedSet implements (child interface) Set Interface.
  • If we want to insert unique elements where duplicates are not allowed and all elements should be inserted according to some sorting order then we should go for the SortedSet interface.
  • Sorting order can be either default sorting  (or) user can decide sorting order.
  1. TreeSet
  • Java TreeSet class implements the Set interface that uses a tree structure to store elements.
  • It contains Unique Elements.
  • TreeSet class access and retrieval time are very fast.
  • It does not allow null elements.
  • It maintains Ascending Order.

  • Queue Interface

 The queue interface is provided in java. utill package and implements the collection interface.

  • The queue implements FIFO i.e., First In First Out which means the elements entered first comes out first.
  • The queue is implemented by LinkedList, priority queue classes, and ArrayDequeue Interface. PriorityQueue is allowed homogeneous data while LinkedList allows heterogeneous as well as homogeneous data.
  • Dequeue is a linear collection that supports element insertion and removal at both sides. Null elements are not allowed in the dequeue.

ArrayDequeue is faster than LinkedList. 

Methods in Queue :

add() :- It used to insert data into queue. If data is not inserted successfully it throws an exception.

offer():- It is used to insert data into the queue. If data is not inserted successfully it returns false.

element():-It returns head elements from the queue. If Queue is empty it will throw exception NoSuchElementException.

peek():- It returns head elements from the queue. . If Queue is empty it will return Null.

remove():- It removes an element from the queue. If Queue is empty it will throw exception NoSuchElementException.

poll():- It removes the element from the removing. If Queue is empty it will return Null.

  • Map Interface
  • A map is a part of the collection framework but it does not implement a collection interface.
  • A map stores values based on Key and value Pair.
  • Duplicate value of the key is not allowed. In short,

Key must be unique while duplicates values are allowed.

  • Map Interface
  •  HashMap
  • LinkedHashMap
  • Hashtable
  1. HashMap
  • Map Interface is implemented by HashMap.
  • HashMap stores the elements by using a mechanism called Hashing.
  • It contains values based on the key-value pair.
  • It contains a unique key.
  • It can store one Null key and Multiple null values.
  • Insertion order is not maintained and it is based on the hash code of the keys.
  • HashMap is Non-Synchronized.
  • How to create HashMap

For example,

  1. LinkedHashMap
  • The basic data structure is a combination of LinkedList and Hashtable.
  • It is the same as HashMap except above difference.
  1. Hashtable
  • A Hashtable is an array of lists. Each list is known as a bucket. 
  • A hashtable contains values based on key-value pair.
  • It contains unique elements only.
  • Hashtable class does not allow null key as well as value otherwise it will throw NullPointerException.
  • Every method is synchronized. i.e At a time only one thread is allowed and the other threads are on a wait.  
  • Performance is poor as compared to HashMap.  

How to create HashMap

There are three ways:

  1. Here default capacity is 11, the load factor is 0.75. (Load factor refer HashSet)

  2. Here Hashtable is created with some capacity

Here Hashtable is created with some capacity and the load factor is decided by the user. It should be >=0.75.

Note:- Methods in Hashtable are exactly the same as Hash Map.


0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

nineteen − 10 =