Introduction to Java Collections
In Java, the collections framework provides a set of interfaces and classes that allow you to store, manipulate, and retrieve collections of objects. These collections can be used to efficiently manage and work with groups of related data.
One of the key benefits of using collections in Java is the ability to use pre-defined data structures that are optimized for specific use cases. This can greatly simplify and enhance the efficiency of your code.
The Java collections framework includes various interfaces and classes, such as:
ArrayList
: A resizable array implementation of theList
interface.LinkedList
: A doubly-linked list implementation of theList
interface.HashMap
: A hash table implementation of theMap
interface.Stack
: A stack implementation of theList
interface.Queue
: An interface that specifies a simple queue (first-in, first-out) data structure.Set
: An interface that represents an unordered collection of unique elements.TreeSet
: A sorted set implementation of theSet
interface.
Let's take a look at an example that demonstrates the usage of some of these collection classes in Java:
TEXT/X-JAVA
1// Example of using various Java collection classes
2ArrayList<String> arrayList = new ArrayList<>();
3LinkedList<String> linkedList = new LinkedList<>();
4HashMap<String, Integer> hashMap = new HashMap<>();
5Stack<String> stack = new Stack<>();
6Queue<String> queue = new LinkedList<>();
7Set<String> set = new TreeSet<>();
8
9// Add elements to ArrayList
10arrayList.add("Element 1");
11arrayList.add("Element 2");
12arrayList.add("Element 3");
13
14// Add elements to LinkedList
15linkedList.add("Element A");
16linkedList.add("Element B");
17linkedList.add("Element C");
18
19// Add element to HashMap
20hashMap.put("Key 1", 1);
21hashMap.put("Key 2", 2);
22hashMap.put("Key 3", 3);
23
24// Push elements to Stack
25stack.push("Element X");
26stack.push("Element Y");
27stack.push("Element Z");
28
29// Add elements to Queue
30queue.add("Element I");
31queue.add("Element II");
32queue.add("Element III");
33
34// Add elements to Set
35set.add("Element Alpha");
36set.add("Element Bravo");
37set.add("Element Charlie");
xxxxxxxxxx
50
}
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.Stack;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
// Example of using various Java collection classes
ArrayList<String> arrayList = new ArrayList<>();
LinkedList<String> linkedList = new LinkedList<>();
HashMap<String, Integer> hashMap = new HashMap<>();
Stack<String> stack = new Stack<>();
Queue<String> queue = new LinkedList<>();
Set<String> set = new TreeSet<>();
// Add elements to ArrayList
arrayList.add("Element 1");
arrayList.add("Element 2");
arrayList.add("Element 3");
// Add elements to LinkedList
linkedList.add("Element A");
linkedList.add("Element B");
linkedList.add("Element C");
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment