0% found this document useful (0 votes)
5 views5 pages

Collections

java collections

Uploaded by

TRANAND TR
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)
5 views5 pages

Collections

java collections

Uploaded by

TRANAND TR
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/ 5

Collections

In Java, a Collection is a framework that provides an architecture to store,


manipulate, and manage a group of objects. The Java Collections Framework (JCF)
is a set of classes and interfaces that implement commonly reusable collection data
structures.

The Collection interface (found in java.util) is the root of the collections hierarchy.
From this interface, several other interfaces like List, Set, and Queue extends.

Interfaces in JCF

• Collection Interface - java.util.Collection


• List Interface - java.util.List
• Set Interface - java.util.Set
• Queue Interface - java.util.Queue
• Map Interface - java.util.Map
• Deque Interface - java.util.Deque

Classes in JCF

• ArrayList
• LinkedList
• HashSet
• TreeSet
• HashMap
• PriorityQueue
ArrayList in Java
ArrayList in Java is a part of the Java Collections Framework and is one of the
most commonly used classes to store and manipulate dynamic data.
Key Features of ArrayList:
• Dynamic Size: The size of an ArrayList is not fixed. It can increase or
decrease as elements are added or removed.
• Maintains Insertion Order: Elements in an ArrayList are stored in the order
in which they were inserted.
• Allows Duplicates: An ArrayList can store duplicate elements.
• Index-Based Access: Elements can be accessed using their index position,
providing fast access to elements.
Syntax for Creating an ArrayList
ArrayList<data_type> listName = new ArrayList<data_type>();

Methods of ArrayList
. add(E element) - Adds the specified element to the end of the list.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
add(int index, E element) - Inserts the specified element at the specified index.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add(0, "Banana"); // Insert "Banana" at index 0
get(int index) - Returns the element at the specified position in the list.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
String fruit = list.get(0); // Returns "Apple"
set(int index, E element) - Replaces the element at the specified position with the
specified element.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.set(0, "Orange"); // Replaces "Apple" with "Orange"
remove(int index) - Removes the element at the specified position and returns it.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.remove(0); // Removes the element at index 0
remove(Object o) - Removes the first occurrence of the specified element from
the list, if it is present.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.remove("Apple"); // Removes the "Apple" object from the list
size() - Returns the number of elements in the list.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
int size = list.size(); // Returns 1
contains(Object o) - Returns true if the list contains the specified element,
otherwise returns false.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
boolean hasApple = list.contains("Apple"); // Returns true
isEmpty() - Returns true if the list contains no elements.
ArrayList<String> list = new ArrayList<>();
boolean empty = list.isEmpty(); // Returns true if the list is empty
clear() - Removes all elements from the list.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.clear(); // Clears the entire list
indexOf(Object o) - Returns the index of the first occurrence of the specified
element, or -1 if it is not found.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
int index = list.indexOf("Apple"); // Returns the index of "Apple", which is 0
lastIndexOf(Object o) - Returns the index of the last occurrence of the specified
element, or -1 if it is not found.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple");
int index = list.lastIndexOf("Apple"); // Returns 2, the last occurrence of "Apple"

toArray() - Returns an array containing all of the elements in the list.


ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Object[] array = list.toArray(); // Converts the list to an array
subList(int fromIndex, int toIndex) - Returns a view of the portion of the list
between the specified fromIndex, inclusive, and toIndex, exclusive.
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
List<String> sublist = list.subList(0, 2); // Returns a sublist containing "Apple" and "Banana"

clone() - Returns a shallow copy of the ArrayList.


ArrayList<String> list = new ArrayList<>();
list.add("Apple");
ArrayList<String> copy = (ArrayList<String>) list.clone();
When to Use ArrayList:
• When you need a dynamic array that can grow and shrink.
• When you need a data structure that maintains insertion order.

You might also like