- Definition: Fixed-size sequential collection of elements of the same type.
- Syntax:
int[] arr = new int[10]; int[] arr = {1, 2, 3, 4, 5};
- Access:
arr[index]
- Length:
arr.length
- Definition: Resizable array implementation of the List interface.
- Import:
import java.util.ArrayList;
- Syntax:
ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.get(0); // Access element at index 0
- Methods:
add()
,get()
,remove()
,size()
- Definition: Doubly-linked list implementation of the List and Deque interfaces.
- Import:
import java.util.LinkedList;
- Syntax:
LinkedList<Integer> list = new LinkedList<>(); list.add(1); list.get(0); // Access element at index 0
- Methods:
add()
,get()
,remove()
,size()
- Definition: Implementation of the Set interface that uses a hash table.
- Import:
import java.util.HashSet;
- Syntax:
HashSet<Integer> set = new HashSet<>(); set.add(1); set.contains(1); // Check if set contains element
- Methods:
add()
,contains()
,remove()
,size()
- Definition: A NavigableSet implementation that uses a TreeMap.
- Import:
import java.util.TreeSet;
- Syntax:
TreeSet<Integer> set = new TreeSet<>(); set.add(1); set.contains(1); // Check if set contains element
- Methods:
add()
,contains()
,remove()
,size()
,first()
,last()
- Definition: Hash table based implementation of the Map interface.
- Import:
import java.util.HashMap;
- Syntax:
HashMap<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.get(1); // Get value for key 1
- Methods:
put()
,get()
,remove()
,size()
,containsKey()
- Definition: Red-Black tree based implementation of the NavigableMap interface.
- Import:
import java.util.TreeMap;
- Syntax:
TreeMap<Integer, String> map = new TreeMap<>(); map.put(1, "One"); map.get(1); // Get value for key 1
- Methods:
put()
,get()
,remove()
,size()
,firstKey()
,lastKey()
- Definition: Immutable sequence of characters.
- Syntax:
String str = "Hello";
- Methods:
length()
,charAt()
,substring()
,indexOf()
,toUpperCase()
,toLowerCase()
- Definition: Mutable sequence of characters.
- Import:
import java.lang.StringBuilder;
- Syntax:
StringBuilder sb = new StringBuilder("Hello"); sb.append(" World");
- Methods:
append()
,insert()
,delete()
,reverse()
,toString()
- Definition: Last-in-first-out (LIFO) stack of objects.
- Import:
import java.util.Stack;
- Syntax:
Stack<Integer> stack = new Stack<>(); stack.push(1); stack.pop(); // Remove and return the top element
- Methods:
push()
,pop()
,peek()
,isEmpty()
,size()
- Definition: First-in-first-out (FIFO) queue of objects.
- Import:
import java.util.Queue;
- Common Implementations:
LinkedList
,PriorityQueue
- Syntax:
Queue<Integer> queue = new LinkedList<>(); queue.add(1); queue.poll(); // Remove and return the head element
- Methods:
add()
,poll()
,peek()
,isEmpty()
,size()
- Definition: Double-ended queue, can be used as both stack and queue.
- Import:
import java.util.Deque;
- Common Implementations:
LinkedList
,ArrayDeque
- Syntax:
Deque<Integer> deque = new LinkedList<>(); deque.addFirst(1); deque.addLast(2); deque.pollFirst(); // Remove and return the first element
- Methods:
addFirst()
,addLast()
,pollFirst()
,pollLast()
,peekFirst()
,peekLast()
- Definition: Simple container to store a pair of objects.
- Import:
import javafx.util.Pair;
- Syntax:
Pair<Integer, String> pair = new Pair<>(1, "One"); pair.getKey(); // Get the first element pair.getValue(); // Get the second element
- Methods:
getKey()
,getValue()
- Definition: Container object which may or may not contain a non-null value.
- Import:
import java.util.Optional;
- Syntax:
Optional<String> optional = Optional.of("Hello"); optional.isPresent(); // Check if a value is present optional.get(); // Get the value
- Methods:
of()
,empty()
,isPresent()
,get()
,ifPresent()
,orElse()
- Definition: Sorts the specified list into ascending order.
- Import:
import java.util.Collections;
- Syntax:
List<Integer> list = new ArrayList<>(); Collections.sort(list);
- Custom Comparator:
Collections.sort(list, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } });
- Definition: Sorts the specified array into ascending order.
- Syntax:
int[] arr = {5, 3, 4, 1, 2}; Arrays.sort(arr);
- Definition: Introduced in Java 8, provides a functional approach to processing collections of objects.
- Import:
import java.util.stream.*;
- Syntax:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
- Common Methods:
- filter(): Filters elements based on a condition.
list.stream().filter(n -> n > 2);
- map(): Transforms elements.
list.stream().map(n -> n * 2);
- collect(): Collects elements into a new collection.
List<Integer> evenNumbers = list.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
- forEach(): Performs an action for each element.
list.stream().forEach(System.out::println);
- reduce(): Combines elements into a single result.
int sum = list.stream().reduce(0, Integer::sum);
- filter(): Filters elements based on a condition.