<?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 Blog &#187; Java Collection</title>
	<atom:link href="http://javapapers.com/category/core-java/java-collection/feed/" rel="self" type="application/rss+xml" />
	<link>http://javapapers.com</link>
	<description>Blog on core java, servlets, jsp and design patterns.</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:25:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Read Only Collections</title>
		<link>http://javapapers.com/core-java/java-collection/read-only-collections/</link>
		<comments>http://javapapers.com/core-java/java-collection/read-only-collections/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:59:51 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Java Collection]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=179</guid>
		<description><![CDATA[I do build crazy buildings using my collection of Lego blocks. My 11 months old kid Ben curiously stares at me build it. He always wishes to get hold of it. After I complete the building when I give that to his hand, you know what the first thing he does. Modify the building blocks. [...]]]></description>
			<content:encoded><![CDATA[<p>I do build crazy buildings using my collection of Lego blocks. My 11 months old kid Ben curiously stares at me build it. He always wishes to get hold of it. After I complete the building when I give that to his hand, you know what the first thing he does.</p>
<div class="wp-caption alignright"><img class="alignright size-full wp-image-180" title="Lego Blocks" src="http://javapapers.com/wp-content/uploads/2009/09/legoblocks.jpg" alt="Lego Blocks" width="350" height="234" /></div>
<p>Modify the building blocks. Though I wish them to be intact forever Lego buildings are built to be modified.</p>
<p>But this is not the case in programming. You create a java collection and store objects in it. Then there are scenarios where you want them not be modified. Obsessed with file system terminology Java guys have named it as read only collections.</p>
<p>By default some of the languages like dot net provide read only collections. But in Java there are no such things. This is not a special type of collection it is an additional facility provided to change the usual collections as read only.</p>
<h2>Methods by Collections class</h2>
<p>The Collections class provides six factory methods, one for each of Collection, List, Map, Set, SortedMap, and SortedSet.</p>
<ul>
<li>Collection unmodifiableCollection(Collection collection)</li>
<li>List unmodifiableList(List list)</li>
<li>Map unmodifiableMap(Map map)</li>
<li>Set unmodifiableSet(Set set)</li>
<li>SortedMap unmodifiableSortedMap(SortedMap map)</li>
<li>SortedSet unmodifiableSortedSet(SortedSet set)</li>
</ul>
<p>You should set the collection with required values then pass it as value to the Collections&#8217; respective method. Most important thing here is, just passing and setting it as unModifiableX is not enough. These methods will return you collection as read only. You need to overwrite your old collection with this new read only collection. If you don&#8217;t do that, using the reference of the old collection the values can be modified. Cool right!</p>
<p>The returned set will be serializable if the specified set is serializable. If you attempt to modify a read-only collection it will throw an UnsupportedOperationException.</p>
<h2>Example source code for java read only collections</h2>
<pre class="brush: java;">
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

// example program to demonstrate the read-only collection in java
public class ReadOnlyCollections {

public static void main(String args[]) {

// creating a list
List godList = Arrays
.asList(new String[] { &quot;Donald&quot;, &quot;Dennis&quot;, &quot;Ken&quot; });

// making a read-only list
List list = new ArrayList(godList);
list = Collections.unmodifiableList(list);

// checking the reference in a read-only set
Set set = new HashSet(godList);
Collections.unmodifiableSet(set);

// the following statement allows to modify the above set as the
// reference is pointing to the original collection therefore it is not
// read-only
set.add(&quot;Alan&quot;);

// making a read-only map and try to modify it
Map godMap = new HashMap();
godMap.put(&quot;TAOCP&quot;, &quot;Donald&quot;);
godMap.put(&quot;C&quot;, &quot;Dennis&quot;);

godMap = Collections.unmodifiableMap(godMap);

try {
// modifying the read-only map to check what happens
godMap.put(&quot;Unix&quot;, &quot;Ken&quot;);
} catch (UnsupportedOperationException e) {
System.out.println(&quot;You cannot modify a read only collection!&quot;);
}
}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/java-collection/read-only-collections/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Difference between Vector and ArrayList in java?</title>
		<link>http://javapapers.com/core-java/java-collection/difference-between-vector-and-arraylist-in-java/</link>
		<comments>http://javapapers.com/core-java/java-collection/difference-between-vector-and-arraylist-in-java/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 07:05:37 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Java Collection]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=63</guid>
		<description><![CDATA[java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework. All the methods of Vector is [...]]]></description>
			<content:encoded><![CDATA[<p>java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.</p>
<p><strong>All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized.</strong> All the new implementations of java collection framework is not synchronized.</p>
<p>Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.</p>
<p>Therefore as per Java API the only main difference is, Vector&#8217;s methods are synchronized and ArrayList&#8217;s methods are not synchronized.</p>
<p><strong>Vector or ArrayList? Which is better to use in java?</strong></p>
<p>In general, executing a &#8216;synchronized&#8217; method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used.</p>
<p><strong>Is there an alternate available in java for Vector?</strong><br />
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.</p>
<p>When there is no need for synchronized operation and you still look for better performance &#8216;Array&#8217; can be used instead of ArrayList. But the development is tedious, since it doesn&#8217;t provide user friendly methods.</p>
<p>When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.</p>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/java-collection/difference-between-vector-and-arraylist-in-java/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Session Tracking Methods</title>
		<link>http://javapapers.com/servlet/explain-the-methods-used-for-session-tracking/</link>
		<comments>http://javapapers.com/servlet/explain-the-methods-used-for-session-tracking/#comments</comments>
		<pubDate>Sat, 31 May 2008 07:38:48 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Java Collection]]></category>
		<category><![CDATA[Servlet]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=61</guid>
		<description><![CDATA[Following answer is applicable irrespective of the language and platform used. Before we enter into session tracking, following things should be understood. What is a session? A session is a conversation between the server and a client. A conversation consists series of continuous request and response. Why should a session be maintained? When there is [...]]]></description>
			<content:encoded><![CDATA[<p>Following answer is applicable irrespective of the language and platform used. Before we enter into session tracking, following things should be understood.</p>
<h2>What is a session?</h2>
<p>A session is a conversation between the server and a client. A conversation consists series of continuous request and response.</p>
<h2>Why should a session be maintained?</h2>
<p>When there is a series of continuous request and response from a same client to a server, the server cannot identify from which client it is getting requests. Because HTTP is a stateless protocol.</p>
<p>When there is a need to maintain the conversational state, session tracking is needed. For example, in a shopping cart application a client keeps on adding items into his cart using multiple requests. When every request is made, the server should identify in which client&#8217;s cart the item is to be added. So in this scenario, there is a certain need for session tracking.</p>
<p>Solution is, when a client makes a request it should introduce itself by providing unique identifier every time. There are five different methods to achieve this.</p>
<h2>Session tracking methods:</h2>
<ol>
<li>User authorization</li>
<li>Hidden fields</li>
<li>URL rewriting</li>
<li>Cookies</li>
<li>Session tracking API</li>
</ol>
<p>The first four methods are traditionally used for session tracking in all the server-side technologies. The session tracking API method is provided by the underlying technology (java servlet or PHP or likewise). Session tracking API is built on top of the first four methods.</p>
<h3>1. User Authorization</h3>
<p>Users can be authorized to use the web application in different ways. Basic concept is that the user will provide username and password to login to the application. Based on that the user can be identified and the session can be maintained.</p>
<h3>2. Hidden Fields</h3>
<p>&lt;INPUT TYPE=&#8221;hidden&#8221; NAME=&#8221;technology&#8221; VALUE=&#8221;servlet&#8221;&gt;<br />
Hidden fields like the above can be inserted in the webpages and information can be sent to the server for session tracking. These fields are not visible directly to the user, but can be viewed using view source option from the browsers. This type doesn&#8217;t need any special configuration from the browser of server and by default available to use for session tracking. This cannot be used for session tracking when the conversation included static resources lik html pages.</p>
<h3>3. URL Rewriting</h3>
<p>Original URL: <a href="http://server:port/servlet/ServletName">http://server:port/servlet/ServletName</a><br />
Rewritten URL: <a href="http://server:port/servlet/ServletName?sessionid=7456">http://server:port/servlet/ServletName?sessionid=7456</a><br />
When a request is made, additional parameter is appended with the url. In general added additional parameter will be sessionid or sometimes the userid. It will suffice to track the session. This type of session tracking doesn&#8217;t need any special support from the browser. Disadvantage is, implementing this type of session tracking is tedious. We need to keep track of the parameter as a chain link until the conversation completes and also should make sure that, the parameter doesn&#8217;t clash with other application parameters.</p>
<h3>4. Cookies</h3>
<p>Cookies are the mostly used technology for session tracking. Cookie is a key value pair of information, sent by the server to the browser. This should be saved by the browser in its space in the client computer. Whenever the browser sends a request to that server it sends the cookie along with it. Then the server can identify the client using the cookie.<br />
In java, following is the source code snippet to create a cookie:</p>
<p>Cookie cookie = new Cookie(&#8220;userID&#8221;, &#8220;7456&#8243;);<br />
res.addCookie(cookie);</p>
<p>Session tracking is easy to implement and maintain using the cookies. Disadvantage is that, the users can opt to disable cookies using their browser preferences. In such case, the browser will not save the cookie at client computer and session tracking fails.</p>
<h3>5. Session tracking API</h3>
<p>Session tracking API is built on top of the first four methods. This is inorder to help the developer to minimize the overhead of session tracking. This type of session tracking is provided by the underlying technology. Lets take the java servlet example. Then, the servlet container manages the session tracking task and the user need not do it explicitly using the java servlets. This is the best of all methods, because all the management and errors related to session tracking will be taken care of by the container itself.</p>
<p>Every client of the server will be mapped with a javax.servlet.http.HttpSession object. Java servlets can use the session object to store and retrieve java objects across the session. Session tracking is at the best when it is implemented using session tracking api.</p>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/servlet/explain-the-methods-used-for-session-tracking/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
	</channel>
</rss>

