Depends on your data;

HashSet gives you an O(1) contains() method but doesn't preserve order.

ArrayList contains() is O(n) but you can control the order of the entries.

Array if you need to insert anything in between, worst case can be O(n), since you will have to move the data down and make room for the insertion. In Set, you can directly use SortedSet which too has O(n) too but with flexible operations.

I believe Set is more flexible.

Answer from JNL on Stack Overflow
🌐
Quora
quora.com › What-is-the-difference-between-Sets-and-Arrays-in-Java
What is the difference between Sets and Arrays in Java? - Quora
Answer (1 of 9): As mentioned before, a Set must not contain duplicate elements (which is allowed in arrays). This will (almost) always be true for a specific object with a specific address, so two members can never share the same memory address. This is however only a part of the truth. There ma...
Top answer
1 of 4
24

Depends on your data;

HashSet gives you an O(1) contains() method but doesn't preserve order.

ArrayList contains() is O(n) but you can control the order of the entries.

Array if you need to insert anything in between, worst case can be O(n), since you will have to move the data down and make room for the insertion. In Set, you can directly use SortedSet which too has O(n) too but with flexible operations.

I believe Set is more flexible.

2 of 4
3

The choice greatly depends on what do you want to do with it.

If it is what mentioned in your question:

I have a collection of objects that are guaranteed to be distinct (in particular, indexed by a unique integer ID). I also know exactly how many of them there are

If this is what you need to do, the you need neither of them. There is a size() method in Collection for which you can get the size of it, which mean how many of them there are in the collection.

If what you mean for "collection of object" is not really a collection, and you need to choose a type of collection to store your objects for further processing, then you need to know, for different kind of collections, there are different capabilities and characteristic.

First, I believe to have a fair comparison, you should consider using ArrayList instead Array, for which you don't need to deal with the reallocation.

Then it become the choice of ArrayList vs HashSet, which is quite straight-forward:

Do you need a List or Set? They are for different purpose: Lists provide you indexed access, and iteration is in order of index. While Sets are mainly for you to keep a distinct set of data, and given its nature, you won't have indexed access.

After you made your decision of List or Set to use, then it is a choice of List/Set implementation, normally for Lists, you choose from ArrayList and LinkedList, while for Sets, you choose between HashSet and TreeSet.

All the choice depends on what you would want to do with that collection of data. They performs differently on different action.

For example, an indexed access in ArrayList is O(1), in HashSet (though not meaningful) is O(n), (just for your interest, in LinkedList is O(n), in TreeSet is O(nlogn) )

For adding new element, both ArrayList and HashSet is O(1) operation. Inserting in the middle is O(n) for ArrayList, while it doesn't make sense in HashSet. Both will suffer from reallocation, and both of them need O(n) for the reallocation (HashSet is normally slower in reallocation, because it involve calculation of hash for each element again).

To find if certain element exists in the collection, ArrayList is O(n) and HashSet is O(1).

There are still lots of operations you can do, so it is quite meaningless to discuss for performance without knowing what you want to do.

Discussions

java - What would be better set, array or list? - Stack Overflow
I need to make a list of people and their time of arrival to a party, and when ever they leave I need to take them off this list. (the party maximum is 150) Set would provide me that in no case I ... More on stackoverflow.com
🌐 stackoverflow.com
When to use Sets vs Lists .
It's generally not useful to think about data structures without knowing what queries you're going to perform on them. In general, if you need to look up if the collection contains the element, use a Set. Otherwise a List will be faster for about every other operation. More on reddit.com
🌐 r/javahelp
49
14
July 31, 2022
java - What is the difference between Set and List? - Stack Overflow
One thing to note: positional access ... array vs linked list stackoverflow.com/questions/322715/… 2015-11-30T16:35:01.637Z+00:00 ... How are Sets indexed if not by positional access? (+1 for the ASCII-table) 2018-09-13T17:56:04.593Z+00:00 ... I don't like to read. This makes it very concise and simple. 2022-02-02T14:51:16.77Z+00:00 ... Ordered lists of element (unique or not) Conform to Java's interface ... More on stackoverflow.com
🌐 stackoverflow.com
Two dimensional ArrayList, need help with constructor
Thanks for all the replies, I had to hand over my project before any of the replies ticked in. But i read them now so thanks guys. I just ended up making a "flattened array" of generics and just convert them. I used this method to find the index: public int finnIndeks(int x, int y){ return (x+hight*y); } And from my "get" method i just converted from non-generic to generic sort of: public E hent(int x, int y){ return (E) tabell[finnIndeks(x, y)]; } I guess this is not the best way to go about this problem. But it certainly was the simplest. It's an unchecked cast from object E so thats sort of bad. More on reddit.com
🌐 r/java
5
0
February 17, 2012
🌐
Medium
hanwenzhang123.medium.com › basic-data-structures-in-java-arrays-lists-sets-and-maps-7db29a7b1b82
Basic Data Structures in Java— Arrays, Lists, Sets, Stacks, Queues, and Maps | by Hanwen Zhang | Medium
November 13, 2022 - The list is an ordered collection while the set is an unordered collection, the list allows duplicates while the set does not. A set is a collection that cannot contain duplicates.
🌐
GeeksforGeeks
geeksforgeeks.org › difference-between-a-set-and-an-array
Difference Between a Set and an Array
February 1, 2024 - Java · Python · JavaScript · Data Science · Machine Learning · Courses · Linux · DevOps · While you’re here, feast your eyes upon these popular · recommendations for you. Problem of the dayKnow more >> Courses @90% RefundAvail Now!
🌐
Programiz
programiz.com › java-programming › examples › convert-array-set
Java Program to Convert Array to Set (HashSet) and Vice-Versa
To convert array to set, we first convert it to a list using asList() as HashSet accepts a list as a constructor. Then, we initialize the set with the elements of the converted list. import java.util.*; public class ArraySet { public static void main(String[] args) { String[] array = {"a", "b", "c"}; Set<String> set = new HashSet<>(Arrays.stream(array).collect(Collectors.toSet())); System.out.println("Set: " + set); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-array-to-set-in-java
Program to convert Array to Set in Java - GeeksforGeeks
July 11, 2025 - An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in the heap segment. Set in Java is a part of java.util package and extends java.util.Collection interface.
🌐
Baeldung
baeldung.com › home › java › java collections › converting between an array and a set in java
Converting between an Array and a Set in Java | Baeldung
June 9, 2025 - In this short article we’re going to look at converting between an array and a Set – first using plain java, then Guava and the Commons Collections library from Apache.
Find elsewhere
🌐
javaspring
javaspring.net › blog › java-set-to-array
Java Set to Array: A Comprehensive Guide — javaspring.net
In Java, the `Set` interface is a part of the Java Collections Framework, which does not allow duplicate elements. On the other hand, arrays are fixed-size data structures that store elements of the same type.
🌐
Medium
medium.com › @viranthrocky › understanding-the-difference-between-set-and-arraylist-in-java-4bac4b5214d9
🔍✨ Understanding the Difference Between Set and ArrayList in Java 🧠💻 | by Viranth Dharmalingam | Medium
April 11, 2025 - However, if an existing object shares the same hash code, Java uses the equals() method to check if the new object is truly equal to the existing one. If equals() returns true, the object is considered a duplicate and not added. If equals() returns false, the object is considered unique, and it is added, even though the hash codes match. This mechanism ensures that a Set never allows duplicate elements, unlike a List.
🌐
javathinking
javathinking.com › blog › convert-array-to-set-java
Convert Array to Set in Java — javathinking.com
An array is a fixed-size collection that stores elements in a contiguous block of memory, and elements can be accessed using an index. On the other hand, a `Set` is a collection that does not allow duplicate elements.
🌐
Medium
medium.com › front-end-weekly › es6-set-vs-array-what-and-when-efc055655e1a
ES6 — Set vs Array — What and when? | by Maya Shavin | Frontend Weekly | Medium
November 9, 2018 - One of the biggest differences here, you may notice, is that elements in Array can be duplicate (unless you tell it not to be), and in Set, they just can’t (regardless what you decide).
🌐
Medium
paoladolcemascolo.medium.com › can-i-interest-you-in-a-set-the-set-vs-the-array-in-javascript-b410aad0ba9c
Can I interest you in a Set? The set vs. the array in Javascript | by Paola Dolcemascolo | Medium
March 12, 2021 - Another very important distinction ... a Set is considered a keyed collection, whereas an Array is considered an indexed collection. The former, as its name implies, is a collection that uses keys…and the elements are iterable in the order ...
🌐
DEV Community
dev.to › david4473 › set-vs-array-14c0
Set vs Array - DEV Community
October 11, 2021 - Set stores data in collections with keys that are iterable in the order of insertion. An array on the other hand - is a global object that stores high-level list-like objects called arrays in the consecutive memory for later use.
🌐
Coderanch
coderanch.com › t › 614766 › java › Set-methods-arrays-righter
Get/Set methods and arrays? What's the right (or righter) way to do it? (Beginning Java forum at Coderanch)
June 28, 2013 - The problem I get to is when I have a class that contains an array. How would I efficiently create a get and a set for an int[], boolean[], or WhatHaveYou[]? In a practice program, I created a SalesTeam class, I think I made a constructor and get/sets that works, but I'm really looking for one that is acceptable by convention/standard : Interesting. I am surprised that no one mentioned this yet... Or perhaps someone did, and I missed it. JavaBean properties actually defines a standard for array elements -- or specifically, Indexed Properties.
🌐
Java67
java67.com › 2012 › 07 › difference-between-arraylist-hashset-in-java.html
Difference between ArrayList and HashSet in Java? Answer with Example | Java67
... The main difference between ArrayList and HashSet is that one is a List implementation while the other is a Set implementation. It means all the differences between a List data structure and a Set data structure also apply to this pair.
🌐
GeeksforGeeks
geeksforgeeks.org › java › array-set-method-in-java
Array set() method in Java - GeeksforGeeks
November 10, 2022 - Below programs illustrate the Array.set() method: Program 1 : ... // Java code to demonstrate set() method of Array class import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining a String array String s[] = { "Geeks", "is", "Geeks" }; System.out.print("Before Set : "); // printing the array for (String x : s) { System.out.print(x); } // set method of class Array Array.set(s, 1, "for"); System.out.print("\nAfter Set : "); // printing array for (String x : s) { System.out.print(x); } } }
🌐
Oracle
docs.oracle.com › javase › tutorial › reflect › special › arraySetGet.html
Getting and Setting Arrays and Their Components (The Java™ Tutorials > The Reflection API > Arrays and Enumerated Types)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. Just as in non-reflective code, an array field may be set or retrieved in its entirety or component by component.