<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE251Ex%3ASet_Operations</id>
	<title>SE251Ex:Set Operations - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE251Ex%3ASet_Operations"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE251Ex:Set_Operations&amp;action=history"/>
	<updated>2026-05-20T11:14:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE251Ex:Set_Operations&amp;diff=9823&amp;oldid=prev</id>
		<title>Mark: 4 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE251Ex:Set_Operations&amp;diff=9823&amp;oldid=prev"/>
		<updated>2008-11-03T05:21:05Z</updated>

		<summary type="html">&lt;p&gt;4 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Writing set operations in Java ==&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
This exercise requires you to have completed the exercise [[SE251Ex:Generic_Pair]], as it makes use of the &amp;#039;&amp;#039;Pair&amp;lt;E1,E2&amp;gt;&amp;#039;&amp;#039; class from the exercise.&lt;br /&gt;
&lt;br /&gt;
=== Description ===&lt;br /&gt;
You learnt about the &amp;#039;&amp;#039;Set&amp;lt;E&amp;gt;&amp;#039;&amp;#039; interface from java.util, and the operations supported by it. What the Java API doesn&amp;#039;t provide however are the mathematical set operations including union, intersection, difference and cartesian product &amp;#039;&amp;#039;(edit: actually it kind of does with the methods addAll(), removeAll() and retainAll(), but still performs the operations destructively, that is, by modifying the original set)&amp;#039;&amp;#039;. These operations are quite straightforward, but you may want to brush up on them with the help of [http://en.wikipedia.org/wiki/Set#Basic_operations Wikipedia]. Below is a skeleton for the class &amp;#039;&amp;#039;SetOperations&amp;#039;&amp;#039; providing four static generic methods, each for one of the operations.  &lt;br /&gt;
The Javadoc comments for each method should provide enough information about what exactly it should do. Note: each method is declared to return a Set&amp;lt;E&amp;gt;, which means it is up to you to chose which implementation of Set (i.e. HashSet or a TreeSet) to return.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class SetOperations {&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the union of two given sets. E.g. given sets {a,c,d,g}&lt;br /&gt;
	 * and {b,d,e,f} the method should return {a,b,c,d,e,f,g}&lt;br /&gt;
	 * @param &amp;lt;E&amp;gt; the type of elements in each of the sets&lt;br /&gt;
	 * @param sets the sets to perform union on&lt;br /&gt;
	 * @return the union of all given sets&lt;br /&gt;
	 */&lt;br /&gt;
	public static&amp;lt;E&amp;gt; Set&amp;lt;E&amp;gt; union(Set&amp;lt;E&amp;gt; set1, Set&amp;lt;E&amp;gt; set2) {&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	/**&lt;br /&gt;
	 * Return the intersection between the two given sets. E.g. given&lt;br /&gt;
	 * set1 = {a,b,c,d} and set2 = {a,d,e,f,g}, the method should&lt;br /&gt;
	 * return {a,d} &lt;br /&gt;
	 * @param &amp;lt;E&amp;gt; the type of elements in each of the sets&lt;br /&gt;
	 * @param set1 the first set&lt;br /&gt;
	 * @param set2 the second set&lt;br /&gt;
	 * @return the intersection between set1 and set2&lt;br /&gt;
	 */&lt;br /&gt;
	public static&amp;lt;E&amp;gt; Set&amp;lt;E&amp;gt; intersection(Set&amp;lt;E&amp;gt; set1, Set&amp;lt;E&amp;gt; set2) {&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the set difference between set1 and set2. Specifically, returns&lt;br /&gt;
	 * a set of all items in set1 that set2 doesn&amp;#039;t contain. E.g. given&lt;br /&gt;
	 * set1 = {a,b,c,d} and set2 = {a,d,e,f,g}, the method should return&lt;br /&gt;
	 * {b,c}&lt;br /&gt;
	 * @param &amp;lt;E&amp;gt; the type of elements in each of the sets&lt;br /&gt;
	 * @param set1 the first set&lt;br /&gt;
	 * @param set2 the second set&lt;br /&gt;
	 * @return The mathematical function &amp;lt;code&amp;gt;set1 \ set2&amp;lt;/code&amp;gt;&lt;br /&gt;
	 */&lt;br /&gt;
	public static&amp;lt;E&amp;gt; Set&amp;lt;E&amp;gt; difference(Set&amp;lt;E&amp;gt; set1, Set&amp;lt;E&amp;gt; set2) {&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	/**&lt;br /&gt;
	 * Return the cartesian product between the two given sets, i.e. set1 x set2.&lt;br /&gt;
	 * The result is effectively a set of all pairs &amp;lt;x,y&amp;gt; such that x is a member of&lt;br /&gt;
	 * set1 and y is a member of y. E.g. given set1 = {a,b,c} and set2 = {1,2,3}&lt;br /&gt;
	 * the result would be {(a,1),(a,2),(a,3),(b,1),(b,2),(b,3),(c,1),(c,2),(c,3)}&lt;br /&gt;
	 * @param &amp;lt;E&amp;gt; the type of elemets in set1&lt;br /&gt;
	 * @param &amp;lt;F&amp;gt; the type of elemets in set2&lt;br /&gt;
	 * @param set1 the first set&lt;br /&gt;
	 * @param set2 the second set&lt;br /&gt;
	 * @return the cartesian product as a set of Pair objects&lt;br /&gt;
	 */&lt;br /&gt;
	public static&amp;lt;E,F&amp;gt; Set&amp;lt;Pair&amp;lt;E,F&amp;gt;&amp;gt; cartesianProduct(Set&amp;lt;E&amp;gt; set1, Set&amp;lt;F&amp;gt; set2) {&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Cases ===&lt;br /&gt;
* Use [[SE251Ex:Generic_Pair_TestSetOperations|TestSetOperations]] to test your implementation of the set operations&lt;br /&gt;
&lt;br /&gt;
=== Tips ===&lt;br /&gt;
All classes implementing &amp;#039;&amp;#039;Collection&amp;#039;&amp;#039; defines a method called &amp;#039;&amp;#039;addAll(Collection)&amp;#039;&amp;#039; method. You may find this useful. What this does is adds all elements from the given Collection to the current collection. For example we can do this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
List&amp;lt;String&amp;gt; original = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
original.add(&amp;quot;A&amp;quot;);&lt;br /&gt;
original.add(&amp;quot;B&amp;quot;);&lt;br /&gt;
original.add(&amp;quot;C&amp;quot;);&lt;br /&gt;
original.add(&amp;quot;B&amp;quot;);&lt;br /&gt;
Set&amp;lt;String&amp;gt; words = new HashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
words.addAll(original);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The contents of &amp;#039;&amp;#039;words&amp;#039;&amp;#039; will become {&amp;quot;A&amp;quot;, &amp;quot;B&amp;quot;, &amp;quot;C&amp;quot;} as a result.&lt;br /&gt;
&lt;br /&gt;
== Discussion ==&lt;br /&gt;
Here you can ask questions and discuss stuff&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>