<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Define HashSet</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-util-questions/define-hashset/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Define HashSet Class</title>
		<link>http://www.bestjavainterviewquestions.com/define-hashset-class/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-hashset-class/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 17:34:58 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Define HashSet]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[core java]]></category>
		<category><![CDATA[HashSet]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[set]]></category>
		<category><![CDATA[utill package]]></category>
		<category><![CDATA[what is HashSet class]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/define-hashset-class/</guid>
		<description><![CDATA[HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. As most readers likely know, a hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code. The [...]]]></description>
			<content:encoded><![CDATA[<p><strong>HashSet </strong>extends <strong>AbstractSet </strong>and implements the <strong>Set </strong>interface. It creates a collection that uses a hash table for storage. As most readers likely know, a hash table stores information by using a mechanism called <em>hashing</em>. In <em>hashing</em>, the informational content of a key is used to determine a unique value, called its <em>hash code</em>. The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically, you never see the hash code itself. Also, your code can&#8217;t directly index the hash table. The advantage of hashing is that it allows the execution time of basic operations, such as<strong> add(), contains(), remove()</strong>, and size, to remain constant even for large sets,<br />
The following constructors are defined:</p>
<p><strong><em>HashSet()<br />
HashSet(Collection c)<br />
HashSet(int capacity)<br />
HashSet(int capacity, float fillRatio)</em></strong></p>
<p>The first form constructs a default has set. The second form initializes the hash set by using the elements of c. The third form initializes the capacity of the hash set to capacity. The fourth form initializes both the capacity and the fill ration (also called<em> load capacity</em>) of the hash set from its arguments. The fill ratio must be between 0.0 and 1.0, and it determines how full the hash set can be before it is re sized upward. Specifically when the number of elements is greater than the capacity of the hash set multiplied by its fill ratio, the hash set is expanded. For constructors that do not take a fill ratio, 0.75 is used.</p>
<p><strong>HashSet </strong>does not define any additional methods beyond those provided by its <em>superclasses </em>and interfaces.<br />
Importantly, note that a hash set does not guarantee the order of its elements, because the process of hashing doesn&#8217;t usually lend itself to the creation of sorted sets. If you need sorted storage, then another collection, such as <strong>TreeSet</strong>, is a better choice.</p>
<p><em>Here is an example that demonstrates <strong>HashSet</strong>:</em></p>
<p><em>//Demonstrate HashSet.</em></p>
<p><em>import java.util.*;<br />
class HashSet Demo {<br />
public static void main(String args[])<br />
{<br />
//create a hash set</em></p>
<p><em>HashSet hash=new HashSet();</em></p>
<p><em>//add elements to the hash set</em></p>
<p><em>hash.add(&#8220;B&#8221;);<br />
hash.add(&#8220;A&#8221;);<br />
hash.add(&#8220;D&#8221;);<br />
hash.add(&#8220;E&#8221;);<br />
hash.add(&#8220;C&#8221;);<br />
hash.add(&#8220;F&#8221;);<br />
System.out.println(hash);<br />
}<br />
}</em></p>
<p><em>The following is the output from this program:</em></p>
<p><em>[ A,F, E, D, C, B]</em><br />
As explained , the elements are not stored in sorted order, and the precise output may vary.</p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/19/" title="Java Basic Questions">Java Basic Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/basic-java-interview-question/" title="Basic Java Interview Question">Basic Java Interview Question</a> (1)</li><li>August 17, 2010 -- <a href="http://www.bestjavainterviewquestions.com/define-hashtablehashmap-and-hashset/" title="Define Hashtable,HashMap and HashSet">Define Hashtable,HashMap and HashSet</a> (1)</li><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/when-the-port-number-is-changed/" title="When the port number is changed">When the port number is changed</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-hashset-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

