Array vs ArrayList in Java: Differences and When to Use Each
Array vs ArrayList in Java compared on size, capacity growth, boxing, type safety and Big-O cost, with a decision table and one-line conversions.
Array vs ArrayList in Java compared on size, capacity growth, boxing, type safety and Big-O cost, with a decision table and one-line conversions.
Learn to sort ArrayList in Java in ascending and descending order with List.sort(), Comparator.reverseOrder(), Collections.sort() and streams.
Learn to flatten a list of lists in Java with flatMap(), addAll() loops, mapMulti(), recursion for deep nesting, Guava and Eclipse Collections.
Arrays.asList() vs new ArrayList() in Java explained with examples, covering fixed size, the shared array, List.of(), primitive arrays and conversions.
Learn the Java ArrayList with tested examples for get(), size() vs capacity, subList(), ensureCapacity(), sorting, streams and Java 21 getFirst().
Learn how Java CopyOnWriteArrayList copies its array on writes, why its iterators never fail, addIfAbsent(), Java 21 methods and when to use it.
Learn to remove duplicates from ArrayList in Java with distinct(), LinkedHashSet and removeIf(), plus duplicate objects by a field and ignoring case.
ArrayList removeIf() removes every element that matches a Predicate. See return values, exceptions, Map views, lists that refuse it and stream filters.
ArrayList forEach() runs a Consumer on each element. Learn method references, andThen(), break and index workarounds, CME rules and Map.forEach().
Java ArrayList clone() creates a shallow copy. Learn how it differs from a deep copy and how to deep copy a list with copy constructors and streams.
LinkedList vs ArrayList in Java compared on memory, time complexity and real use cases, plus ArrayDeque advice and how to convert between them.
ArrayList vs Vector in Java compared on synchronization, capacity growth and iteration, with modern replacements for Vector, Stack and Enumeration.
Learn to convert Set to List in Java with the ArrayList constructor, List.copyOf() and streams, keep or sort the order, and convert a List to a Set.
Convert array to ArrayList in Java with new ArrayList<>(Arrays.asList()), List.of() or streams. Covers fixed-size views, int[] arrays and the way back.
Learn how to check if element exists in ArrayList in Java with contains(), indexOf(), lastIndexOf() and anyMatch(), incl. custom objects and fast lookups.
Learn to replace element in ArrayList in Java by index or value with set(), indexOf(), Collections.replaceAll() and List.replaceAll(), with safe examples.
Check if ArrayList is empty in Java with isEmpty() or size(), handle null lists with Commons and Spring, and empty a list with clear() vs removeAll().
Merge two ArrayLists in Java with addAll() or Stream.concat(), drop duplicates with LinkedHashSet, merge sorted lists and merge objects by key.
Serialize an ArrayList to a file or a byte array with ObjectOutputStream, read it back with ObjectInputStream, handle the unchecked cast, and see when JSON with Jackson is the better choice.
Swap two elements in ArrayList with Collections.swap(), by value or with set(), avoid IndexOutOfBoundsException and move items with rotate().
Create a synchronized ArrayList with Collections.synchronizedList() or CopyOnWriteArrayList, lock iteration correctly and pick the right option.
Compare two lists in Java with equals() or ignoring order, count duplicates correctly, and find additional, missing and common items with removeAll().
Learn how to add multiple elements to ArrayList in Java with addAll(), Collections.addAll(), add(index), addFirst() and streams, plus common exceptions.
Learn to remove element from ArrayList in Java by index, value or condition with remove(), removeIf(), removeAll() and retainAll(), plus common traps.
Learn to iterate ArrayList in Java with the for-each loop, index loop, forEach(), Iterator, reversed() and streams, and remove elements without errors.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.