Java Collections Functions
java arraylist,linkedlist,map,set and stack fxn ,iteration with code
Here’s a quick overview of `ArrayList`, `LinkedList`, `Map`, `Set`, and `Stack` in Java, along with
functions and iteration examples for each:
1. ArrayList
An `ArrayList` is a part of the Java Collection Framework. It is a dynamic array that grows as needed.
Functions:
`add()`: Adds an element to the list.
`get()`: Retrieves an element at a specified index.
`remove()`: Removes an element from the list.
`size()`: Returns the number of elements in the list.
Iteration:
java
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
// Iterating using for loop
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
}
// Iterating using for-each loop
for (String fruit : list) {
[Link](fruit);
}
}
}
2. LinkedList
A `LinkedList` is a linear data structure where each element points to the next.
Functions:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/4
`add()`:Adds an element at the end of the list.
`get()`:Retrieves an element at a specified index.
`remove()`: Removes an element at a specified index or value.
`size()`: Returns the number of elements in the list.
Iteration:
java
import [Link];
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
[Link]("Dog");
[Link]("Cat");
[Link]("Bird");
// Iterating using for loop
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
}
// Iterating using for-each loop
for (String animal : list) {
[Link](animal);
}
}
}
3. Map (HashMap)
A `Map` is a collection of key-value pairs, where each key is unique.
Functions:
`put()`: Adds a key-value pair.
`get()`: Retrieves the value associated with a key.
`remove()`: Removes the key-value pair.
`size()`: Returns the number of key-value pairs.
Iteration:
java
import [Link];
import [Link];
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
[Link]("Alice", 25);
[Link]("Bob", 30);
[Link]("Charlie", 35);
// Iterating using for-each loop
for ([Link]<String, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/4
// Iterating using forEach and lambda
[Link]((key, value) -> [Link](key + ": " + value));
}
}
4. Set (HashSet)
A `Set` is a collection of unique elements with no duplicates.
Functions:
`add()`:Adds an element to the set.
`remove()`: Removes an element.
`contains()`: Checks if the set contains an element.
`size()`: Returns the number of elements in the set.
Iteration:
java
import [Link];
public class SetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
[Link]("One");
[Link]("Two");
[Link]("Three");
// Iterating using for-each loop
for (String num : set) {
[Link](num);
}
// Iterating using iterator
[Link]().forEachRemaining([Link]::println);
}
}
5. Stack
A `Stack` is a last-in-first-out (LIFO) data structure.
Functions:
`push()`: Adds an element to the top of the stack.
`pop()`: Removes the top element.
`peek()`: Retrieves the top element without removing it.
`empty()`: Checks if the stack is empty.
Iteration:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/4
java
import [Link];
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
[Link]("First");
[Link]("Second");
[Link]("Third");
// Iterating using while loop and pop
while (![Link]()) {
[Link]([Link]());
}
// Iterating using for-each loop
for (String element : stack) {
[Link](element);
}
}
}
Each of these collections provides efficient ways to store and manipulate data, with iteration methods
that help access elements in different styles.
ChatGPT can make mistakes. Check important info.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/4