Collections in Java

Profile picture for user arilio666

Collection in Java is a framework that provides an architecture for storing and modifying a group of items. Java collection classes implement many data structures such as lists, sets, maps, queues, etc. Here are some examples of collection classes that are commonly used in Java:

List

List is an ordered collection where duplicates are allowed. Java provides two introductory classes to implement a list: ArrayList and LinkedList.

List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
System.out.println(list); // output: [apple, banana, cherry]
// Accessing elements by index
String firstElement = list.get(0);
System.out.println(firstElement); // output: apple
// Removing an element by value
list.remove("banana");
System.out.println(list); // output: [apple, cherry]

Set

Set is an unordered collection that does not permit duplicates. Java provides three basic classes to implement a set: HashSet, TreeSet, and LinkedHashSet.

Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("cherry");
set.add("apple"); // Duplicate value, will not be added
System.out.println(set); // output: [banana, cherry, apple]
// Checking if a value exists in the set
boolean containsBanana = set.contains("banana");
System.out.println(containsBanana); // output: true
// Removing an element by value
set.remove("cherry");
System.out.println(set); // output: [banana, apple]

Map

Map is a collection of  pairs in key-value where each key corresponds to a value. Java provides three primary classes to implement a map: HashMap, TreeMap, and LinkedHashMap.

Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
System.out.println(map); // output: {banana=2, cherry=3, apple=1}
// Accessing a value by key
int cherryValue = map.get("cherry");
System.out.println(cherryValue); // output: 3
// Removing an entry by key
map.remove("banana");
System.out.println(map); // output: {cherry=3, apple=1}

Queue

A queue is a collection that follows the first-in, first-out (FIFO) principle. Java provides two major classes to implement a queue: LinkedList and PriorityQueue.

Queue<String> queue = new LinkedList<>();
queue.offer("apple");
queue.offer("banana");
queue.offer("cherry");
System.out.println(queue); // output: [apple, banana, cherry]
// Removing and returning the head of the queue
String head = queue.poll();
System.out.println(head); // output: apple
System.out.println(queue); // output: [banana, cherry]

Stack

A stack is a collection that follows the last-in, first-out (LIFO) principle. Stack is a Java class that implements a stack.

Stack<String> stack = new Stack<>();
stack.push("apple");
stack.push("banana");
stack.push("cherry");
System.out.println(stack); // output: [apple, banana, cherry]
// Removing and returning the top element of the stack
String top = stack.pop();
System.out.println(top); // output: cherry
System.out.println(stack); // output: [apple, banana]

Conclusion

These collection classes are part of the Java Collections Framework, which provides a standardized way to work with collections.

Tags