How to convert an Array to a Set in Java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting an array to a Set in Java is common when you want to remove duplicates, test membership quickly, or switch from ordered storage to uniqueness-based storage. The right conversion depends on whether you care about preserving insertion order, sorting the result, or handling primitive arrays rather than object arrays.
Core Sections
The most direct conversion for object arrays
For arrays of reference types such as String[] or Integer[], the standard pattern is to turn the array into a list and then construct a set from that list.
This removes duplicates automatically because sets do not allow duplicate elements. If you do not care about order, HashSet is usually the simplest choice.
Preserve insertion order with LinkedHashSet
A HashSet does not preserve the original order of the array. If order matters, use LinkedHashSet.
This keeps the first occurrence order from the array while still removing duplicates.
Sort the result with TreeSet
If you want uniqueness plus sorted output, use TreeSet instead.
The tradeoff is that TreeSet sorts by natural ordering or a comparator, which changes iteration order and usually has different performance characteristics from HashSet.
Stream-based conversion in modern Java
If you are already using streams, collecting directly to a set can be more readable.
This is concise, but it is worth remembering that Collectors.toSet() does not promise a specific set implementation. If you care about order or type, collect into a concrete implementation explicitly.
Primitive arrays need special handling
A common surprise is that Arrays.asList() does not behave the same way for primitive arrays such as int[]. Instead of creating a list of individual integers, it creates a list containing the entire array as one element.
To convert a primitive array properly, use Arrays.stream() and then box the values.
That is the correct pattern for primitive arrays.
Common Pitfalls
- Using
HashSetwhen iteration order matters can produce unexpected ordering in later code. - Assuming
Collectors.toSet()returns a specific implementation leads to brittle code. - Calling
Arrays.asList()on a primitive array such asint[]does not produce a list of boxed elements. - Forgetting that duplicate removal is automatic can hide data issues if duplicates were actually meaningful.
- Choosing
TreeSetfor conversion without intending to sort the values can silently change the result order.
Summary
- For object arrays,
new HashSet<>(Arrays.asList(array))is the simplest array-to-set conversion. - Use
LinkedHashSetif you need insertion order andTreeSetif you need sorted results. - Streams are concise, but
Collectors.toSet()does not guarantee a particular set type. - Primitive arrays need
Arrays.stream(...).boxed()before collection into aSet<Integer>. - Pick the set implementation based on uniqueness, ordering, and performance requirements rather than converting mechanically.

