Collection 1
Collection 1
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.
- 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 {
// create ArrayList
languages.add("Python");
languages.add("Swift");
• 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