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.



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



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





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



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



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













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)





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





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









JSetAddAll[] adds all elements to the list :





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 :





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








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













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.







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.


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








Compare against storing three million integers in the Mathematica kernel:





Spikey Created with Wolfram Mathematica 7.0