Skip to content

Collection Methods

This cheat sheet lists commonly used methods from core Java libraries (java.util, java.lang, java.util.stream, etc.) for typical data structure and backend tasks. Each section covers a topic with a table of key functions, descriptions, and one-line example usage.

#Method/FunctionDescriptionExample
1Collections.sort(List<T>)Sorts the list into natural (ascending) order.Collections.sort(list);
2Collections.reverse(List<?>)Reverses the order of elements in the list.Collections.reverse(list);
3Collections.shuffle(List<?>)Randomly permutes (shuffles) the list.Collections.shuffle(list);
4Collections.binarySearch(List<?>, key)Searches for key in a sorted list; returns index if found, or -(insertionPoint)-1 if not.int idx = Collections.binarySearch(list, key);
5Collections.max(Collection<? extends T>)Returns the maximum element of the collection (natural order or by supplied comparator).Collections.max(list);
6Collections.min(Collection<? extends T>)Returns the minimum element of the collection.Collections.min(list);
7Collections.frequency(Collection<?>, Object)Counts how many times the specified element appears in the collection.Collections.frequency(list, elem);
8Collections.fill(List<? super T>, T)Replaces all elements in the list with the given value.Collections.fill(list, 0);
9Collections.replaceAll(List<T>, T oldVal, T newVal)Replaces all occurrences of oldVal with newVal.Collections.replaceAll(list, 1, 100);
#Method/FunctionDescriptionExample
1List.add(E)Appends the element to the end of the list.list.add("item");
2List.add(int, E)Inserts the element at the given index.list.add(0, "first");
3List.get(int)Returns the element at the specified index.String s = list.get(2);
4List.set(int, E)Replaces the element at index with a new value.list.set(1, "new");
5List.remove(int)Removes the element at the specified index.list.remove(0);
6List.size()Returns the number of elements.int n = list.size();
7List.isEmpty()Checks if the list has no elements.boolean empty = list.isEmpty();
8List.contains(Object)Checks if the list contains the given element.boolean has = list.contains("item");
9List.indexOf(Object)Returns the index of the first occurrence or -1 if not found.int i = list.indexOf("item");
10List.subList(int, int)Returns a view between indices.List<String> part = list.subList(1, 3);
11List.sort(Comparator<? super E>)Sorts using comparator or natural order. (Java 8+)list.sort(Comparator.naturalOrder());
12List.removeIf(Predicate<? super E>)Removes all elements that match predicate. (Java 8+)list.removeIf(x -> x < 0);
#Method/FunctionDescriptionExample
1Set.add(E)Adds the element (ignores duplicates).set.add(42);
2Set.remove(Object)Removes element.set.remove(42);
3Set.contains(Object)Checks if contains.boolean ok = set.contains(5);
4Set.size()Returns number of elements.int n = set.size();
5Set.isEmpty()Checks if empty.boolean empty = set.isEmpty();
6Set.clear()Removes all elements.set.clear();
7Set.removeIf(Predicate<? super E>)Removes elements matching predicate. (Java 8+)set.removeIf(x -> x % 2 == 0);
8Set.retainAll(Collection<?>)Keeps only elements present in another collection (intersection).set.retainAll(otherSet);
#Method/FunctionDescriptionExample
1Map.put(K, V)Associates value with key.map.put("key", 100);
2Map.get(Object)Returns value for key or null.int v = map.get("key");
3Map.containsKey(Object)Checks if key exists.map.containsKey("key");
4Map.size()Returns entry count.int n = map.size();
5Map.isEmpty()Checks if map has no entries.map.isEmpty();
6Map.remove(Object)Removes mapping for key.map.remove("key");
7Map.remove(Object, Object)Removes entry if key mapped to value.map.remove("key", 100);
8Map.keySet()Returns keys as Set.Set<String> keys = map.keySet();
9Map.values()Returns values as Collection.map.values();
10Map.putIfAbsent(K,V)Adds mapping if key missing. (Java 8+)map.putIfAbsent("key", 50);
11Map.getOrDefault(K, V)Returns value or default. (Java 8+)map.getOrDefault("key", 0);
12Map.computeIfAbsent(K, Function)Computes value if absent. (Java 8+)map.computeIfAbsent("k", k -> new ArrayList<>());
13Map.merge(K, V, BiFunction)Merges values. (Java 8+)map.merge("k", 1, Integer::sum);
14Map.forEach(BiConsumer)Iterates entries. (Java 8+)map.forEach((k,v)->System.out.println(k+":"+v));
#Method/FunctionDescriptionExample
1Queue.add(E)Inserts element (throws if full).queue.add(10);
2Queue.offer(E)Inserts element (false if full).queue.offer(20);
3Queue.poll()Retrieves/removes head or null if empty.queue.poll();
4Queue.peek()Retrieves head without removal.queue.peek();
5Queue.remove()Removes head (throws if empty).queue.remove();
6Queue.element()Returns head (throws if empty).queue.element();
7Queue.size()Returns count.queue.size();
8Queue.isEmpty()Checks if empty.queue.isEmpty();
#Method/FunctionDescriptionExample
1String.length()Returns length."Hello".length();
2String.charAt(int)Returns char at index."Hello".charAt(1);
3String.substring(int,int)Returns substring."Hello".substring(1,4);
4String.indexOf(String)First occurrence index."abcabc".indexOf("bc");
5String.lastIndexOf(String)Last occurrence index."abcabc".lastIndexOf("bc");
6String.contains(CharSequence)Checks substring presence."Hello".contains("ell");
7String.startsWith(String)Checks prefix."Hello".startsWith("He");
8String.endsWith(String)Checks suffix."Hello".endsWith("lo");
9String.equals(Object)Compares for equality."a".equals("A");
10String.equalsIgnoreCase(String)Ignores case."a".equalsIgnoreCase("A");
11String.toLowerCase()Converts to lowercase."Hello".toLowerCase();
12String.toUpperCase()Converts to uppercase."Hello".toUpperCase();
13String.trim()Removes whitespace." Hi ".trim();
14String.replace(CharSequence, CharSequence)Replaces literal text."aabb".replace("a","c");
15String.replaceAll(String, String)Replaces using regex."abc123".replaceAll("\\d","");
16String.split(String)Splits using regex."a,b,c".split(",");
17String.join(CharSequence, CharSequence...)Joins with delimiter. (Java 8+)String.join("-", "a","b","c");
18String.format(String,Object...)Formats string.String.format("Hi %s", "Bob");
#Method/FunctionDescriptionExample
1Arrays.sort(int[])Sorts ascending.Arrays.sort(arr);
2Arrays.sort(T[], Comparator)Sorts with comparator.Arrays.sort(arr, Collections.reverseOrder());
3Arrays.binarySearch(int[], key)Searches sorted array.Arrays.binarySearch(arr, key);
4Arrays.equals(int[], int[])Compares arrays.Arrays.equals(a,b);
5Arrays.toString(Object[])Returns string form.Arrays.toString(arr);
6Arrays.asList(T...)Returns fixed-size List view.Arrays.asList(1,2,3);
7Arrays.copyOf(T[], int)Copies to new length.Arrays.copyOf(arr,5);
8Arrays.copyOfRange(T[], int, int)Copies range.Arrays.copyOfRange(arr,0,3);
9Arrays.fill(T[], T)Fills array.Arrays.fill(arr,0);
10Arrays.parallelSort(int[])Parallel sort (Java 8+)Arrays.parallelSort(arr);
11Arrays.stream(T[])Creates Stream. (Java 8+)Arrays.stream(arr);
#Method/FunctionDescriptionExample
1Collection.stream()Creates stream.list.stream()
2Stream.filter(Predicate)Filters elements.list.stream().filter(n -> n>0)
3Stream.map(Function)Maps elements.list.stream().map(String::length)
4Stream.flatMap(Function)Flattens nested streams.listOfLists.stream().flatMap(List::stream)
5Stream.distinct()Removes duplicates.list.stream().distinct()
6Stream.sorted()Sorts elements.list.stream().sorted()
7Stream.limit(long)Limits count.list.stream().limit(5)
8Stream.skip(long)Skips first n.list.stream().skip(2)
9Stream.forEach(Consumer)Iterates elements.list.stream().forEach(System.out::println)
10Stream.collect(Collector)Collects to container.list.stream().collect(Collectors.toList())
11Stream.reduce(T, BinaryOperator)Reduces values.list.stream().reduce(0, Integer::sum)
12Stream.count()Counts elements.list.stream().count()
13Stream.anyMatch(Predicate)Any matches predicate.list.stream().anyMatch(x->x>5)
14Stream.allMatch(Predicate)All match predicate.list.stream().allMatch(x->x>0)
15Stream.findFirst()Returns first element Optional.list.stream().findFirst()
16Stream.min(Comparator)Minimum element.list.stream().min(Integer::compareTo)
17Stream.max(Comparator)Maximum element.list.stream().max(Integer::compareTo)
#Method/FunctionDescriptionExample
1Optional.of(T)Creates non-null Optional.Optional.of("test")
2Optional.ofNullable(T)Creates Optional or empty.Optional.ofNullable(maybeNull)
3Optional.empty()Empty Optional.Optional.empty()
4Optional.isPresent()Checks if value present.opt.isPresent()
5Optional.ifPresent(Consumer)Runs if present.opt.ifPresent(x->System.out.println(x));
6Optional.orElse(T)Returns value or default.opt.orElse("fallback");
7Optional.orElseGet(Supplier)Returns value or supplier result.opt.orElseGet(() -> "fallback");
8Optional.orElseThrow(Supplier)Throws exception if empty.opt.orElseThrow(() -> new RuntimeException("No value"));
9Optional.filter(Predicate)Filters value.opt.filter(x -> x.startsWith("A"));
10Optional.map(Function)Transforms if present.opt.map(String::toUpperCase);
11Optional.flatMap(Function)Flattens nested Optional.opt.flatMap(x -> Optional.of(x.length()));
12Optional.ifPresentOrElse(Consumer,Runnable)Runs consumer or else. (Java 9+)opt.ifPresentOrElse(x->System.out.println(x),()->System.out.println("Empty"));