0% found this document useful (0 votes)
21 views18 pages

Collection 1

The document provides an overview of the Java Collections Framework, which includes interfaces and classes for storing and manipulating groups of objects, such as List, Set, and Queue. It details the implementation of ArrayList and LinkedList, including methods for adding, accessing, changing, and removing elements. Additionally, it explains the Queue data structure and its methods for adding and removing elements.

Uploaded by

Jeyakumar Vjk
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)
21 views18 pages

Collection 1

The document provides an overview of the Java Collections Framework, which includes interfaces and classes for storing and manipulating groups of objects, such as List, Set, and Queue. It details the implementation of ArrayList and LinkedList, including methods for adding, accessing, changing, and removing elements. Additionally, it explains the Queue data structure and its methods for adding and removing elements.

Uploaded by

Jeyakumar Vjk
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
You are on page 1/ 18

Collections

List,Array,Queue

www.iamneo.ai
Introduction
• The Collection in Java is a framework that provides an architecture to store and
manipulate the group of objects.
• Java Collections can achieve all the operations that you perform on a data such as
searching, sorting, insertion, manipulation, and deletion.
• Java Collection means a single unit of objects. Java Collection framework provides many
interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList,
PriorityQueue, HashSet, LinkedHashSet, TreeSet).
Collection framework
• The Collection framework represents a unified architecture for storing and manipulating a
group of objects. It has:
• Interfaces and its implementations, i.e., classes
• Algorithm
Array List
• In Java, we use the ArrayList class to implement the functionality of resizable-arrays.

• It implements the List interface of the collections framework.

• Java ArrayList Vs Array

- In Java, we need to declare the size of an array before we can use it. Once the size of an
array is declared, it's hard to change it. To handle this issue, we can use the ArrayList class. It
allows us to create resizable arrays. Unlike arrays, arraylists can automatically adjust their
capacity when we add or remove elements from them. Arraylists are also known as dynamic
arrays Creating
Creating an ArrayList
• Before using ArrayList, we need to import the java.util.ArrayList package first.
• Here is how we can create arraylists in Java:
ArrayList<Type> arrayList= new ArrayList<>();
Here, Type indicates the type of an arraylist.
For example,
// create Integer type arraylist ArrayList<Integer> arrayList = new ArrayList<>();
// create String type arraylist ArrayList<String> arrayList = new ArrayList<>();
Example
class Main {

public static void main(String[] args){

// create ArrayList

ArrayList<String> languages = new ArrayList<>();

// Add elements to ArrayList languages.add("Java");

languages.add("Python");

languages.add("Swift");

System.out.println("ArrayList: " + languages); } }


Operations

• The ArrayList class provides various methods to perform different operations on


arraylists.
• We will look at some commonly used arraylist operations:
• Add elements
• Access elements
• Change elements
• Remove elements
1. Add Elements - To add a single element to the arraylist, we use the add() method of
the ArrayList class.
2. Access Elements - To access an element from the arraylist, we use the get() method of
the ArrayList class.
3. Change Elements - To change elements of the arraylist, we use the set() method of the
ArrayList class.
4. Remove Elements
• To remove an element from the arraylist, we can use the remove() method of the
ArrayList class.
• We can also remove all the elements from the arraylist at once using Java ArrayList
removeAll() and Java ArrayList clear()
Iterate through an ArrayList
import java.util.*;
class Main {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<>();
al.add("Hai");
al.add("wecome");
for (int i = 0; i < al.size(); i++) {
System.out.print(al.get(i) + " "); }
al.set(1, "welcome");
al.remove("Hai");
for (String str : al)
System.out.print(str + " ");
}}
Linked List

• The LinkedList class of the Java collections framework provides the functionality of the
linked list data structure (doubly linkedlist).
• Each element in a linked list is known as a node. It consists of 3 fields:
• Prev - stores an address of the previous element in the list. It is null for the first element
• Next - stores an address of the next element in the list. It is null for the last element
• Data - stores the actual data
Creating a Java LinkedList
• LinkedList<Type> linkedList = new LinkedList<>();
Methods

• Add elements - We can use the add() method to add an element (node) at the end of the
LinkedList.
• Access elements - get() method of the LinkedList class is used to access an element
from the LinkedList.
• Change Elements - The set() method of LinkedList class is used to change elements of
the LinkedList.
• Remove element - The remove() method of the LinkedList class is used to remove an
element from the LinkedList.
Example
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> animals = new LinkedList<>();
animals.add("Cow");
animals.add("Cat");
animals.add("Dog");
System.out.println("LinkedList: " + animals);
String str = animals.get(1);
animals.set(3, "puppies");
Example
String str = animals.remove(1);
System.out.println("Accessing linked list elements:");
for(String animal: animals) {
System.out.print(animal);
System.out.print(", ");
}
}
}
Queue

• A queue is an object that represents a data structure designed to have the element
inserted at the end of the queue, and the element removed from the beginning of the
queue.
Methods

Add an Element to a Queue


• The Queue interface includes two methods that can be used to add elements to a queue.
• Add () - used to insert a specified element into the queue. It returns true when the task is
successful or else it throws an exception.
• Offer () - used to insert a specified element into the queue. It returns true when the task
is successful or else its return false.
Methods
Remove the Elements from Queue in Java
• Remove () - used to return the head of the queue when the task is successful or else
throws an exception if the queue is empty.
• Poll () - used to return the head of the queue when the task is successful or else it returns
null if the queue is empty.
• Element () - used to return the head of the queue when the task is successful or else
throws an exception if the queue is empty.
• Peek () - used to return the head of the queue when the task is successful or else it
returns null if the queue is empty.
Example
public static void main(String[] arg) {
Queue<Integer> queueOne = new LinkedList<>();
queueOne.add(6); // add method to use insert element
queueOne.add(1);
queueOne.add(8);
queueOne.add(4);
queueOne.add(7);
System.out.println("Before remove and poll method The queue is: " + queueOne);
int positionOne = queueOne.poll();
System.out.println("Removed Element value from Queue : "+positionOne);
int positionTwo = queueOne.remove();
Example

System.out.println("Removed Element value from Queue : "+positionTwo);


System.out.println("After remove and poll method The queue is: " + queueOne);
Iterator<Integer> iterator = queueOne.iterator();
while(iterator.hasNext()){
Integer iteratedvalue = iterator.next(); // next is a inbuilt method in Iterator class
System.out.println("Iterated value is : " + iteratedvalue);
}
}

You might also like