# Representing sets in Mathematica [duplicate]

What is the right ways to define a mathematical set in Mathematica?

I wrote the line

A = {1, 2, 3}


and

A := {1, 2, 3}


Both cases work, but I don't know which if any is right.

• {} is a List and the difference in : you can find in difference between Set (or =) and SetDelayed (or :=)
– Kuba
Jul 25 '17 at 9:52
• There is no set data structure in Mathematica, only lists. There are functions such as Union which treat lists as if they were sets (they sort the lists and remove duplicate elements). You should check this page, choose a tutorial you find suitable for your level of knowledge, and work through at least the first few chapters. Jul 25 '17 at 10:07
• Jul 25 '17 at 10:10

a = {1, 2, 3} is the correct way.

• does not work for me, if I input a = {1,1} it contains duplicate elements.
– Kuba
Jul 25 '17 at 14:28

With Java 8 we got the cool new streams framework. In fact, one of the original designers of Java recommends to always use streams for all arrays / collections, unless you have specific reasons not to. Streams are the new default, especially together with collections.

Needs["JLink"]

ReinstallJava[]

t[s_]:=ArraysasList[MakeJavaObject/@s];

list={1,1,2,3,4,5};
set=Arraysstream[MakeJavaObject/@list]@collect[CollectorstoSet[]];
set@toArray[]

set@toArray[]


set//Methods