How to convert an Array to a Set in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How to convert an ArrayList containing Integers to primitive int array?
- How to convert an ArrayList to a strongly typed generic list without using a foreach?
- How to convert an entire MySQL database characterset and collation to UTF-8?
- How to convert an int array to String with toString method in Java
- How to convert an Instant to a date format?
- How to convert an Iterator to a Stream?
- How to convert byte array to string
- How to convert byte array to string and vice versa?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.