Set

Why have a set? A set is a collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, the set models the mathematical set abstraction. The set is also used internally in the implemention of the JavaTools function JDijkstra[]to find all shortest paths from the source node, as the algorithm requires maintaining a set of all nodes, a set of those nodes that have already been visited and labelled, and a set of those nodes that have not yet been visited. As a network cannot have duplicate nodes, the set is the ideal data structure to represent the node sets.

In JavaTools a set can simply be created with JSet[]. It then has to be filled with elements afterwards.

"set_1.gif"

"set_2.gif"

However, it's also possible to create a new set and supply initial values during creation:

"set_3.gif"

"set_4.gif"

JSetSize[] returns the number of elements in the set, and JSetIsEmpty[] shows if the set is empty:

"set_5.gif"

"set_6.gif"

"set_7.gif"

"set_8.gif"

A new element is added to the set with JSetAdd[]:

"set_9.gif"

"set_10.gif"

The whole list of elements in the multiset can be shown with JSetToArray[]:

"set_11.gif"

"set_12.gif"

The type of elements that can be put in a multiset can be anything: Integer, Real, String, or even List:

"set_13.gif"

"set_14.gif"

"set_15.gif"

"set_16.gif"

"set_17.gif"

"set_18.gif"

"set_19.gif"

"set_20.gif"

"set_21.gif"

"set_22.gif"

"set_23.gif"

"set_24.gif"

Note that it is NOT possible to add an element that is already in the set. That is the core essence of a set: There can be no duplicates in the set! (see JMultiSet[] if you want to allow duplicates)

"set_25.gif"

"set_26.gif"

"set_27.gif"

"set_28.gif"

With JSetRemove[ we can remove an element that is in the set:

"set_29.gif"

"set_30.gif"

"set_31.gif"

"set_32.gif"

With JSetContains[] we can see if the set contains a particular element:

"set_33.gif"

"set_34.gif"

"set_35.gif"

"set_36.gif"

"set_37.gif"

"set_38.gif"

"set_39.gif"

"set_40.gif"

JSetAddAll[] adds all elements to the list :

"set_41.gif"

"set_42.gif"

"set_43.gif"

"set_44.gif"

Note that JSetAddAll[] adds all elements in the list to the set individually, whereas JSetAdd[] adds all elements in the list to the set as one list element :

"set_45.gif"

"set_46.gif"

"set_47.gif"

"set_48.gif"

JSetClear[] completely clears the entire queue, returning an empty set and setting its size to 0.

"set_49.gif"

"set_50.gif"

"set_51.gif"

"set_52.gif"

"set_53.gif"

"set_54.gif"

"set_55.gif"

We can use any data type, not just Integer, Real, and String.

"set_56.gif"

"set_57.gif"

"set_58.gif"

"set_59.gif"

"set_60.gif"

"set_61.gif"

"set_62.gif"

"set_63.gif"

"set_64.gif"

"set_65.gif"

"set_66.gif"

"set_67.gif"

The set property is maintained, it's positively impossible to insert Plot3D[Sin[3 x] Cos[y], {x, 0, 2 Pi}, {y, -Pi, Pi}] again, it was already inserted in the group insertion above.

"set_68.gif"

"set_69.gif"

"set_70.gif"

"set_71.gif"

"set_72.gif"

"set_73.gif"

We can use any type Mathematica provids us: numeric types (Integer, Real, SparseArray), strings, formulas (e. g. SeriesData), graphic types (e. g. Graphics, Graphics3D, GraphicsComplex), database connections (SQLConnection[]), XML expressions (XMLElement[]), file formats, etc.

Performance

Note the memory-footprint the JSet[] has on the Mathematica system: Not a single byte of data is stored in the kernel:

"set_74.gif"

"set_75.gif"

"set_76.gif"

"set_77.gif"

"set_78.gif"

"set_79.gif"

"set_80.gif"

Compare against storing three million integers in the Mathematica kernel:

"set_81.gif"

"set_82.gif"

"set_83.gif"

"set_84.gif"

Spikey Created with Wolfram Mathematica 7.0