DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Java Collections Cheat Sheet with Examples

๐Ÿง  Java Collections Cheat Sheet with Examples

A concise but comprehensive reference for Java Collections โ€” including Collection, List, Set, Queue, Map, and the Collections utility class.


๐Ÿงฉ 1. Collection Interface (Common to List, Set, etc.)

Method Description Example
add(E e) Add element list.add("Apple");
addAll(Collection<? extends E> c) Add all elements from another collection list.addAll(anotherList);
remove(Object o) Remove element set.remove("Banana");
removeAll(Collection<?> c) Remove all elements in another collection list.removeAll(anotherList);
retainAll(Collection<?> c) Keep only elements in another collection list.retainAll(anotherList);
contains(Object o) Check if element exists set.contains("Apple");
containsAll(Collection<?> c) Check if all elements exist list.containsAll(anotherList);
isEmpty() Check if empty list.isEmpty();
size() Number of elements list.size();
clear() Remove all elements list.clear();
toArray() Convert to array Object[] arr = set.toArray();

๐Ÿ“‹ 2. List Interface Methods

Method Description Example
get(int index) Get element at index list.get(2);
set(int index, E element) Replace element at index list.set(1, "Orange");
add(int index, E element) Insert at index list.add(0, "Mango");
remove(int index) Remove element at index list.remove(2);
indexOf(Object o) First occurrence list.indexOf("Apple");
lastIndexOf(Object o) Last occurrence list.lastIndexOf("Apple");
subList(int from, int to) Get part of list list.subList(1, 3);
listIterator() Iterator in both directions ListIterator<String> it = list.listIterator();

Example:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list.get(1)); // Banana
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ข 3. Set Interface Methods

Method Description Example
add(E e) Add element (no duplicates) set.add("Apple");
remove(Object o) Remove element set.remove("Apple");
contains(Object o) Check existence set.contains("Apple");
size() Number of elements set.size();
isEmpty() Check if empty set.isEmpty();
clear() Remove all set.clear();

Example:


 Set<String> set = new HashSet<>();
 set.add("Apple");
 set.add("Banana");
 set.add("Apple"); // Ignored
 System.out.println(set.size()); // 2


Enter fullscreen mode Exit fullscreen mode

๐Ÿ•’ 4. Queue Interface Methods

Method Description Example
add(E e) Add element queue.add("Apple");
offer(E e) Add element, returns false if full queue.offer("Banana");
remove() Remove and return head queue.remove();
poll() Remove head, return null if empty queue.poll();
element() Return head, exception if empty queue.element();
peek() Return head, null if empty queue.peek();

Example:


Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
System.out.println(queue.poll()); // Apple
System.out.println(queue.peek()); // Banana

Enter fullscreen mode Exit fullscreen mode

๐Ÿ—บ๏ธ 5. Map Interface Methods

Method Description Example
put(K key, V value) Add key-value map.put(1, "Apple");
putAll(Map<? extends K, ? extends V> m) Add all from another map map.putAll(otherMap);
get(Object key) Get value by key map.get(1);
remove(Object key) Remove key-value map.remove(1);
containsKey(Object key) Check key exists map.containsKey(1);
containsValue(Object value) Check value exists map.containsValue("Apple");
keySet() Get all keys map.keySet();
values() Get all values map.values();
entrySet() Get key-value pairs map.entrySet();
isEmpty() Check if empty map.isEmpty();
size() Number of entries map.size();
clear() Remove all entries map.clear();

Example:


Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
System.out.println(map.get(1)); // Apple
System.out.println(map.keySet()); // [1, 2]

Enter fullscreen mode Exit fullscreen mode

โš™๏ธ 6. Collections Utility Class Methods

Method Description Example
sort(List<T> list) Sort list naturally Collections.sort(list);
sort(List<T> list, Comparator c) Sort with custom comparator Collections.sort(list, Comparator.reverseOrder());
reverse(List<?> list) Reverse list Collections.reverse(list);
shuffle(List<?> list) Random shuffle Collections.shuffle(list);
swap(List<?> list, int i, int j) Swap elements Collections.swap(list, 0, 1);
max(Collection c) Maximum element Collections.max(list);
min(Collection c) Minimum element Collections.min(list);
frequency(Collection c, Object o) Count occurrences Collections.frequency(list, "Apple");
binarySearch(List list, T key) Binary search (sorted list) Collections.binarySearch(list, "Banana");
fill(List list, T obj) Fill list with obj Collections.fill(list, "Empty");

Example:

List<String> list = new ArrayList<>(Arrays.asList("Banana", "Apple", "Cherry"));
Collections.sort(list);
System.out.println(list); // [Apple, Banana, Cherry]
Collections.reverse(list);
System.out.println(list); // [Cherry, Banana, Apple]
Enter fullscreen mode Exit fullscreen mode

โœ… Final Notes

  • Collection is the root interface for most data structures.
  • Use List when order matters.
  • Use Set to prevent duplicates.
  • Use Queue for FIFO (First-In-First-Out).
  • Use Map for key-value pairs.
  • Use Collections utility methods for sorting, searching, and manipulation.

๐Ÿ’ก Save this cheat sheet and keep it handy for interviews, quick reference, or daily coding practice!

Top comments (0)