<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Using Java Collections in Matlab	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/using-java-collections-in-matlab/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-java-collections-in-matlab</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Tue, 19 Feb 2019 08:16:01 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-463024</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 19 Feb 2019 08:16:01 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-463024</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-462906&quot;&gt;Phil&lt;/a&gt;.

@Phil - as you discovered, this conversion is automatically done by Matlab. I am not familiar with a way to override it. The best choice is to store the scalar along with some dummy value, and only use the first item in the array when you fetch it back from the hashmap.
&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; map.put(&#039;key2&#039;, int32([5,0]));
&gt;&gt; data = map.get(&#039;key2&#039;)
data =
  2×1 int32 column vector
   5
   0
&gt;&gt; data(1)
ans =
  int32
   5
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-462906">Phil</a>.</p>
<p>@Phil &#8211; as you discovered, this conversion is automatically done by Matlab. I am not familiar with a way to override it. The best choice is to store the scalar along with some dummy value, and only use the first item in the array when you fetch it back from the hashmap.</p>
<pre lang="matlab">
>> map.put('key2', int32([5,0]));
>> data = map.get('key2')
data =
  2×1 int32 column vector
   5
   0
>> data(1)
ans =
  int32
   5
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Phil		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-462906</link>

		<dc:creator><![CDATA[Phil]]></dc:creator>
		<pubDate>Mon, 18 Feb 2019 22:39:51 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-462906</guid>

					<description><![CDATA[Is there any workaround to prevent Matlab from automatic converting scalar, numeric Java types into doubles? (described here https://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-java-methods.html#bvizhgf)

See the following simple example:
&lt;pre lang=&quot;matlab&quot;&gt;
% initialize hashmap
map = java.util.LinkedHashMap();

% add a k/v pair with integer vector value
map.put(&#039;key1&#039;, int32([5 6 7]));

% add a k/v pair with integer scalar value
map.put(&#039;key2&#039;, int32(5));

% get the value for key1 - it&#039;s an int32, as one would expect
class(map.get(&#039;key1&#039;))

% get the value for key2 - it&#039;s a double - Matlab automatically converted this
% is there any way to get this value&#039;s type before it is converted?
% conversion, so that I cam 
class(map.get(&#039;key2&#039;))
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Is there any workaround to prevent Matlab from automatic converting scalar, numeric Java types into doubles? (described here <a href="https://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-java-methods.html#bvizhgf" rel="nofollow ugc">https://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-java-methods.html#bvizhgf</a>)</p>
<p>See the following simple example:</p>
<pre lang="matlab">
% initialize hashmap
map = java.util.LinkedHashMap();

% add a k/v pair with integer vector value
map.put('key1', int32([5 6 7]));

% add a k/v pair with integer scalar value
map.put('key2', int32(5));

% get the value for key1 - it's an int32, as one would expect
class(map.get('key1'))

% get the value for key2 - it's a double - Matlab automatically converted this
% is there any way to get this value's type before it is converted?
% conversion, so that I cam 
class(map.get('key2'))
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Mathews		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-408260</link>

		<dc:creator><![CDATA[Jonathan Mathews]]></dc:creator>
		<pubDate>Fri, 09 Jun 2017 06:34:56 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-408260</guid>

					<description><![CDATA[Thank you for the tutorial. I found this resource for a &lt;a href=&quot;http://eeenthusiast.com/java-queue-stack-implementation-tutorial-linked-list-programming-explained-from-scratch/&quot; rel=&quot;nofollow&quot;&gt;java stacks queues tutorial&lt;/a&gt;. They cover an implementation through a linked list. Does it matter if you build it from scratch or through a linked list?

Cheers,
John]]></description>
			<content:encoded><![CDATA[<p>Thank you for the tutorial. I found this resource for a <a href="http://eeenthusiast.com/java-queue-stack-implementation-tutorial-linked-list-programming-explained-from-scratch/" rel="nofollow">java stacks queues tutorial</a>. They cover an implementation through a linked list. Does it matter if you build it from scratch or through a linked list?</p>
<p>Cheers,<br />
John</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-385828</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 16 Aug 2016 20:00:43 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-385828</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-385825&quot;&gt;Christopher Volpe&lt;/a&gt;.

@Christopher - primitive Java arrays (e.g. &lt;code&gt;double[]&lt;/code&gt;) are automatically converted into a column vector in Matlab. 

When you store your data in a Java object using automated Matlab-Java type-conversions, Java strips away any such meta-info. Matlab has no way of knowing whether the array should be a row or column array, because this meta-info is not stored by Java, so the decision to create a column Matlab array is just as valid as a decision to create a row array. 

If you wish to store the Matlab data including the meta-info, then you need to serialize your Matlab data before storing it, and deserialize it back when you read it: &lt;a href=&quot;http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data&lt;/a&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-385825">Christopher Volpe</a>.</p>
<p>@Christopher &#8211; primitive Java arrays (e.g. <code>double[]</code>) are automatically converted into a column vector in Matlab. </p>
<p>When you store your data in a Java object using automated Matlab-Java type-conversions, Java strips away any such meta-info. Matlab has no way of knowing whether the array should be a row or column array, because this meta-info is not stored by Java, so the decision to create a column Matlab array is just as valid as a decision to create a row array. </p>
<p>If you wish to store the Matlab data including the meta-info, then you need to serialize your Matlab data before storing it, and deserialize it back when you read it: <a href="http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data" target="_blank" rel="nofollow">http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Christopher Volpe		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-385825</link>

		<dc:creator><![CDATA[Christopher Volpe]]></dc:creator>
		<pubDate>Tue, 16 Aug 2016 19:13:00 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-385825</guid>

					<description><![CDATA[Nice overview. Is there a reason why (Matlab&#039;s interface to) java.util.LinkedList turns row vectors into column vectors? E.g.:
&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; dummyQueue = java.util.LinkedList
dummyQueue =
[]

&gt;&gt; vecIn = [1,2,3]
vecIn =
     1     2     3

&gt;&gt; dummyQueue.add(vecIn)
ans =
     1

&gt;&gt; vecOut = dummyQueue.remove()
vecOut =
     1
     2
     3
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Nice overview. Is there a reason why (Matlab&#8217;s interface to) java.util.LinkedList turns row vectors into column vectors? E.g.:</p>
<pre lang="matlab">
>> dummyQueue = java.util.LinkedList
dummyQueue =
[]

>> vecIn = [1,2,3]
vecIn =
     1     2     3

>> dummyQueue.add(vecIn)
ans =
     1

>> vecOut = dummyQueue.remove()
vecOut =
     1
     2
     3
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-383598</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 20 Jul 2016 07:39:04 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-383598</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-383574&quot;&gt;Sepehr&lt;/a&gt;.

@Sepehr - while &lt;code&gt;Hashtable&lt;/code&gt; does indeed accept arrays as keys, the automatic conversion between Java and Matlab data types causes &lt;i&gt;hash.get&lt;/i&gt; not to find the key. Instead, use a simpler data type such as a number or string. 

You also need to remember that all numeric data in Java is signed, unlike Matlab which uses &lt;code&gt;uint8&lt;/code&gt; for its serialization. Therefore, you need to first convert into a signed &lt;code&gt;int16&lt;/code&gt; before storing in the Java array, and then to convert back to &lt;code&gt;uint8&lt;/code&gt; when loading back into Matlab from the hash:
&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; data = int16(getByteStreamFromArray([2,3]));
&gt;&gt; hash.put(&#039;mykey&#039;, data);
...
&gt;&gt; data2 = getArrayFromByteStream(uint8(hash.get(&#039;mykey&#039;)))
data2 =
     2     3
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-383574">Sepehr</a>.</p>
<p>@Sepehr &#8211; while <code>Hashtable</code> does indeed accept arrays as keys, the automatic conversion between Java and Matlab data types causes <i>hash.get</i> not to find the key. Instead, use a simpler data type such as a number or string. </p>
<p>You also need to remember that all numeric data in Java is signed, unlike Matlab which uses <code>uint8</code> for its serialization. Therefore, you need to first convert into a signed <code>int16</code> before storing in the Java array, and then to convert back to <code>uint8</code> when loading back into Matlab from the hash:</p>
<pre lang="matlab">
>> data = int16(getByteStreamFromArray([2,3]));
>> hash.put('mykey', data);
...
>> data2 = getArrayFromByteStream(uint8(hash.get('mykey')))
data2 =
     2     3
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sepehr		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-383574</link>

		<dc:creator><![CDATA[Sepehr]]></dc:creator>
		<pubDate>Wed, 20 Jul 2016 00:32:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-383574</guid>

					<description><![CDATA[Hi Yair,
Great article. I have tried your suggestion on serializing objects so they can be used as both keys and values in a hashtable. It does not work. 
Here a piece of code that shows hashtable fails at finding a byte array!
&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; hash = java.util.Hashtable;
&gt;&gt; key = getByteStreamFromArray([1,2]);
&gt;&gt; value = getByteStreamFromArray([2,3]);
&gt;&gt; hash.put(key,value)
ans =
     []

&gt;&gt; hash.containsKey(key)
ans =
     0
&lt;/pre&gt;

Also, when I use strings as keys and byte arrays as values, the retrieved values are scrambled! e.g.

&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; hash = java.util.Hashtable;
&gt;&gt; hash.put(&#039;mykey&#039;,getByteStreamFromArray([2,3]));
&gt;&gt; getArrayFromByteStream(hash.get(&#039;mykey&#039;))
Error using getArrayFromByteStream
The input must be a uint8 byte stream.
&lt;/pre&gt;

Any suggestions?]]></description>
			<content:encoded><![CDATA[<p>Hi Yair,<br />
Great article. I have tried your suggestion on serializing objects so they can be used as both keys and values in a hashtable. It does not work.<br />
Here a piece of code that shows hashtable fails at finding a byte array!</p>
<pre lang="matlab">
>> hash = java.util.Hashtable;
>> key = getByteStreamFromArray([1,2]);
>> value = getByteStreamFromArray([2,3]);
>> hash.put(key,value)
ans =
     []

>> hash.containsKey(key)
ans =
     0
</pre>
<p>Also, when I use strings as keys and byte arrays as values, the retrieved values are scrambled! e.g.</p>
<pre lang="matlab">
>> hash = java.util.Hashtable;
>> hash.put('mykey',getByteStreamFromArray([2,3]));
>> getArrayFromByteStream(hash.get('mykey'))
Error using getArrayFromByteStream
The input must be a uint8 byte stream.
</pre>
<p>Any suggestions?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-353641</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 24 Jul 2015 07:51:30 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-353641</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-353640&quot;&gt;Yola&lt;/a&gt;.

To store a Matlab class object in a Java collection, you would need to first &lt;a href=&quot;http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data&quot; rel=&quot;nofollow&quot;&gt;serialize the object&lt;/a&gt; into a byte array that can then be stored in the Java collection object (and deserialize it when it is extracted).

Comparators, AFAIK, can only be specified in Java code. I don&#039;t think you can specify a Matlab function to act as a comparator function that Java understands.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-353640">Yola</a>.</p>
<p>To store a Matlab class object in a Java collection, you would need to first <a href="http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data" rel="nofollow">serialize the object</a> into a byte array that can then be stored in the Java collection object (and deserialize it when it is extracted).</p>
<p>Comparators, AFAIK, can only be specified in Java code. I don&#8217;t think you can specify a Matlab function to act as a comparator function that Java understands.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yola		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-353640</link>

		<dc:creator><![CDATA[Yola]]></dc:creator>
		<pubDate>Fri, 24 Jul 2015 07:40:46 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-353640</guid>

					<description><![CDATA[Can you please write something on how i can use java collections with user-defined types. How i can provide comparator for my type. Thanks!]]></description>
			<content:encoded><![CDATA[<p>Can you please write something on how i can use java collections with user-defined types. How i can provide comparator for my type. Thanks!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-193106</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 23 Apr 2013 07:09:57 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2725#comment-193106</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-193104&quot;&gt;Rajpal&lt;/a&gt;.

@Rajpal - either use the &lt;a href=&quot;http://www.mathworks.com/products/javabuilder/&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;Matlab Builder for Java&lt;/a&gt; toolbox ($$$) or use one of the free alternatives such as &lt;a href=&quot;http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/&quot; rel=&quot;nofollow&quot;&gt;JMI&lt;/a&gt;, JNI, JNA, COM etc. The toolbox costs money but saves you a lot of programming and QA time, so it could well be worth the investment.

You can find more details in my &lt;a href=&quot;http://undocumentedmatlab.com-java-book/&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;Matlab-Java programming book&lt;/a&gt; (Chapter 9).]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/using-java-collections-in-matlab#comment-193104">Rajpal</a>.</p>
<p>@Rajpal &#8211; either use the <a href="http://www.mathworks.com/products/javabuilder/" target="_blank" rel="nofollow">Matlab Builder for Java</a> toolbox ($$$) or use one of the free alternatives such as <a href="http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/" rel="nofollow">JMI</a>, JNI, JNA, COM etc. The toolbox costs money but saves you a lot of programming and QA time, so it could well be worth the investment.</p>
<p>You can find more details in my <a href="http://undocumentedmatlab.com-java-book/" target="_blank" rel="nofollow">Matlab-Java programming book</a> (Chapter 9).</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
