Course contents

Converting between arrays, lists, sets and maps

Conversions between arrays, lists, sets, maps, streams, iterators and JSON, including mutability, ordering, duplicate-key handling and collection views.

PremiumOften askedLesson11 min readCoreReviewed Oct 2026 · Java 21
On this page
  1. Picture it first: adapters, copies and views
  2. Arrays and lists under the hood
  3. Arrays, lists and sets
  4. Maps to arrays, lists and sets
  5. How to convert a list to a map
  6. Map to string conversion

🔄 What is collection conversion?

Collection conversion is taking the same logical data and giving it a different API shape: an array for a legacy method, a List for ordered processing, a Set for uniqueness, or a Map for key lookup. It acts like repacking groceries: the apples are the same, but a basket, a sealed box and a labelled shelf let you do different things with them. For example, String[] can become a List<String> for stream processing, then a LinkedHashSet<String> to remove duplicates while keeping first-seen order.

For this lesson, we'll focus on the conversions you write in interviews and code review: arrays to lists and sets, lists to sets and maps, maps to lists or arrays, stream results, iterators, primitive arrays, map-to-string output and short JSON array examples. We will lean on sibling lessons for the deeper engines: How ArrayList works, How HashSet, LinkedHashSet and TreeSet work, Everyday Map operations, Collections and Arrays utility methods and Immutable collections.

Below the line (out of scope)

The 30-second answer

Arrays.asList(array) gives a fixed-size list backed by the array: set writes through, add and remove throw. Use new ArrayList<>(Arrays.asList(array)) when you need a normal mutable list, and List.of(array) for an unmodifiable, null-rejecting Java 9+ list. Convert back with list.toArray(new String[0]) or, since Java 11, list.toArray(String[]::new); the zero-length array version is fine because modern JDKs allocate the correctly sized array internally. Use new LinkedHashSet<>(list) when unique values must keep first-seen order, new TreeSet<>(list) when sorted order matters, and remember that map keySet, values and entrySet are live views, so copy them when you need a snapshot. For list-to-map, use Collectors.toMap(keyMapper, valueMapper, mergeFunction, LinkedHashMap::new) when duplicates or order matter, and use groupingBy when keys repeat naturally.

What you’ll learn

  • Choose the right conversion between arrays and lists without accidentally creating a fixed-size list
  • Convert between List, Set and arrays while preserving order when the result depends on it
  • Turn a List into a Map with Collectors.toMap, including duplicate keys, merge functions and LinkedHashMap order
  • Explain why keySet(), values() and entrySet() are live views, and when to copy them
  • Collect streams into mutable or unmodifiable lists with toList, Collectors.toList and toCollection
  • Convert iterators and primitive arrays safely, including IntStream.boxed() and mapToInt
  • Know when string conversion is display-only and when JSON should use a real library

Interview radar

Asked often: Why does Arrays.asList(...).add(...) throw UnsupportedOperationException? Core follow-up: How do you handle duplicate keys in Collectors.toMap? Production angle: A cached keySet() view changes under a caller because someone later mutates the backing map.

01Picture it first: adapters, copies and views

The first question is not "which method exists?" It is what relationship you want after conversion. Some conversions create an adapter over the same storage, some create a shallow copy, and some create an unmodifiable result.

Think of the data as one set of cards on a table. Arrays.asList is a plastic frame over the same cards. new ArrayList<>(...) photocopies the card references into a new tray. List.of seals those references into a read-only packet.

Fig. 1

Conversion map between Array, List, Set, Map and Stream showing Arrays.asList, new ArrayList, toArray, new LinkedHashSet, Collectors.toMap, entrySet, stream and collect arrows

Conversion map between Array, List, Set, Map and Stream showing Arrays.asList, new ArrayList, toArray, new LinkedHashSet, Collectors.toMap, entrySet, stream and collect arrows

Premium lesson

Keep reading with the Java Pass

The rest of this lesson is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

  • Arrays and lists under the hood
  • Arrays, lists and sets
  • Maps to arrays, lists and sets
  • How to convert a list to a map
  • Map to string conversion

From ₹199 · one-time payment · No auto-renewal

Practise this topicUtilities & patterns: interview questions & scenarios
Further reading (8)