🌐
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!
🌐
Medium
iampravo.medium.com › comparing-data-structure-in-java-and-python-84367851d7e9
Comparing Data Structure in Java and Python | by Pravinkumar Singh | Medium
June 20, 2023 - int[] numbers = new int[3]; numbers[0] = 1; numbers[1] = 2; numbers[2] = 3; System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 3] ... In Python, the list is a built-in data structure that is similar to Java's array.
Discussions

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 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
Are Python arrays the same as in Java? - Stack Overflow
I have an assignment in my class to implement something in Java and Python. I need to implement an IntegerStack with both languages. All the values are supposed to be held in an array and there are... More on stackoverflow.com
🌐 stackoverflow.com
Java equivalent of a Python functionality -> set(string) - Stack Overflow
I want to mimic a Python functionality in Java. In Python if I want unique characters in a string I can do just, text = "i am a string" print set(text) # o/p is set(['a', ' ', 'g', 'i', 'm', 'n',... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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...
🌐
DEV Community
dev.to › tlylt › python-array-vs-java-array-31ee
Python Array VS Java Array - DEV Community
October 24, 2020 - So, Python arrays are not equal to Java static arrays, but it is quite close to Java ArrayList/LinkedList.
🌐
Medium
medium.com › swlh › a-python-style-set-in-simple-java-698977f1b5d0
A Python-style Set in simple Java | by Dan Hales | The Startup | Medium
June 9, 2020 - We could have accomplished this with generics or polymorphism, but that would have distracted from the important behaviors that distinguish the Set class from ArrayList. This version of a Set is based on the set found in Python 2, which has since been deprecated. I opted for this older version because the latest set retains the fundamental behavior, but adds in a few additional features that could have made this post unruly. For APCSA teachers and students, or Java programmers looking to get their toes wet with Python, I hope this post helped to shed some light on Python set functionality.
🌐
W3Schools
w3schools.com › python › python_sets.asp
Python Sets
Sets are used to store multiple items in a single variable.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › swift › swift-difference-between-sets-and-arrays
Swift - Difference Between Sets and Arrays - GeeksforGeeks
November 4, 2022 - An array is a linear data structure which has contiguous memory in a single variable that can store N number of elements.
🌐
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 - A list is like a shopping list, you can add the item you want at the end of the list, remove an item, or fetch an item from the list by getting its index. Unlike arrays, lists are empty when instantiated, you need to add the values in the list; otherwise, the list itself is empty upon creation. ... Sets have a slightly different purpose than lists.
Top answer
1 of 5
3

Fist of all, you need to distinguish between arrays and lists in Python. What you are talking about here is list class, but there are actual arrays and they are more or less the same as arrays in Java.

Python's list is similar to Java's ArrayList and to C++'s std::vector.

In other words, you have three possible solutions here:

  1. Use simple list and just append elements to it.
  2. Use python's arrays that are the closest thing to Java's arrays.
  3. Use python's deque.

Regarding the use of list, if your goal is to initialize it with N empty elements, what you can do is:

N = 10     # or any other number
my_list = [0] * N # 0 is the element here for the list to be filled with

or little more fancy approach

from itertools import repeat
my_list = list(repeat(0, N))
2 of 5
1

If you want to implement something close to your java version, you could use a numpy array by importing numpy. Numpy arrays are similar because they are immutable objects like in java. Then you could write in your constructor:

    _stack = np.zeros(MAX_NUMBER)

Otherwise you could use the mutable list object from python itself, in this case the list is already a stack essentially as you can see in the python docs for data structures:

The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:

    >>> stack = [3, 4, 5]
    >>> stack.append(6)
    >>> stack.append(7)
    >>> stack
    [3, 4, 5, 6, 7]
    >>> stack.pop()
    7
    >>> stack
    [3, 4, 5, 6]
    >>> stack.pop()
    6
    >>> stack.pop()
    5
    >>> stack
    [3, 4]

The first version is more performant however, because mutable python objects have to be copied everytime they are changed whereas immutable objects are just created and old objects with no names referring to it are garbage collected.

🌐
Quora
quora.com › What-is-the-difference-between-an-array-in-Java-versus-Python
What is the difference between an array in Java versus Python? - Quora
Answer: In Java, we have to specify the size for the `Array` before using it and we can’t insert the elements inside the array beyond the specified size. Array can’t grow dynamically in java, we have to think of size while creating an array. In Python, we don’t have to specify the size ...
🌐
Programiz
programiz.com › java-programming › library › arraylist › set
Java ArrayList set()
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... Here, arraylist is an object of the ArrayList class. The set() method takes two parameters.
🌐
Board Infinity
boardinfinity.com › blog › list-and-set
List vs Set: Key Differences | Board Infinity
August 30, 2025 - It includes a sublist() method that returns a list of sublists within a given range from the original list. The modifications you make to the sublist are reflected in the original list as well. ArrayList, LinkedList, CopyOnWriteArrayList, Vector, and Stack are several implementations of the List interface. The Set interface enlarges the Collection interface.
🌐
Stack Overflow
stackoverflow.com › questions › 70029636 › difference-between-array-and-sets
Difference between Array and Sets - Stack Overflow
I don't understand the difference between Arrays and Sets. What is the difference between sets and arrays in python and where does numpy appear in arrays?
🌐
Quora
quora.com › What-is-the-difference-between-lists-arrays-and-sets-in-Python
What is the difference between lists, arrays, and sets in Python? - Quora
Answer: Lists: A built in type, with the following characteristics. * Variable length with a maximum of [code ]sys.maxsize[/code] elements * Elements can be anything - built in types, custom types, etc; * There is no requirement for elements to be hashable. * Elements can be repeated within...
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.