Collections
Collections
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
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"