Skip to main content
Arrays Utility API

Arrays ↔ Collections

Arrays.asList(a,b,c) → fixed-size List (no add/remove). new ArrayList<>(Arrays.asList(...)) → resizable. int[] → List<Integer> via stream().boxed(). List<Integer> → int[] via mapToInt(). new HashSet<>(list) to deduplicate. list.toArray(new T[0]) back to array.

O(n)
·
O(n)

How It Works

Java's split between primitive arrays and collections makes conversion patterns essential. Arrays.asList(a, b, c) wraps elements in a fixed-size list — reads and sets work, but add or remove throws UnsupportedOperationException; wrap it as new ArrayList<>(Arrays.asList(...)) for a resizable copy. Primitives need streams: int[] becomes List<Integer> via Arrays.stream(arr).boxed().collect(Collectors.toList()), and the reverse uses list.stream().mapToInt(Integer::intValue).toArray().

Deduplication drops out of new HashSet<>(list), and list.toArray(new T[0]) returns to array land for object types. The core gotcha: Arrays.asList on an int[] produces a one-element List<int[]> rather than a list of integers, because primitives cannot be generic type arguments.

Step-by-Step Visualization

Arrays.asList(arr) — fixed-size wrapper. Elements editable, but add/remove throws
arr→List
0
arr→ArrayList
1
int[]→List
2
List→int[]
3
List→Set
4
Set→List
5
List→arr
6
Arrays.asList()FIXED-SIZE ⚠
new ArrayList<>(asList())resizable ✓
1/4

Code

Java
String[]  arr  = {"a", "b", "c"};
int[]     nums = {1, 2, 3};

// ─── Array → List ────────────────────────────────────────────
List<String> fixed   = Arrays.asList(arr);                  // fixed-size ⚠
List<String> mutable = new ArrayList<>(Arrays.asList(arr)); // resizable ✓

// ─── int[] → List<Integer> (must box first) ──────────────────
List<Integer> boxed = Arrays.stream(nums).boxed().collect(Collectors.toList());

// ─── List<Integer> → int[] ────────────────────────────────────
int[] back = list.stream().mapToInt(Integer::intValue).toArray();

// ─── List ↔ Set ───────────────────────────────────────────────
Set<Integer>  deduped  = new HashSet<>(list);       // deduplicate
List<Integer> fromSet  = new ArrayList<>(set);      // Set → List

// ─── List → Array ─────────────────────────────────────────────
String[] toArr = list.toArray(new String[0]);

// ─── Map → List ───────────────────────────────────────────────
List<Integer> keys   = new ArrayList<>(map.keySet());
List<Integer> values = new ArrayList<>(map.values());

Tips & Gotchas

1Arrays.asList() returns a FIXED-SIZE list — add/remove throws UnsupportedOperationException
2Wrap with new ArrayList<>(...) to make it resizable
3int[] cannot be used as List<Integer> directly — must stream().boxed()
4list.toArray(new T[0]) is the canonical typed array conversion

Practice Problems

  • 1Contains Duplicate
  • 2Intersection of Two Arrays
  • 3Group Anagrams
  • 4Top K Frequent Elements

About the Arrays Utility API Pattern

java.util.Arrays methods and array ↔ collection conversion patterns. These utilities handle sorting, searching, copying, filling, and bridging between primitive arrays and Java collections.

Key insight

When you see 'subarray', 'contiguous', or 'in-place', think arrays. The key is reducing brute-force O(n²) to O(n) using sliding window, two pointers, or prefix sums.

Common Array Interview Problems

  • Two Sum
  • Best Time to Buy & Sell Stock
  • Maximum Subarray
  • Merge Intervals
  • Product of Array Except Self
  • Container With Most Water

Frequently Asked Questions

Why does Arrays.asList(myIntArray) not give me a list of integers?

Generics cannot hold primitives, so the compiler treats the entire int[] as a single element and returns List<int[]> of size one. Convert with Arrays.stream(myIntArray).boxed().collect(Collectors.toList()), or switch the source array to Integer[] if boxing up front is acceptable.

Why does adding to the result of Arrays.asList throw an exception?

Arrays.asList returns a fixed-size view backed by the original array, so structural changes like add and remove are unsupported, though set works and writes through to the array. Copy it into new ArrayList<>(...) whenever you need a genuinely mutable list.

What is the fastest way to deduplicate an array in Java?

Pour it into a HashSet — new HashSet<>(list) for objects, or a loop of set.add(x) for primitives — for O(n) average time at the cost of losing order. Use LinkedHashSet to keep insertion order, or Arrays.stream(arr).distinct() for a stream-based one-liner.