<?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%3AGeneric_Pair</id>
	<title>SE251Ex:Generic Pair - 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%3AGeneric_Pair"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE251Ex:Generic_Pair&amp;action=history"/>
	<updated>2026-05-20T13:24:22Z</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:Generic_Pair&amp;diff=9806&amp;oldid=prev</id>
		<title>Mark: 8 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE251Ex:Generic_Pair&amp;diff=9806&amp;oldid=prev"/>
		<updated>2008-11-03T05:21:04Z</updated>

		<summary type="html">&lt;p&gt;8 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Writing a generic Pair ==&lt;br /&gt;
&lt;br /&gt;
=== Description ===&lt;br /&gt;
One of the drawbacks of Java is there isn&amp;#039;t an easy way to return multiple values from a method. It would be nice to return say a pair of values, each with a specifiable type. So, for this exercise, you are to write a generic &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039; class to hold two objects of parameterisable types. For example, a variable of type &amp;lt;code&amp;gt;Pair&amp;lt;String,Date&amp;gt;&amp;lt;/code&amp;gt; will hold a String object and a Date object. It should define/override the following public methods:&lt;br /&gt;
* T1 getFirst() - access the first element of the pair&lt;br /&gt;
* void setFirst(T1) - update the first element of the pair, where T1 is the type variable for the first element&lt;br /&gt;
* T2 getSecond() - access the second element of the pair&lt;br /&gt;
* void setFirst(T2) - update the second element of the pair, where T2 is the type variable for the second element&lt;br /&gt;
* int hashCode() - returns the sum of the hashCodes of the elements. I.e. returns first.hashCode() + second.hashCode(). The reason for overriding this is a little tricky. It&amp;#039;s mainly for our &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039; to work nicely with HashSets and HashMaps.&lt;br /&gt;
* boolean equals(Object) - returns true if and only if the given Object is instace of &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039; and the individual elements of this &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039; are equal to the respective elements of the given &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039;&lt;br /&gt;
* String toString() - returns the String representation of the pair in the form of &amp;quot;&amp;lt;first, second&amp;gt;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Below is an example of how we can benefit from your &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039; class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public static void main(String[] args) {&lt;br /&gt;
   int[] nums = {1,4,2,4,3,4,9,8,7,6,6,2};&lt;br /&gt;
   Pair&amp;lt;Integer,Double&amp;gt; result = medianAndMean(nums);&lt;br /&gt;
   System.out.println(&amp;quot;Median &amp;amp; Mean: &amp;quot;+result);&lt;br /&gt;
   &lt;br /&gt;
   int[] nums2 = {6,4,2,3,5,9,2,8,3};&lt;br /&gt;
   Pair&amp;lt;Integer,Double&amp;gt; result2 = medianAndMean(nums);&lt;br /&gt;
   System.out.println(&amp;quot;Same? &amp;quot; + result.equals(result2));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static Pair&amp;lt;Integer,Double&amp;gt; medianAndMean(int[] numbers) {&lt;br /&gt;
   Integer median = calculateMedian(numbers); //assume this method exists&lt;br /&gt;
   Double mean = calculateMean(numbers); //assume this method exists&lt;br /&gt;
   return new Pair&amp;lt;Integer,Double&amp;gt;(median,mean);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the corresponding output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Median &amp;amp; Mean: &amp;lt;4, 4.6667&amp;gt;&lt;br /&gt;
Same? true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Cases ===&lt;br /&gt;
* Use [[SE251Ex:Generic_Pair_TestPair|TestPair]] to test the correctness of your Pair&lt;br /&gt;
&lt;br /&gt;
=== Tips ===&lt;br /&gt;
If you&amp;#039;re still not friendly with generics, first write a non-generic &amp;#039;&amp;#039;Pair&amp;#039;&amp;#039; that stores both its items as Objects.&lt;br /&gt;
Then pretty much replace every occurrence of &amp;#039;&amp;#039;Object&amp;#039;&amp;#039; with the appropriate type variable.&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>