<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Java Programming Forum - Learn Java Programming - Blogs - Algorithm and Data Structure</title>
		<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/</link>
		<description>Java Programming Forum - Learning Java easily</description>
		<language>en</language>
		<lastBuildDate>Tue, 21 May 2013 07:49:22 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.java-forums.org/images/misc/rss.jpg</url>
			<title>Java Programming Forum - Learn Java Programming - Blogs - Algorithm and Data Structure</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/</link>
		</image>
		<item>
			<title><![CDATA[Java's SortedMap Interface]]></title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/901-javas-sortedmap-interface.html</link>
			<pubDate>Sun, 19 Feb 2012 06:38:47 GMT</pubDate>
			<description>The java.util.Map interface’s subtype is the java.util.SortedMap. Functionality of java.util.Map is extended by this interface and elements are...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">The java.util.Map interface’s subtype is the java.util.SortedMap. Functionality of java.util.Map is extended by this interface and elements are sorted which have been stored in the Map, internally.<br />
<br />
Element’s sorting orders are as following:<br />
<br />
<ul><li style="">If java.lang.Comparable is implemented then it means that there exist element’s natural sorting order.</li><li style="">Comparator determines the order which you will be providing to the SortedSet.</li></ul><br />
<br />
Default sorting orders are ascending where iteration get started from smallest and moves forward towards largest, in data set. However elements could also be iterated in descending orders. For elements to be iterated in descending order one shall be using TreeMap.descendingKeySet() method.<br />
<br />
The java.util.TreeMap is just the Java Collection API’s SortedMap interface. The java.util.concurrent package possess this interface’s implementation.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain map operations</div>

	<pre class="brush: java">Sorted map creation is explained here.
SortedMap mapA = new TreeMap();
Comparator comparator = new MyComparator();
SortedMap mapB = new TreeMap(comparator);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/901-javas-sortedmap-interface.html</guid>
		</item>
		<item>
			<title>Removing Elements from Map</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/900-removing-elements-map.html</link>
			<pubDate>Sun, 19 Feb 2012 06:36:36 GMT</pubDate>
			<description>From Map, the removal of elements is done by using remove (Object key) method. This would be removing pair (Key, Value) from Map. 
 
Any type of...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">From Map, the removal of elements is done by using remove (Object key) method. This would be removing pair (Key, Value) from Map.<br />
<br />
Any type of elements could be put into Map. Type of objects can be limited to be used for values or keys in a Map. How to limit values or keys is well explained in the given code.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to  limit values or keys</div>

	<pre class="brush: java">Map&lt;String, MyObject&gt; map = new HashSet&lt;String, MyObject&gt;();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Map just accepts the value son MyObject type or keys of string type, as it is given above. MyObject might be the customized implementation. In such cases, iteration of values and keys is done without casting.<br />
This process is explained by this code.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to expain map iteration</div>

	<pre class="brush: java">for(MyObject anObject : map.values()){
   //do someting to anObject...
}
for(String key : map.keySet()){
   MyObject value = map.get(key);
   //do something to value
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/900-removing-elements-map.html</guid>
		</item>
		<item>
			<title>Adding and Accessing Map Elements</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/899-adding-accessing-map-elements.html</link>
			<pubDate>Sun, 19 Feb 2012 06:24:03 GMT</pubDate>
			<description><![CDATA[For addition of elements to Map, use put() method. This process is explained by the given code. 
 
Map mapA = new HashMap(); 
mapA.put("key1",...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">For addition of elements to Map, use put() method. This process is explained by the given code.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain map add operation</div>

	<pre class="brush: java">Map mapA = new HashMap();
mapA.put(&quot;key1&quot;, &quot;element 1&quot;);
mapA.put(&quot;key2&quot;, &quot;element 2&quot;);
mapA.put(&quot;key3&quot;, &quot;element 3&quot;);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Key values are mapped by calling 3 put() methods, in the code given above. Both values &amp; keys are String, in above code. To get value, use this code.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain map access operation</div>

	<pre class="brush: java">String element1 = (String) mapA.get(&quot;key1&quot;);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Values are retrieved from Map by using get() method. A key will be provided and Map would be returning the corresponding value.<br />
<br />
Iteration could also be performed on Map by using value or key iteration. This is explained by this code.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain map iteration</div>

	<pre class="brush: java">// key iterator
Iterator iterator = mapA.keySet().iterator();
// value iterator
Iterator iterator = mapA.values();
Also you can iterate the keys and get the corresponding values based on these keys.
Iterator iterator = mapA.keySet().iterator();
while(iterator.hasNext(){
  Object key   = iterator.next();
  Object value = mapA.get(key);
}
//access via new for-loop
for(Object key : mapA.keySet()) {
    Object value = mapA.get(key);
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/899-adding-accessing-map-elements.html</guid>
		</item>
		<item>
			<title>Java Map Implementations</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/898-java-map-implementations.html</link>
			<pubDate>Sun, 19 Feb 2012 06:21:21 GMT</pubDate>
			<description>Map can’t be instantiated as it is an interface. For creation of the Map interface instance, concrete implementation is required. Map interface has...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Map can’t be instantiated as it is an interface. For creation of the Map interface instance, concrete implementation is required. Map interface has these concrete implementations which are present in Collection API.<br />
<br />
•	java.util.HashMap<br />
•	java.util.Hashtable<br />
•	java.util.EnumMap<br />
•	java.util.IdentityHashMap<br />
•	java.util.LinkedHashMap<br />
•	java.util.Properties<br />
•	java.util.TreeMap<br />
•	java.util.WeakHashMap<br />
<br />
Map implementations which are commonly used are:<br />
<br />
•	TreeMap<br />
•	HashMap<br />
<br />
Such approaches could be differentiated based on implementations. Element order is the basic difference, when iteration is performed and also the time duration it would be taking for insertion or access of the Set elements.<br />
<br />
Base of the HashMap is key value pair. A key is mapped to its value. Elements stored are not supported or guaranteed by it.<br />
<br />
Base of the TreeMap is key value pair. The order in which values get iterated are guaranteed by it.<br />
How a Map instance is created is explained by these examples: <br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain map creation</div>

	<pre class="brush: java">Map mapA = new HashMap();
Map mapB = new TreeMap();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/898-java-map-implementations.html</guid>
		</item>
		<item>
			<title>Java Hash Map</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/897-java-hash-map.html</link>
			<pubDate>Sun, 19 Feb 2012 06:19:08 GMT</pubDate>
			<description>Key value paired data structure is provided by the Map interface so that for mapping the keys to values. Keys are unique which are used for retrieval...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Key value paired data structure is provided by the Map interface so that for mapping the keys to values. Keys are unique which are used for retrieval of the values, from Map.<br />
<br />
Key features are as following:<br />
<br />
<ul><li style="">Key value repair is used for storage of all values. Key is used for the retrieval.</li><li style="">When Map doesn’t contain any element, various methods throw the “NoSuchElementException”.</li><li style="">When compatibility of element is not present with map elements, ClassCastException gets thrown.</li><li style="">In Map, Null objects are not permitted. In these cases, “NullPointerException” would be thrown.</li></ul><br />
<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  A map is declared by this code.</div>

	<pre class="brush: java">HashMap&lt;String, Object&gt; myMap = new HashMap&lt;String, Object&gt;();
Map&lt;String, Object&gt; myMap = new HashMap&lt;String, Object&gt;();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <ul><li style="">Interface implemented is the major thing which differentiates the two mentioned approaches.</li><li style="">Map&lt;String,Object&gt; shall be used. This helps to provide the substitute for underlying implementation.</li></ul><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  Java Hash Map Example</div>

	<pre class="brush: java">import java.util.*;
public class MyMapDemo {
	public static void main(String&#91;&#93; args) {
	Map m1 = new HashMap();
	m1.put(&quot;Ankit&quot;, &quot;8&quot;);
	m1.put(&quot;Kapil&quot;, &quot;31&quot;);
	m1.put(&quot;Saurabh&quot;, &quot;12&quot;);
	m1.put(&quot;Apoorva&quot;, &quot;14&quot;);
	System.out.println();
	System.out.println(&quot;Elements of Map&quot;);
	System.out.print(m1);
	}
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/897-java-hash-map.html</guid>
		</item>
		<item>
			<title><![CDATA[Java's Stack Class]]></title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/896-javas-stack-class.html</link>
			<pubDate>Sun, 19 Feb 2012 06:16:11 GMT</pubDate>
			<description>Stack is considered to be a data structure which used last in first out policy. New elements could be added or removed to/from the top of Stack....</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Stack is considered to be a data structure which used last in first out policy. New elements could be added or removed to/from the top of Stack. Queue uses First in First out mechanism as compared to Stack.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  Stack usage is explained by this code</div>

	<pre class="brush: java">Stack myStack = new Stack();
myStack.push(&quot;1&quot;);
myStack.push(&quot;2&quot;);
myStack.push(&quot;3&quot;);
//look at top object (&quot;3&quot;), without taking it off the stack.    
Object topObj = stack.peek();
Object obj3 = stack.pop(); //the string &quot;3&quot; is at the top of the stack.
Object obj2 = stack.pop(); //the string &quot;2&quot; is at the top of the stack.
Object obj1 = stack.pop(); //the string &quot;1&quot; is at the top of the stack.</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 •	The push() method pushes an element at the Stack’s top.<br />
•	The peek() method gets the top element without removal from Stack.<br />
•	The pop() method gets the top element. It also plays a role in its removal from Stack.<br />
<br />
<b>Searching the Stack</b><br />
<br />
On Stack, an element could also be searched. For searching, search() method is used. For matching all the elements to the value, equal() method of object is called.<br />
<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  Stack search mechanism is explained by this given code.</div>

	<pre class="brush: java">Stack myStack = new Stack();
myStack.push(&quot;1&quot;);
myStack.push(&quot;2&quot;);
myStack.push(&quot;3&quot;);
int index = myStack.search(&quot;3&quot;);     //index = 3</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/896-javas-stack-class.html</guid>
		</item>
		<item>
			<title>Implementing a Stack in Java</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/895-implementing-stack-java.html</link>
			<pubDate>Sun, 19 Feb 2012 06:13:28 GMT</pubDate>
			<description>This post will be explaining the Java’s Stack implementation. Last in first order policy is used in Stack in which one by one elements get inserted...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">This post will be explaining the Java’s Stack implementation. Last in first order policy is used in Stack in which one by one elements get inserted in a sequence which could be retrieved later on in reverse order. Use these 2 methods to insert or read the elements to/from the Stack.<br />
<br />
•	Pop()<br />
•	Push()<br />
<br />
Code is given below to show the implementation of LIFP stack. Total elements that are needed to be inserted to Stack will be taken and after which it will be asked for those elements which are required to get inserted to Stack.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain Stack Implementation in Java</div>

	<pre class="brush: java">import java.io.*;
import java.util.*;

public class StackImplement{
  Stack&lt;Integer&gt; stack;
  String str;
  int num, n;
  public static void main(String&#91;&#93; args){
  StackImplement q = new StackImplement();
  }
  public StackImplement(){
  try{
  stack = new Stack&lt;Integer&gt;();
  InputStreamReader ir = new InputStreamReader(System.in);
  BufferedReader bf = new BufferedReader(ir);
  System.out.print(&quot;Enter number of elements : &quot;);
  str = bf.readLine();
  num = Integer.parseInt(str);
  for(int i = 1; i &lt;= num; i++){
  System.out.print(&quot;Enter elements : &quot;);
  str = bf.readLine();
  n = Integer.parseInt(str);
  stack.push(n);
  }
  }
  catch(IOException e){}
  System.out.print(&quot;Retrieved elements from the stack : &quot;);
  while (!stack.empty()){
  System.out.print(stack.pop() + &quot;  &quot;);
  }
  }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/895-implementing-stack-java.html</guid>
		</item>
		<item>
			<title>Java Stack Introduction</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/894-java-stack-introduction.html</link>
			<pubDate>Sun, 19 Feb 2012 06:11:52 GMT</pubDate>
			<description>A stack is basically a data structure which uses the following: 
 
 
* Methods for inserting the data by the help of push operation 
* Methods for...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">A stack is basically a data structure which uses the following:<br />
<br />
<ul><li style="">Methods for inserting the data by the help of push operation</li><li style="">Methods for removal of data by the help of pop operation.</li></ul><br />
<br />
Peak method is supported by Stack implementations. It will make the head to be read without removal from Stack. Last in First out policy is used in Stack. This means that data which has been inserted in data structure at last will be made to get removed first.<br />
<br />
Data will be placed at head when data is inserted into Stack. First, data will remove from Stack. This process could be better explained by this given figure:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/jcreator/3073d1329631775-add-jlist-column-1.jpg" border="0" alt="Name:  1.JPG
Views: 67
Size:  5.5 KB" class="thumbnail" style="float:CONFIG" /><br />
<b><br />
Before push operation. After push operation.</b><br />
<br />
<img src="http://www.java-forums.org/attachments/jcreator/3074d1329631843-add-jlist-column-1.jpg" border="0" alt="Name:  1.JPG
Views: 64
Size:  5.8 KB" class="thumbnail" style="float:CONFIG" /><br />
<br />
<b>Before pop operation. After pop operation</b></div><br />
Stack implementation is provided by Java in form of java.util.Stack. Stack operations are explained by this code:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain Java Stack</div>

	<pre class="brush: java">import java.util.Stack;

public class MyStackDemo
{
        public static void main(String args&#91;&#93;)
        {
                // Create a new, empty stack
                Stack lifo = new Stack();

                // Let's add some items to it
                for (int i = 1; i &lt;= 10; i++)
                {
                        lifo.push ( new Integer(i) );
                }

                // Last in first out means reverse order
                while ( !lifo.empty() )
                {
                        System.out.print ( lifo.pop() );
                        System.out.print ( ',' );
                }

                // Empty, let's lift off!
                System.out.println (&quot; LIFT-OFF!&quot;);
        }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/894-java-stack-introduction.html</guid>
		</item>
		<item>
			<title>Java Deque Implementations</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/893-java-deque-implementations.html</link>
			<pubDate>Sun, 19 Feb 2012 06:07:24 GMT</pubDate>
			<description>Collection interface subtype is DeQue interface. In DeQue interface, all of the Collection interface methods are present. 
 
DeQue interface instance...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Collection interface subtype is DeQue interface. In DeQue interface, all of the Collection interface methods are present.<br />
<br />
DeQue interface instance can be created. This interface’s concrete implementation is required to do so. In Collections API, these DeQue implementations are present. Among them, you may select to use.<br />
<br />
<ul><li style="">java.util.LinkedList</li><li style="">java.util.ArrayDeque</li></ul><br />
<br />
LinkedList is considered to be a pretty queue implementation.<br />
<br />
In an array, elements are stored internally in ArrayDeQue. If elements number get exceeded as compared to the array space, an allocation of a new space is done and elements get moved over. We can say that ArrayQueue will be growing as per requirement even when its elements are stored in an array.<br />
<br />
In Java.util.concurrent package, Queue implementations are present.<br />
<br />
How a DeQue instance is created is shown in the given examples.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to present Deque</div>

	<pre class="brush: java">Deque dequeA = new LinkedList();
Deque dequeB = new ArrayDeque();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/893-java-deque-implementations.html</guid>
		</item>
		<item>
			<title>Adding and Accessing Queue Elements</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/892-adding-accessing-queue-elements.html</link>
			<pubDate>Sun, 19 Feb 2012 06:05:17 GMT</pubDate>
			<description>Call add() method for addition of the elements to Queue. This is an inherited type of method of Collection interface. 
 
Different elements are added...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Call add() method for addition of the elements to Queue. This is an inherited type of method of Collection interface.<br />
<br />
Different elements are added to a Queue which is explained by this given code.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain add operation in Java Queue</div>

	<pre class="brush: java">Queue myQueue = new LinkedList();
myQueue.add(&quot;element 1&quot;);
myQueue.add(&quot;element 2&quot;);
myQueue.add(&quot;element 3&quot;);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 In a Queue, added elements get stored in internal order and they are dependent upon implementation. Same happens with element retrieval.<br />
<br />
To get 1st element from queue, use this code:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to get first element of Java Queue</div>

	<pre class="brush: java">Object firstElement = myQueue.element();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 For removing or taking the 1st element, removal method is used. Following code explains it.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to remove elements from Java Queue</div>

	<pre class="brush: java">Queue myQueue = new LinkedList();
myQueue.add(&quot;element 0&quot;);
myQueue.add(&quot;element 1&quot;);
myQueue.add(&quot;element 2&quot;);

//access via Iterator
Iterator iterator = myQueue.iterator();
while(iterator.hasNext(){
  String element = (String) iterator.next();
}

//access via new for-loop
for(Object object : myQueue) {
    String element = (String) object;
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/892-adding-accessing-queue-elements.html</guid>
		</item>
		<item>
			<title>Java Queue Implementations</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/891-java-queue-implementations.html</link>
			<pubDate>Sun, 19 Feb 2012 06:02:32 GMT</pubDate>
			<description>Collection Interface’s subtype is Queue. In the Queue interface, collection interface methods are present automatically. 
 
Queue interface instance...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Collection Interface’s subtype is Queue. In the Queue interface, collection interface methods are present automatically.<br />
<br />
Queue interface instance can’t be created. Queue interface concrete implementations are required. In Collections API, the available Queue implementations are as following. Among them, one can make a choice:<br />
<ul><li style="">java.util.PriorityQueue</li><li style="">java.util.LinkedList</li></ul><br />
<br />
LinkedList is a considered to be a standard queue implementation. <br />
<br />
Elements are stored internally by PriorityQueue in accordance with their natural order or in accordance in which a Comparator is passed to PriorityQueue.<br />
<br />
In java.util.concurrent package, Queue implementations are also present.<br />
<br />
How a Queue instance is created is explained in these given examples:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain Java Queue Implementations</div>

	<pre class="brush: java">Queue queueA = new LinkedList();
Queue queueB = new PriorityQueue();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/891-java-queue-implementations.html</guid>
		</item>
		<item>
			<title>Java – Queue Example</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/890-java-queue-example.html</link>
			<pubDate>Sun, 19 Feb 2012 06:00:27 GMT</pubDate>
			<description>Queue base is at first in first out policy. The Queue interface is present in java.util package. Queue size is not required to be specified just like...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Queue base is at first in first out policy. The Queue interface is present in java.util package. Queue size is not required to be specified just like it is done in arrays.<br />
<br />
Functionality of Queue is explained in the given code.<br />
<br />
<b>Example of Queue </b><br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain Java Queue</div>

	<pre class="brush: java">import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class QueueTesting {

    public static void main(String&#91;&#93; args) {

        Queue&lt;String&gt; myQueue=new LinkedList&lt;String&gt;();

        myQueue.add(&quot;b&quot;);
        myQueue.add(&quot;a&quot;);
        myQueue.add(&quot;c&quot;);
        myQueue.add(&quot;e&quot;);
        myQueue.add(&quot;d&quot;);

        Iterator it= myQueue.iterator();

        System.out.println(&quot;Initial Size of Queue :&quot;+ myQueue.size());

        while(it.hasNext())
        {
            String iteratorValue=(String)it.next();
            System.out.println(&quot;Queue Next Value :&quot;+iteratorValue);
        }

        // get value and does not remove element from queue
        System.out.println(&quot;Queue peek :&quot;+ myQueue.peek());

        // get first value and remove that object from queue
        System.out.println(&quot;Queue poll :&quot;+ myQueue.poll());

        System.out.println(&quot;Final Size of Queue :&quot;+ myQueue.size());
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <b>Output</b><br />
<br />
Initial Size of Queue :5<br />
Queue Next Value :b<br />
Queue Next Value :a<br />
Queue Next Value :c<br />
Queue Next Value :e<br />
Queue Next Value :d<br />
Queue peek :b<br />
Queue poll :b<br />
Final Size of Queue :4</blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/890-java-queue-example.html</guid>
		</item>
		<item>
			<title>Java Queue</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/889-java-queue.html</link>
			<pubDate>Sun, 19 Feb 2012 05:58:47 GMT</pubDate>
			<description>Java Queue is also collection interface for holding the objects. Collections store or hold elements, before processing. Following operations are...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Java Queue is also collection interface for holding the objects. Collections store or hold elements, before processing. Following operations are provided by Queues:<br />
<br />
1.	Insertion<br />
2.	Inspections<br />
3.	Extraction<br />
4.	(All) Other Collection operations<br />
<br />
Queues are basically first in first order implements. They don’t depend on use order. Poll() and remove() methods are used to remove the elements present at head.<br />
<br />
From queue’s tail, all elements gets added in case of FIFO queue. During different queues implementations, these rules might be different.<br />
<br />
•	An element is inserted by offer() method.<br />
•	The poll() or remove() methods are used for removal of the element. An exception is thrown by remove() method in case queue is empty. On the other hand, exception is not thrown by the poll() method. Just nulls is returned.<br />
•	The peek() and element() methods are used to return the elements. Head of queue is not removed by it.</blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/889-java-queue.html</guid>
		</item>
		<item>
			<title>Java Linked List Implementations</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/888-java-linked-list-implementations.html</link>
			<pubDate>Sun, 19 Feb 2012 05:57:21 GMT</pubDate>
			<description>Collection interface is subtype of a List. Collection interface methods are also present in list interface. 
List can’t be instantiated as it is an...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Collection interface is subtype of a List. Collection interface methods are also present in list interface.<br />
List can’t be instantiated as it is an interface. For creation of instance of the List, create concrete implementation instance of the interface. In Java Collection APIs, these options are present:<br />
<br />
•	java.util.ArrayList<br />
•	java.util.LinkedList<br />
•	java.util.Vector<br />
•	java.util.Stack<br />
<br />
In java.util.concurrent package, List implementations are present.<br />
<br />
How a List instance is created is shown here in examples:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain Linked List Implementations</div>

	<pre class="brush: java">List listA = new ArrayList();
List listB = new LinkedList();
List listC = new Vector();
List listD = new Stack();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/888-java-linked-list-implementations.html</guid>
		</item>
		<item>
			<title>What is the advantage of using an Iterator compared to the get(index) method?</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/887-what-advantage-using-iterator-compared-get-index-method.html</link>
			<pubDate>Sun, 19 Feb 2012 05:55:02 GMT</pubDate>
			<description>One of the following methods should be used to iterate the List. 
 
1.	Iterator 
2.	get(index) 
 
get(index) method is quite slower in comparison...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">One of the following methods should be used to iterate the List.<br />
<br />
1.	Iterator<br />
2.	get(index)<br />
<br />
get(index) method is quite slower in comparison with Iterator. <br />
<br />
<b>Consider this example:</b><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain Iterator</div>

	<pre class="brush: java">public class TestClass {
  public static void main(String&#91;&#93; args) {
    int  len = 100000;
    LinkedList linkedLst = new LinkedList(); 
    ArrayList  arrayLst = new ArrayList();
    for (int m =0; m!= len; m++) {
      int x = (int)Math.random();
      linkedLst.add(x);
      arrayLst.add(x);
    }
    
    long t = System.currentTimeMillis();
    for (int i = 0; i!=len; i++) {
      linkedLst.get(i);
    }
    t = System.currentTimeMillis() - t;
    System.out.println(&quot;LinkedList -- get(index) takes &quot;+t +&quot;(ms)&quot;);

    t = System.currentTimeMillis();
    for  (Iterator itr = linkedLst.iterator();
      itr.hasNext();) {
      itr.next();     		
    }		
    t = System.currentTimeMillis() - t;
    System.out.println(&quot;LinkedList -- Iterator takes &quot;+t +&quot;(ms)&quot;);
    
    t = System.currentTimeMillis();
    for (int i = 0; i!=len; i++) {
      arrayLst.get(i);
    }
    t = System.currentTimeMillis() - t;
    System.out.println(&quot;ArrayList -- get(index) takes &quot;+t +&quot;(ms)&quot;);

    t = System.currentTimeMillis();
    for  (Iterator itr = arrayLst.iterator();
           itr.hasNext();) {
      itr.next();     		
    }		
    t = System.currentTimeMillis() - t;
    System.out.println(&quot;ArrayList -- Iterator takes &quot;+t +&quot;(ms)&quot;);
		
  }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <b>Output:</b><br />
<br />
LinkedList -- get(index) takes 25777(ms)<br />
LinkedList -- Iterator takes 0(ms)<br />
ArrayList -- get(index) takes 10(ms)<br />
ArrayList -- Iterator takes 10(ms)</blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/887-what-advantage-using-iterator-compared-get-index-method.html</guid>
		</item>
		<item>
			<title>Removing an Item from the List</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/886-removing-item-list.html</link>
			<pubDate>Sun, 19 Feb 2012 05:52:48 GMT</pubDate>
			<description>For removal of an item out of List, different methods would be discussed here. Use these methods. 
 
 
* remove() 
* removeFirst() 
* removeLast()</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">For removal of an item out of List, different methods would be discussed here. Use these methods.<br />
<br />
<ul><li style="">remove()</li><li style="">removeFirst()</li><li style="">removeLast()</li></ul><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code:  This is the code to explain removal of an element from the List</div>

	<pre class="brush: java">theList.removeLast();
theList.remove(0);
theList.removeFirst();
theList.removeFirst();

import java.util.*;
public class TestLinkedList
{
public static void main(String&#91;&#93; args)
{
int pause;
String temp;
LinkedList theList = new LinkedList();
theList.add(&quot;1&quot;);
theList.add(0, &quot;2&quot;);
theList.addFirst(&quot;3&quot;);
theList.addLast(&quot;4&quot;);
System.out.println(&quot;Size: &quot;+theList.size());
System.out.println(&quot;Element 0: &quot;+theList.get(0));
temp = theList.get(1);
System.out.println(&quot;Element 1: &quot;+temp);
System.out.println(&quot;Size :&quot;+ theList.size());
System.out.println(&quot;First :&quot;+ theList.getFirst());
System.out.println(&quot;Last :&quot;+ theList.getLast());
System.out.println(&quot;Size yet again: &quot;+theList.size());
theList.removeFirst();
System.out.println(&quot;Size yet again: &quot;+theList.size());
theList.removeLast();
theList.remove(0);
theList.removeFirst();
System.out.println(&quot;Empty Size: &quot;+theList.size());
for(int x=0; x&lt;100; x++) {
theList.add(&quot;&quot;+x);
}
System.out.println(&quot;Loop Size :&quot;+ theList.size());
for(int x=100; x&gt;0; x--) {
theList.removeLast();
}
System.out.println(&quot;Empty Size: &quot;+theList.size());
}
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/886-removing-item-list.html</guid>
		</item>
		<item>
			<title>Accessing Items from a List</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/885-accessing-items-list.html</link>
			<pubDate>Sun, 19 Feb 2012 05:50:37 GMT</pubDate>
			<description>*Get List Size:* 
 
To get the List size, method “size()”is used. Size of MyList will be printed by this code. 
 
System.out.println(MyList.size());...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><b>Get List Size:</b><br />
<br />
To get the List size, method “size()”is used. Size of MyList will be printed by this code.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">System.out.println(MyList.size());</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <b>Get Item:</b><br />
<br />
For getting a specific spot value, use get() method. In this method index of the List is provided. In case when index is not present, IndexOutOfBoundsException gets thrown.<br />
<br />
Element is not removed from the list by this method. Just this object is returned. This given code will be getting the List element &amp; will be displaying it.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">System.out.println(MyList.get(0));
//or to store the value first
temp = MyList.get(0);
System.out.println(temp);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 First item will be printed out in our list. However, removal from list would not be done.<br />
Also To get 1st &amp; last elements, these 2 methods would be used.<br />
1.	getLast()<br />
2.	getFirst()</blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/885-accessing-items-list.html</guid>
		</item>
		<item>
			<title>Adding Elements to a List</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/884-adding-elements-list.html</link>
			<pubDate>Sun, 19 Feb 2012 05:47:51 GMT</pubDate>
			<description><![CDATA[Elements are added to list by using these 4 different methods given below: 
 
MyList.add("1"); 
//or 
MyList.add(varName); 
An item is added  by it...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Elements are added to list by using these 4 different methods given below:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">MyList.add(&quot;1&quot;);
//or
MyList.add(varName);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 An item is added  by it to end of list.<br />
At a particular location, elements could be added as given below.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">MyList.add(0, &quot;2&quot;);
//or
MyList.add(0, varName);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Zero tells the element location, as given in above code. This is basically added as 1st element.<br />
Other two ways of adding are self documented. They are addLast() &amp; addFirst() functions. Just like add() methods, they are used.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">MyList.addFirst(&quot;3&quot;);
MyList.addLast(&quot;4&quot;);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/884-adding-elements-list.html</guid>
		</item>
		<item>
			<title>Java Linked Lists</title>
			<link>http://www.java-forums.org/blogs/algorithm-and-data-structure/883-java-linked-lists.html</link>
			<pubDate>Sun, 19 Feb 2012 05:45:32 GMT</pubDate>
			<description>Arrays of data are stored by Linked Lists, which is considered to be a well known way of doing so. Linked List has many benefits among which a major...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Arrays of data are stored by Linked Lists, which is considered to be a well known way of doing so. Linked List has many benefits among which a major one is that fixed size specification is not needed. To the list, you can add desired number of elements.<br />
<br />
Linked lists types would be discussed in this post.<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/awt-swing/3072d1329630290-problem-about-java-mail-client-program-1.jpg" border="0" alt="Name:  1.JPG
Views: 332
Size:  48.1 KB" class="thumbnail" style="float:CONFIG" /><br />
<br />
<b>Basic Types of Linked Lists</b></div></blockquote>

]]></content:encoded>
			<dc:creator>Algorithm and Data Structure</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/algorithm-and-data-structure/883-java-linked-lists.html</guid>
		</item>
	</channel>
</rss>
