What is Collection in JAVA:
-It is a pre-defined structure that provides a set of classes and interfaces that help developers store, manipulate, and retrieve data easily.
Why do we need collections?
-Before collections, Java mainly used arrays to store multiple values. However, arrays have some limitations:
*Arrays have a fixed size
*They can store only the same type of elements(Homogeneous)
*Difficult to perform operations like insertion, deletion, and searching
*Memory waste for unused positions
Collections solve these problems by providing flexible and powerful data structures.
Java Collection Framework Hierarchy:
*The Collection interface is the root interface of the collections framework hierarchy.
*Java does not provide direct implementations of the Collection interface, but provides implementations of its subinterfaces like List, Set, and Queue.
- The framework includes other interfaces as well: Map and Iterator. These interfaces may also have subinterfaces.
Sub interfaces of collection:
- List Interface
- Set Interface
- Queue Interface
- Map(Not a sub interface of collection)
1.1 What is List Interface:
- The List interface maintains insertion order and allows duplicate elements. -In Java, we must import java.util.List package to use List.
// ArrayList implementation of List
ArrayList list = new ArrayList();
** Classes that implement List: **
- ArrayList
- LinkedList
- Vector
- Stack
1.1 What is an ArrayList:
- It is one of the classes that provides implementation for the List.
- Duplicate values are allowed, and insertion order is maintained
- Can insert Hetrogeneous objects if generics are not used, but this is not the best practice
- It is not synchronised, not thread safe.
- Default capacity is 10
- Best for search operations and worst for insertion and deletion
1.2 What is a LinkedList:
- LinkedList is best for insertion or deletion operations and not suitable for retrieval
- Insertion order is maintained, and duplicate values are allowed
1.3/1.4- Stack and Vector:
- These are legacy classes. (From first version)
- To add an element push is used
- To remove an element pop is used
Methods of List:
Methods Description
add() adds an element to a list
addAll() adds all elements of one list to another
get() helps to randomly access elements from lists
iterator() returns an iterator object that can be used to sequentially access elements of lists
set() changes elements of lists
remove() removes an element from the list
removeAll() removes all the elements from the list
clear() removes all the elements from the list (more efficient than removeAll())
size() returns the length of lists
toArray() converts a list into an array
contains() returns true if a list contains a specific element
Implementation of the List Interface
import java.util.List;
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Creating list using the ArrayList class
List<Integer> numbers = new ArrayList<>();
// Add elements to the list
numbers.add(1);
numbers.add(2);
numbers.add(3);
System.out.println("List: " + numbers);
// Access element from the list
int number = numbers.get(2);
System.out.println("Accessed Element: " + number);
// Remove element from the list
int removedNumber = numbers.remove(1);
System.out.println("Removed Element: " + removedNumber);
}
}
List: [1, 2, 3]
Accessed Element: 3
Removed Element: 2
2. What is set interface:
-The Set interface stores unique elements and does not allow duplicates.
-Lists can include duplicate elements. However, sets cannot have duplicate elements.
-Elements in lists are stored in some order. However, elements in sets are stored in groups.
** Classes that implement Set:**
- HashSet
- LinkedHashSet
- Tree Set
Implementation of Set:
Set animals = new HashSet();
Methods of Set:
add() - adds the specified element to the set
addAll() - adds all the elements of the specified collection to the set
iterator() - returns an iterator that can be used to access elements of the set sequentially
remove() - removes the specified element from the set
removeAll() - removes all the elements from the set that is present in another specified set
retainAll() - retains all the elements in the set that are also present in another specified set
clear() - removes all the elements from the set
size() - returns the length (number of elements) of the set
toArray() - returns an array containing all the elements of the set
contains() - returns true if the set contains the specified element
containsAll() - returns true if the set contains all the elements of the specified collection
hashCode() - returns a hash code value (address of the element in the set)
1.1. What is HashSet:
- A HashSet is a class in the Java Collection Framework that implements the Set interface.
- It is used to store a collection of unique elements, meaning it does not allow duplicate values, and it will not maintain insertion order
- We must import the java.util.HashSet package first.
Implementation of HashSet:
HashSet numbers = new HashSet();
2.1: What is LinkedHashSet:
- It is used to store a collection of unique elements, meaning it does not allow duplicate values, but it will maintain the insertion order.
- We must import the java.util.LinkedHashSet package first.
Implementation of LinkedHashSet:
linkedHashSet numbers = new LinkedHashSet();
3.1: What is TreeSet:
- A TreeSet is a class in the Java Collection Framework that implements the Set interface and stores unique elements in sorted order.
- We must import the java.util.TreeSet package first. -Can't add heterogeneous values -Can't store duplicate values and Null values
- If you store different types of data, it will throw classcast Exception.
- If you want to store multiple datatypes, then provide a generic type<>
Implementation of TreeSet:
TreeSet<Integer> numbers = new TreeSet<>();

Top comments (0)