<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
<channel>
<title>hashtable</title>
<link>http://www.computersight.com/tags/hashtable</link>
<description>New posts about hashtable</description>
<item>
<title>Collection Framework: List Interface</title>
<link>http://www.computersight.com/Programming/Java/Collection-Framework-List-Interface.237527</link>
<description>
<![CDATA[<h3><strong>What is Collection Framework?</strong></h3>
<p>A Collection framework is a unified architecture for representing, modifying and deleting group of elements.&amp;nbsp;</p>
<p>A Java Collection framework contains the group of Interfaces and Classes to manage the group of Objects knows as elements. Java Collection framework mainly contains the following Interfaces and Classes mentioned in the below diagrams.</p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/collection1_2.jpg" alt="" /></p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/collection2_1.jpg" alt="" /></p>
<h3>The main interfaces in the Collection are:</h3>
<p>1. Collection</p>
<p>2. List</p>
<p>3. Set</p>
<p>4. SortedSet</p>
<p>5. Map</p>
<p>6. SortedMap</p>
<ol>
<li><strong>Collection:</strong>&amp;nbsp; Collection is the super interface of the Collection framework. A collection represents a group of objects known as elements. The java platform does not provide any direct implementation of this interface. But it provides some specific interfaces extend Collection interface.</li>
<li><strong>List:</strong>&amp;nbsp; The List extends the Collection. The List maintains the order of elements but allows the duplicate elements.</li>
<li><strong>Set:</strong>&amp;nbsp; The Set extends the Collection. The Set maintains the unique elements but does not give the guarantee of order.</li>
<li><strong>SortedSet:</strong>&amp;nbsp; The SortedSet extends the Set. The SortedSet maintains the unique elements as well as sort of elements.</li>
<li><strong>Map:</strong>&amp;nbsp; The Map is used to map the Keys to Values. The map does not accept duplicate keys.</li>
<li><strong>SortedMap:</strong> The SortedMap extends Map. The SortedMap gives the guarantee of sorted keys.</li>
</ol>
<p>There are few concrete classes, which are extended the above interfaces.&amp;nbsp; We concentrate more List implementation classes below here.</p>
<p><strong>List Implementation classes:</strong></p>
<ol>
<li>ArrayList</li>
<li>Vector</li>
<li>LinkedList</li>
</ol>
<p><strong>Set Implementation classes:</strong></p>
<ol>
<li>HashSet</li>
<li>LinkedHashSet</li>
<li>TreeSet</li>
</ol>
<p><strong>Map Implementation classes:</strong></p>
<ol>
<li>HashMap</li>
<li>LinkedHashMap</li>
<li>Hashtable</li>
<li>TreeMap</li>
</ol>
<h3><strong>What are the legacy classes?</strong></h3>
<p>Before introducing Collection frame work in Java, there were some classes used to maintain the group of objects. These classes are called legacy classes.&amp;nbsp; The main legacy classes are</p>
<ol>
<li>Vector</li>
<li>Hashtable</li>
</ol>
<p>The Vector is similar to ArrayList and Hashtable is similar to HashMap. The Vector and Hashtable are synchronized means they are thread safe.</p>
<p>There are different flavours of classes in the collection framework. The flavours are</p>
<ol>
<li>Ordered</li>
<li>Un-ordered</li>
<li>Sorted</li>
<li>Un-sorted</li>
</ol>
<p>We will discuss about corcete classes which are implement the List interface below here.</p>
<p><strong>ArrayList: </strong>It is dynamic growable array. . All of you know that array is fixed size. If you declare the array size once, you can not change it. But the ArrayList size automatically grows, when you add the elements. ArrayList stores the elements by index.</p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/collection3_1.jpg" alt="" /></p>
<p>If you see the above program, we are stored eight objects in the ArrayList and removed one of them. We discuss about them below here.</p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/program_2.jpg" alt="" /></p>
<p>The above elements stores like below mentioned diagram. The John object stores in index 0 and peter is in 1 and etc. So every element has index value. We can access the elements using index values. The elements retrieval is very faster compare to remaining list implementation.</p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/program2_1.jpg" alt="" /></p>
<p>We can add the elements in the ArrayList at particular position or end of the list. The add(int, Object) method will be used to store the elements at particular position and add(Object) will be used to store the elements at end of the list.</p>
<p>We added the element at index 2 using following method.</p>
<p>empList.add(2, "Susan"); So the remaining elements from the index 2 to copied to next index. See the below diagram for full details.</p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/collection41_1.jpg" alt="" /></p>
<p>The Susan Object stored at position 2 (index). You can see it in above diagram. The remaining elements from the index 2 to copied to next position. Initially the Karl object was in&amp;nbsp; the index 2, now this object in&amp;nbsp; the index 3.</p>
<p>The remove method will used to remove the elements from the list. Please see the below example.</p>
<h3>empList.remove(3);</h3>
<p>The element stored at index 3 will be removed. So the Karl will be no longer available after the above line.</p>
<p><img src="http://images.stanzapub.com/readers/2008/09/01/collection51_1.jpg" alt="" /></p>
<p>If you see the above diagram, the Karl object removed from the index 3. The remaining objects from the index 4 to copied to below index i.e. from index 3.</p>
<p><strong>Vector : </strong>Vector is similar to ArrayList except thread safe. It was introduced in previous version of java2. The ArrayList was introduced in Java2 version. All the methods in Vector are synchronized. So we can go for ArrayList instead of Vector. The synchronized methods are performance hit, you might not need them.</p>
<p><strong>LinkedList:</strong>A LinkedList List is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another. So we can add and remove the elements from the beginning or end. The LinkedList may iterate slowly than an ArrayList, but it&amp;rsquo;s a good choice when you need fast insertion and deletion.</p><a href="http://www.pheedo.com/click.phdo?x=&u=http%3A%2F%2Fwww.computersight.com%2FProgramming%2FJava%2FCollection-Framework-List-Interface.237527"><img src="http://www.pheedo.com/img.phdo?x=&u=http%3A%2F%2Fwww.computersight.com%2FProgramming%2FJava%2FCollection-Framework-List-Interface.237527" border="0"/></a>]]></description>
<pubDate>Mon, 01 Sep 2008 09:31:41 PST</pubDate></item>
</channel>
</rss>
