<?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>How-To Java | Automation Dojos</title>
	<atom:link href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.automationdojos.com/category/all-how-to/how-to-java/</link>
	<description>Applied Automation Ops</description>
	<lastBuildDate>Mon, 20 Feb 2023 13:58:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.automationdojos.com/wp-content/uploads/2021/06/Adjo_X_fav_32.png</url>
	<title>How-To Java | Automation Dojos</title>
	<link>https://www.automationdojos.com/category/all-how-to/how-to-java/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How To Create Maven Project in Eclipse With Archetype</title>
		<link>https://www.automationdojos.com/create-maven-project-with-archetype/</link>
					<comments>https://www.automationdojos.com/create-maven-project-with-archetype/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sat, 10 Apr 2021 21:07:00 +0000</pubDate>
				<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven-project]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=21183</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/create-maven-project-with-archetype/">How To Create Maven Project in Eclipse With Archetype</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-archetype.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" fetchpriority="high" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-archetype.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-archetype-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-archetype-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-archetype-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-archetype-400x240.png 400w" sizes="(max-width: 575px) 100vw, 575px" /></p>
<p>Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output jar/war file, etc. This makes the code less error-prone. Maven uses an XML configuration file known as the POM file. Developers need to specify information like the JAR files required [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/create-maven-project-with-archetype/">How To Create Maven Project in Eclipse With Archetype</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/create-maven-project-with-archetype/">How To Create Maven Project in Eclipse With Archetype</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<div role="navigation" aria-label="Table of Contents" class="simpletoc blog-post-toc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#maven-basics">Maven Basics</a>

</li>
<li><a href="#eclipse-maven-integration">Eclipse Maven Integration</a>

</li>
<li><a href="#create-maven-project-with-archetype-in-eclipse">Create Maven Project with Archetype in Eclipse</a>


<ul><li>
<a href="#create-new-artefact-of-type-other">Create new artefact of type &#8216;Other&#8217;</a>

</li>
<li><a href="#select-maven-project">Select Maven Project</a>

</li>
<li><a href="#uncheck-create-simple-project">Uncheck &#8216;Create Simple Project&#8217;</a>

</li>
<li><a href="#use-filter-mavenarchetypewebapp">Use filter &#8216;maven-archetype-webapp&#8217;</a>

</li>
<li><a href="#select-mavenarchetypewebapp">Select maven-archetype-webapp.</a>

</li>
<li><a href="#input-group-id-and-artifact-id">Input Group Id and Artifact Id</a>

</li>
<li><a href="#verify-new-maven-project-in-eclipse">Verify New Maven project in Eclipse</a>

</li>
<li><a href="#finish">Finish!</a>
</li>
</ul>
</li></ul></div>

<h2 class="wp-block-heading" id="maven-basics">Maven Basics</h2>


<p>Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output jar/war file, etc. This makes the code less error-prone. Maven uses an XML configuration file known as the POM file. Developers need to specify information like the JAR files required by an application, the type of output artifact (jar,war, etc). Maven then uses the instructions in the POM file to build the application.</p>


<h2 class="wp-block-heading" id="eclipse-maven-integration">Eclipse Maven Integration</h2>


<p>Most of the recent Eclipse releases already include Maven. So, developers do not need to do anything special to install Maven in Eclipse. &nbsp;However, some earlier versions of Eclipse do not include Maven, so it needs to be installed explicitly from the Eclipse marketplace. Once Maven is set up with Eclipse, it allows you to easily create a Maven project and use all the Maven features like dependency resolution within Eclipse itself.</p>


<h2 class="wp-block-heading" id="create-maven-project-with-archetype-in-eclipse">Create Maven Project with Archetype in Eclipse</h2>


<p>Maven archetype provides a template that can be used to create a particular type of Maven project. If a Maven project is created with an archetype selection, Maven creates the POM file accordingly. Some of the common Maven archetypes are as follows:</p>



<div class="su-list" style="margin-left:0px">



<ul class="wp-block-list">
<li><i class="sui sui-gear" style="color:#888"></i> <strong>maven-archetype-quickstart</strong> – Can be used to create a basic Maven project</li>



<li><i class="sui sui-gear" style="color:#888"></i> <strong>maven-archetype-j2ee-simple</strong> – Can be used to create a simple j2ee project</li>



<li><i class="sui sui-gear" style="color:#888"></i> <strong>maven-archetype-webapp</strong> – Can be used to create a web application</li>
</ul>



</div>



<p>In order to create a Maven project with a webapp archetype, the following steps need to be followed:</p>


<h3 class="wp-block-heading" id="create-new-artefact-of-type-other">Create new artefact of type &#8216;Other&#8217;</h3>


<p>On Eclipse main menu, navigate to <em>&#8216;File &gt; New&#8217;</em> and select &#8216;Other&#8217;</p>



<figure class="wp-block-image size-large is-resized is-style-default td-img-style-shadow"><img decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-1-690x1024.png" alt="" class="wp-image-21164" width="428" height="631"/></figure>


<h3 class="wp-block-heading" id="select-maven-project">Select <strong>Maven Project</strong></h3>


<p>Select the option &#8216;Maven Project&#8217; and click Next.</p>



<figure class="wp-block-image size-large is-resized is-style-default"><img decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2.png" alt="" class="wp-image-21163" width="568" height="411" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-300x218.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-150x109.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-324x235.png 324w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-200x145.png 200w" sizes="(max-width: 568px) 100vw, 568px" /></figure>


<h3 class="wp-block-heading" id="uncheck-create-simple-project">Uncheck &#8216;<strong>Create Simple Project</strong>&#8216;</h3>


<p>Leave the <strong>Create a</strong> <strong>Simple Project (skip archetype selection)</strong> checkbox unchecked. Click <strong>Next</strong></p>



<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-10.png" alt="" class="wp-image-21170" width="597" height="541" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-10-150x137.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-10-200x182.png 200w" sizes="auto, (max-width: 597px) 100vw, 597px" /></figure>


<h3 class="wp-block-heading" id="use-filter-mavenarchetypewebapp">Use filter &#8216;<strong>maven-archetype-webapp</strong>&#8216;</h3>


<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-11-1024x552.png" alt="" class="wp-image-21169" width="701" height="371"/></figure>


<h3 class="wp-block-heading" id="select-mavenarchetypewebapp">Select <strong>maven-archetype-webapp.</strong></h3>


<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-12-1024x553.png" alt="" class="wp-image-21168" width="702" height="373"/></figure>


<h3 class="wp-block-heading" id="input-group-id-and-artifact-id">Input <strong>Group Id</strong> and <strong>Artifact Id</strong></h3>


<p>Enter a value for <strong>Group Id</strong> and <strong>Artifact Id</strong>. Change <strong>version</strong> and <strong>package </strong>if required. Click <strong>Finish</strong></p>



<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-13-1024x556.png" alt="" class="wp-image-21167" width="716" height="382"/></figure>


<h3 class="wp-block-heading" id="verify-new-maven-project-in-eclipse">Verify New Maven project in Eclipse</h3>


<p>This creates a Maven project in Eclipse with the <strong>Maven directory structure</strong> and <strong>POM file</strong> as follows</p>



<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="474" height="303" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-14.png" alt="" class="wp-image-21166" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-14.png 474w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-14-300x192.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-14-150x96.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-14-200x128.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-14-400x256.png 400w" sizes="auto, (max-width: 474px) 100vw, 474px" /></figure>


<h3 class="wp-block-heading" id="finish">Finish!</h3>


<p>Finish! This POM file can then be modified by adding dependencies and other information as required.</p>


[td_smart_list_end]


<hr class="wp-block-separator has-css-opacity"/>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21183" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21183" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21183" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21183&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21183" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=21183" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=21183', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/create-maven-project-with-archetype/">How To Create Maven Project in Eclipse With Archetype</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/create-maven-project-with-archetype/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Create Maven Project in Eclipse Without Archetype</title>
		<link>https://www.automationdojos.com/create-maven-without-archetype/</link>
					<comments>https://www.automationdojos.com/create-maven-without-archetype/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sun, 10 Jan 2021 15:03:00 +0000</pubDate>
				<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven-project]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=21159</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/create-maven-without-archetype/">How To Create Maven Project in Eclipse Without Archetype</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-no-archetype.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-no-archetype.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-no-archetype-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-no-archetype-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-no-archetype-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-maven-no-archetype-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Maven is a very popular build and dependency management tool. Eclipse is an IDE that helps developers write and run Java code easily. In this article, I will be explaining how you can create a Maven project in Eclipse. Maven Basics Maven automates the steps involved in building a software application like adding the JAR [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/create-maven-without-archetype/">How To Create Maven Project in Eclipse Without Archetype</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/create-maven-without-archetype/">How To Create Maven Project in Eclipse Without Archetype</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<div role="navigation" aria-label="Table of Contents" class="simpletoc blog-post-toc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#maven-basics">Maven Basics</a>

</li>
<li><a href="#eclipse-maven-integration">Eclipse Maven Integration</a>

</li>
<li><a href="#creating-a-simple-maven-project">Creating a Simple Maven Project</a>


<ul><li>
<a href="#create-new-artefact-of-type-other">Create new artefact of type &#8216;Other&#8217;</a>

</li>
<li><a href="#select-maven-project-click-next">Select Maven Project. Click Next</a>

</li>
<li><a href="#select-create-a-simple-project">Select &#8216;Create a Simple Project&#8217;</a>

</li>
<li><a href="#enter-value-for-group-id-amp-artifact-id">Enter value for Group Id &amp; Artifact Id.</a>

</li>
<li><a href="#change-version-and-packaging">Change version and packaging</a>

</li>
<li><a href="#new-maven-project-created">New Maven Project Created</a>

</li>
<li><a href="#view-your-pom-file">View Your POM File</a>

</li>
<li><a href="#finish">Finish!</a>
</li>
</ul>
</li></ul></div>


<p>Maven is a very popular build and dependency management tool. Eclipse is an IDE that helps developers write and run Java code easily. In this article, I will be explaining how you can create a Maven project in Eclipse.</p>


<h2 class="wp-block-heading" id="maven-basics">Maven Basics</h2>


<p>Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output jar/war file, etc. This makes the code less error-prone. Maven uses an XML configuration file known as the POM file. Developers need to specify information like the JAR files required by an application, the type of output artifact (jar,war, etc). Maven then uses the instructions in the POM file to build the application.</p>


<h2 class="wp-block-heading" id="eclipse-maven-integration">Eclipse Maven Integration</h2>


<p>Most of the recent Eclipse releases already include Maven. So, developers do not need to do anything special to install Maven in Eclipse. &nbsp;However, some earlier versions of Eclipse do not include Maven, so it needs to be installed explicitly from the Eclipse marketplace. Once Maven is set up with Eclipse, it allows you to easily create a Maven project and use all the Maven features like dependency resolution within Eclipse itself.</p>


<h2 class="wp-block-heading" id="creating-a-simple-maven-project">Creating a Simple Maven Project</h2>


<p>In order to create a simple Maven project in Eclipse, the following steps need to be followed:</p>


<h3 class="wp-block-heading" id="create-new-artefact-of-type-other">Create new artefact of type &#8216;Other&#8217;</h3>


<p>From menu File &gt; New select &#8216;Other&#8217;</p>



<figure class="wp-block-image size-large is-resized is-style-default td-img-style-shadow"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-1-690x1024.png" alt="" class="wp-image-21164" width="428" height="631"/></figure>


<h3 class="wp-block-heading" id="select-maven-project-click-next">Select <strong>Maven Project</strong>. Click <strong>Next</strong></h3>


<p>On the &#8216;new project&#8217; wizard select &#8216;Maven Project&#8217;</p>



<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2.png" alt="" class="wp-image-21163" width="618" height="447" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-300x218.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-150x109.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-324x235.png 324w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-2-200x145.png 200w" sizes="auto, (max-width: 618px) 100vw, 618px" /></figure>


<h3 class="wp-block-heading" id="select-create-a-simple-project">Select &#8216;Create a Simple Project&#8217;</h3>


<p>Click the Create a Simple Project (skip archetype selection) checkbox. Click Next</p>



<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-3.png" alt="" class="wp-image-21162" width="628" height="578" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-3-300x277.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-3-150x138.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-3-455x420.png 455w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-3-200x185.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-3-400x369.png 400w" sizes="auto, (max-width: 628px) 100vw, 628px" /></figure>


<h3 class="wp-block-heading" id="enter-value-for-group-id-amp-artifact-id">Enter value for <strong>Group Id</strong> &amp; <strong>Artifact Id</strong>.</h3>


<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4.png" alt="" class="wp-image-21161" width="656" height="598" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4-300x274.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4-150x137.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4-696x636.png 696w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4-460x420.png 460w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4-200x183.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-4-400x366.png 400w" sizes="auto, (max-width: 656px) 100vw, 656px" /></figure>


<h3 class="wp-block-heading" id="change-version-and-packaging">Change <strong>version</strong> and <strong>packaging</strong></h3>


<p>Change <strong>version</strong> and <strong>packaging</strong> if required (This is an optional step). Click <strong>Finish</strong></p>



<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-5.png" alt="" class="wp-image-21160" width="609" height="555" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-5-150x138.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-5-200x183.png 200w" sizes="auto, (max-width: 609px) 100vw, 609px" /></figure>


<h3 class="wp-block-heading" id="new-maven-project-created">New Maven Project Created</h3>


<p>This creates a Maven project in Eclipse with the <strong>Maven directory structure</strong> and <strong>POM file</strong> as follows</p>



<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="294" height="365" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-6.png" alt="" class="wp-image-21158" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-6.png 294w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-6-242x300.png 242w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-6-150x186.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-6-161x200.png 161w" sizes="auto, (max-width: 294px) 100vw, 294px" /></figure>


<h3 class="wp-block-heading" id="view-your-pom-file">View Your POM File</h3>


<p>The default POM created has the group id, artifact id, version and packaging specified at the time of creation as follows</p>



<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="1024" height="190" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-1024x190.png" alt="" class="wp-image-21157" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-1024x190.png 1024w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-300x56.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-768x143.png 768w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-150x28.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-696x129.png 696w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-200x37.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7-400x74.png 400w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-create-maven-proj-7.png 1045w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>


<h3 class="wp-block-heading" id="finish">Finish!</h3>


<p>This POM file can then be modified by adding dependencies and other information as required.</p>


[td_smart_list_end]


<hr class="wp-block-separator has-css-opacity"/>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21159" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21159" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21159" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21159&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21159" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=21159" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=21159', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/create-maven-without-archetype/">How To Create Maven Project in Eclipse Without Archetype</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/create-maven-without-archetype/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Create Singleton Class in Java</title>
		<link>https://www.automationdojos.com/how-to-create-singleton-class-in-java/</link>
					<comments>https://www.automationdojos.com/how-to-create-singleton-class-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Wed, 15 Jul 2020 09:05:00 +0000</pubDate>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-class]]></category>
		<category><![CDATA[java-design-patterns]]></category>
		<category><![CDATA[singleton-class]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=22511</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/how-to-create-singleton-class-in-java/">How To Create Singleton Class in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Design patterns Overview Design patterns are basically solutions to programming problems that developers normally encounter during software development. Design patterns were first documented by four authors known as the Gang of Four (GoF). Design patterns are broadly classified into creational, structural, and behavioral patterns. Creational patterns aid in object creation, structural patterns help to alter [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/how-to-create-singleton-class-in-java/">How To Create Singleton Class in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/how-to-create-singleton-class-in-java/">How To Create Singleton Class in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/11/java-howto-singleton-1-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#design-patterns-overview">Design patterns Overview</a>

</li>
<li><a href="#introduction-to-singleton-pattern">Introduction to Singleton Pattern</a>

</li>
<li><a href="#singleton-pattern-implementation">Singleton Pattern Implementation</a>


<ul><li>
<a href="#eager-initialization">Eager Initialization</a>

</li>
<li><a href="#lazy-initialization">Lazy Initialization</a>

</li>
<li><a href="#static-block-initialization">Static block Initialization</a>

</li>
</ul>
<li><a href="#advantages-of-singleton">Advantages of Singleton</a>

</li>
<li><a href="#disadvantages-of-singleton">Disadvantages of Singleton</a>

</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul>

<h2 class="wp-block-heading" id="design-patterns-overview">Design patterns Overview</h2>


<p>Design patterns are basically solutions to programming problems that developers normally encounter during software development. Design patterns were first documented by four authors known as the <a href="https://en.wikipedia.org/wiki/Design_Patterns" target="_blank" rel="noreferrer noopener">Gang of Four (GoF)</a>. </p>



<p>Design patterns are broadly classified into creational, structural, and behavioral patterns. Creational patterns aid in object creation, structural patterns help to alter the structure of an object while behavioral patterns mostly have to do with algorithms.</p>


<h2 class="wp-block-heading" id="introduction-to-singleton-pattern">Introduction to Singleton Pattern</h2>


<p>Singleton is a creational design pattern; it helps in creating an object of a class. The Singleton pattern ensures that there is only one instance of a class. It is generally used for a class that consumes a lot of memory or one that uses expensive resources. Singleton ensures that there is only one instance of the class, thus saving on memory and other resources. The singleton pattern is mostly used for database connections, caching, logging, and other programming situations that mandate having a single object of a class.</p>


<h2 class="wp-block-heading" id="singleton-pattern-implementation">Singleton Pattern Implementation</h2>


<p>There are three main ways in which the singleton pattern can be implemented. All the approaches make use of a static instance, a private constructor, and a public getter method.</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">


<h3 class="wp-block-heading" id="eager-initialization">Eager Initialization</h3>


<p>In this approach, the Singleton instance is created at the time the class is loaded. The following code demonstrates creating a Singleton class using this approach:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class MySingleton {
   private static MySingleton mySingleton = new MySingleton();
   private MySingleton() {
   }
   public static MySingleton getMySingleton() {
      return mySingleton;
   }
}</pre>



<p>This code specifies a class called&nbsp;<strong>MySingleton</strong>. It has a private static instance field called&nbsp;<strong>mySingleton</strong>&nbsp;that is initialized at the time of creation. It also has a private constructor. Finally, there is a public getter method called&nbsp;<strong>getMySingleton().</strong>&nbsp;This method returns the&nbsp;<strong>mySingleton</strong> instance.&nbsp;</p>



<p>So, in this approach, the singleton instance is created much before it is used. If the singleton is a heavy class using a lot of resources, then this approach is not appropriate.</p>


</div></div>
<div class="su-spacer" style="height:15px"></div>


<div class="su-note"  style="border-color:#d1cec9;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#ebe8e3;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">


<h3 class="wp-block-heading" id="lazy-initialization">Lazy Initialization</h3>


<p>In this approach, the Singleton instance is created when it is first requested. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public class MySingleton {
private static MySingleton mySingleton;
   private MySingleton() {
   }
   public static MySingleton getMySingleton() {
      if (mySingleton == null) {
         mySingleton = new MySingleton();
      }
      return mySingleton;
   }
}</pre>



<p>This approach is similar to the eager initialization approach in that it has a private static instance field, a private constructor and a public getter method. The only difference is that the singleton instance is not created at start-up. Instead, the&nbsp;<strong>getMySingleton()</strong>&nbsp;method checks if an instance of the singleton class exists. If so, it just returns it. If an instance does not exist, it creates it via the private constructor.</p>



<p>So, in this approach the singleton instance is created when it is first requested. This is the preferred approach for singleton classes that use a lot of resources.</p>


</div></div>
<div class="su-spacer" style="height:15px"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">


<h3 class="wp-block-heading" id="static-block-initialization">Static block Initialization</h3>


<p>In this approach, the singleton instance is created within a static block. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class MySingleton {
   private static MySingleton mySingleton;
   private MySingleton() {
   }
   
   static {
      mySingleton = new MySingleton();
   }
   public static MySingleton getMySingleton() {
      return mySingleton;
   }
   
}</pre>



<p>Just like the previous approaches, this code defines a private static instance, a private constructor and a public getter method. In addition, this code also defines a static block. The singleton instance&nbsp;<strong>mySingleton</strong>&nbsp;is created in this static block. Since a static block is executed only once this ensures that there is only one instance of the singleton. Since the static block is executed when the class is loaded, the singleton instance is created at start-up just like it is done in the eager initialization approach.</p>



<p>The advantage of this approach over the eager initialization approach is that, exception handling code can be added in the static block. So, if there is an error while creating the singleton instance, an appropriate exception can be thrown.</p>


</div></div>
<div class="su-spacer" style="height:5px"></div>


<h2 class="wp-block-heading" id="advantages-of-singleton">Advantages of Singleton</h2>


<p>There are several advantages of using a singleton class. &nbsp;It helps it saving on memory and other resources by ensuring that there is only one instance of the class. It provides global access to the singleton instance. It gives a class control over the instantiation process.</p>


<h2 class="wp-block-heading" id="disadvantages-of-singleton">Disadvantages of Singleton</h2>


<p>The Singleton pattern has some disadvantages too. For one, singleton objects are often used as global variables which is against object-oriented principles like encapsulation. Secondly, singletons make unit testing difficult. A class that uses a singleton object cannot be tested by itself.</p>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>So, in this article, we understood the basics of the singleton design pattern. We also saw some of the ways in which you can implement the singleton pattern in Java. Finally, we understood some of the advantages and disadvantages of the singleton pattern.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22511" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22511" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22511" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22511&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22511" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=22511" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=22511', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/how-to-create-singleton-class-in-java/">How To Create Singleton Class in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/how-to-create-singleton-class-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Sort List in Java</title>
		<link>https://www.automationdojos.com/sort-list-in-java/</link>
					<comments>https://www.automationdojos.com/sort-list-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Tue, 10 Mar 2020 13:17:00 +0000</pubDate>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-how-to]]></category>
		<category><![CDATA[java-list]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=20840</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/sort-list-in-java/">How To Sort List in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Introduction A List is an interface in the Java collection framework. It can be used to store objects. Programmers often encounter scenarios where they need to sort a List. There are several ways you can achieve this. In the next few sections, I will be going over each method in detail. 1. Using Collections.sort The [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/sort-list-in-java/">How To Sort List in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/sort-list-in-java/">How To Sort List in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_sort-list-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>

</li>
<li><a href="#1-using-collectionssort">1. Using Collections.sort</a>


<ul><li>
<a href="#collectionssort-without-comparator">Collections.sort without Comparator</a>

</li>
<li><a href="#collectionssort-with-comparator">Collections.sort with Comparator</a>

</li>
</ul>
<li><a href="#2-using-listsort">2. Using List.sort</a>

</li>
<li><a href="#3-using-stream-api">3. Using Stream API</a>

</li>
<li><a href="#4-sorting-a-list-of-objects">4. Sorting a List of Objects</a>

</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul>

<h2 class="wp-block-heading" id="introduction">Introduction</h2>


<p>A List is an interface in the Java collection framework. It can be used to store objects. Programmers often encounter scenarios where they need to sort a List. There are several ways you can achieve this. In the next few sections, I will be going over each method in detail.</p>


<h2 class="wp-block-heading" id="1-using-collectionssort">1. Using Collections.sort</h2>


<p>The Collection framework has the java.util.Collections class. This has a lot of utility methods that can be used to perform various operations on Collections. One of these is the Collections.sort method. There are two overloaded versions of this method as explained below.</p>



<div class="rt-block">
<h3 class="wp-block-heading" id="collectionssort-without-comparator"><strong>Collections.sort without Comparator</strong></h3>


<p>This version of the Collections.sort method accepts as parameter a List object. It sorts the List according to the natural order of the elements in the List. The following code demonstrates this:</p>


<div class="su-spacer" style="height:20px"></div>


<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
public static void usingCollectionsSort() {
List&lt;Integer> input = Arrays.asList(34,12,67,8,91,54,24);
Collections.sort(input);
System.out.print("Sorted List:");
input.forEach(num -> System.out.print(num+" "));
}</pre>


<div class="su-spacer" style="height:20px"></div>


<p>Here, the code creates a new List with some Integer values. As you can see this List is not sorted. The code then invokes the Collections.sort method. Since the natural ordering for Integers is ascending order, this sorts the List in ascending order. So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Sorted List:8 12 24 34 54 67 91 </code></pre>


<div class="su-spacer" style="height:20px"></div>

<h3 class="wp-block-heading" id="collectionssort-with-comparator"><strong>Collections.sort with Comparator</strong></h3>


<p>There is an overloaded version of the Collections.sort method. In addition to the List to be sorted, this method also accepts as parameter a Comparator. It then sorts the input List as per the specified Comparator. The following code demonstrates this:</p>


<div class="su-spacer" style="height:20px"></div>


<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
public static void usingCollectionsSortWithComparator() {
List&lt;Integer> input = Arrays.asList(34,12,67,8,91,54,24);
Collections.sort(input,(num1,num2) -> num2-num1);
System.out.print("Sorted List:");
input.forEach(num -> System.out.print(num+" "));
}
</pre>


<div class="su-spacer" style="height:20px"></div>


<p>As before, this code creates an unsorted List with some values. The Collections.sort method is invoked with the input List and a lambda expression that implements the Comparator interface. Here, the lambda expression returns the difference of subtracting the first number from the second number. So, this is equivalent to sorting the List in descending order. So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Sorted List:91 67 54 34 24 12 8</code></pre>

</div>


<h2 class="wp-block-heading" id="2-using-listsort">2. Using List.sort</h2>


<p>Java 8 has added a sort method on the List interface. This can also be used to sort a List. It accepts as parameter a Comparator instance and sorts the List based on the specified Comparator. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingListSortWithStrings() {
List&lt;String> input = Arrays.asList("Horse","Cat","Elephant","Giraffe");
	input.sort((str1,str2) -> str1.compareTo(str2));
	System.out.print("Sorted List:");
	input.forEach(num -> System.out.print(num+" "));

}
</pre>



<p>In this case, the code creates a List of String values. The List.sort method is invoked using a lambda expression that compares the Strings and returns the result of comparison. This is equivalent to sorting the List in alphabetical order. So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Sorted List:Cat Elephant Giraffe Horse</code></pre>



<p>List.sort has better performance compared to Collections.sort. This is because Collections.sort sorts the List by dumping its contents in an array. List.sort on the other hands sorts inline.</p>


<h2 class="wp-block-heading" id="3-using-stream-api">3. Using Stream API</h2>


<p>The Stream API added by Java 8 can also be used to perform several operations on Collections. The Stream interface has a method called sorted that sorts the element in the Stream as per their natural order.&nbsp; The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingStreamSort() {
	List&lt;Integer> input = Arrays.asList(34,12,67,8,91,54,24);
	Stream&lt;Integer> inputStream = input.stream();
	Stream&lt;Integer> sortedStream = inputStream.sorted();
	List&lt;Integer> output = sortedStream.collect(Collectors.toList());
	System.out.print("Sorted List:");
	output.forEach(num -> System.out.print(num+" "));
}
</pre>



<p>Here, first the stream() method is invoked on the input List. This returns a Stream corresponding to the List. Then the sorted() method is invoked on the Stream. This returns a new Stream that is sorted. Finally, the collect() method is invoked which converts the Stream to a List back. So, this code prints the same output as before:</p>



<pre class="wp-block-code"><code>Sorted List:8 12 24 34 54 67 91</code></pre>



<p>There is also an overloaded version of the sorted method that accepts as parameter a Comparator instance and sorts the input Stream as per the specified Comparator.</p>


<h2 class="wp-block-heading" id="4-sorting-a-list-of-objects">4. Sorting a List of Objects</h2>


<p>Sometimes, you may have a List of objects and you may need to sort it based on some field in the class. You can achieve this by using a Comparator and either the Collections.sort of List.sort method.</p>



<p>For example, suppose you have a Student class as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public class Student {
	
	private String name;
	private double marks;
	//constructors, getters and setters
}
</pre>



<p>And suppose you have a List of Student objects that need to be sorted based on the marks field. You can do this using the following code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">List&lt;Student> input = new ArrayList&lt;Student>();
		
input.add(new Student("Bill",76));
input.add(new Student("Jane",94));
input.add(new Student("George",68));
	
input.sort((s1,s2) -> s1.getMarks() - s2.getMarks());
System.out.print("Sorted List:");
input.forEach(s -> System.out.print(s.getName()+" "));
</pre>



<p>Here, we have used the List.sort method. The lambda expression passed to the sort method implements a Comparator and compares the marks fields. So, this code prints the following output: Sorted List:George Bill Jane</p>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>So, there are several ways in which you can sort a List in Java. You can use the Collections.sort to sort as per the natural order or using a custom Comparator. You can use the List.sort with a custom Comparator as well. You can also use the Stream API to sort a List.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20840" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20840" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20840" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20840&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20840" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=20840" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=20840', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/sort-list-in-java/">How To Sort List in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/sort-list-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Convert String To Date in Java</title>
		<link>https://www.automationdojos.com/convert-string-to-date/</link>
					<comments>https://www.automationdojos.com/convert-string-to-date/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Tue, 04 Feb 2020 11:58:00 +0000</pubDate>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-how-to]]></category>
		<category><![CDATA[java-strings]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=20831</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/convert-string-to-date/">How To Convert String To Date in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date.png 575w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Introduction There are often scenarios in programming, where you will need to convert a date in String format to an actual Date object. For example, such a scenario may occur when the date is read from an end user via a UI and sent to the back-end code. In this article, we will understand the [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/convert-string-to-date/">How To Convert String To Date in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/convert-string-to-date/">How To Convert String To Date in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date.png 575w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/02/post_howto_string-date-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>

</li>
<li><a href="#java-date-handling">Java Date Handling</a>

</li>
<li><a href="#converting-string-to-javautildate">Converting String to java.util.Date</a>

</li>
<li><a href="#converting-string-to-javautilcalendar">Converting String to java.util.Calendar</a>

</li>
<li><a href="#converting-string-to-javatimelocaldate">Converting String to java.time.LocalDate</a>


<ul><li>
<a href="#localdate-without-formatter">LocalDate without Formatter</a>

</li>
<li><a href="#localdate-with-formatter">LocalDate with formatter</a>

</li>
</ul>
<li><a href="#conclusion">Conclusion</a>
</li></ul>

<h2 class="wp-block-heading" id="introduction">Introduction</h2>


<p>There are often scenarios in programming, where you will need to convert a date in String format to an actual Date object. For example, such a scenario may occur when the date is read from an end user via a UI and sent to the back-end code. In this article, we will understand the different ways in which you can represent a date in Java and how you can convert a String date to each of these Date representations.</p>


<h2 class="wp-block-heading" id="java-date-handling">Java Date Handling</h2>


<p>Before we dive into the details of how to convert a String to a Date, let us understand how Java handles Dates.  In Java, there are three main classes that can be used to represent a date. These are as follows:</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">



<ul class="wp-block-list"><li>java.util.Date</li><li>java.util.Calendar</li><li>java.time.LocalDate</li></ul>



</div></div>



<p>The java.util.Date and java.util.Calendar classes were present right from the early versions of Java. However, these classes have several issues. They are not very user friendly and they do not allow performing date time manipulation very easily. To address all these issues, Java 8 introduced the java.time.LocalDate class as part of DateTime API. The LocalDate class allows performing date operations easily.  So, if you have a String object, you can convert it to a java.util.Date, java.util.Calender object or a java.time.LocalDate object based on your requirements.</p>


<h2 class="wp-block-heading" id="converting-string-to-javautildate">Converting String to java.util.Date</h2>


<p>Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to specify the format in which the String date is present and invoke the parse method. The following code demonstrates how this can be done:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void convertToDate() throws ParseException {
	String strDate = "2015-06-12";
	SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	Date date = dateFormatter.parse(strDate);
	System.out.println(date);
}
</pre>



<p>This code creates a SimpleDateFormat instance. Since the input date is in the <strong>yyyy-MM-dd</strong> format, the SimpleDateFormat is created using this format. The parse() method is then invoked with the actual String date. This returns a java.util.Date object corresponding to the String. So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Fri Jun 12 00:00:00 IST 2015</code></pre>



<p>You can check out the API documentation for <a href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a> in order to understand the patterns that need to be specified for various date formats.</p>


<h2 class="wp-block-heading" id="converting-string-to-javautilcalendar">Converting String to java.util.Calendar</h2>


<p>As mentioned earlier, the java.util.Calendar class also encapsulates a date. The Calendar class has some more features that the java.util.Date class. It provides the ability to extract the day, month, year and other fields corresponding to the date that the Calendar represents. So, sometimes, you may need to convert a String date to a Calendar instance. For this, you first need to convert the String date to a java.util.Date instance via the SimpleDateFormat class. You can then use this Date to create a Calendar instance. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void convertToCalendar() throws ParseException {
String strDate = "2015-06-12";
	SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	Date date = dateFormatter.parse(strDate);
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	System.out.println(date);
}
</pre>



<p>As before, this code uses a SimpleDateFormat and creates a java.util.Date instance from the String date. The Calendar.getInstance() method is invoked which creates a Calendar object corresponding to the current time. The calendar.setTime() method is then invoked with the Date object. This changes the Calendar’s time to the time in the Date object. So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Fri Jun 12 00:00:00 IST 2015</code></pre>


<h2 class="wp-block-heading" id="converting-string-to-javatimelocaldate">Converting String to java.time.LocalDate</h2>


<p>As mentioned earlier, in order to overcome the shortcomings of the Date and Calendar classes, Java 8 introduced the LocalDate class as part of the DateTime API. Compared to the java.util.Date and java.util.Calendar classes, the LocalDate class is much simpler to use.  Let us now understand how to convert a String date to a LocalDate instance.</p>



<div class="rt-block">
<h3 class="wp-block-heading" id="localdate-without-formatter">LocalDate without Formatter</h3>


<p>The LocalDate class has a parse method. You can use this to convert a String to a LocalDate. The following code demonstrates this:</p>

<div class="su-spacer" style="height:20px"></div>

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void convertToLocalDate() {
String strDate = "2015-06-12";
	LocalDate date = LocalDate.parse(strDate);
	System.out.println(date);
}
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>So, this code simply invokes the parse method with the String date object. It prints the following output: 2015-06-12</p>

<div class="su-spacer" style="height:20px"></div>

<p>The LocalDate.parse() method requires the String date to be in the <strong>yyyy-MM-dd</strong> format. If the String date is in some other format, this method throws an Exception. There is an overloaded version of the parse method that can be used for such scenarios as explained in the next section.</p>

<div class="su-spacer" style="height:20px"></div>
<h3 class="wp-block-heading" id="localdate-with-formatter">LocalDate with formatter</h3>


<p>The LocalDate class has an overloaded parse() method. In addition to the String Date, it accepts a DateTimeFormatter instance that specifies the format of the input date. So, this can be used in case the String date is not in the <strong>yyyy-MM-dd</strong> format. The following code demonstrates this:</p>

<div class="su-spacer" style="height:20px"></div>

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void convertToLocalDateWithDateFormatter() { 
String strDate = "06/12/2015";
	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
	LocalDate date = LocalDate.parse(strDate,formatter);
	System.out.println(date);
}
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>In this case, the String date is in the <strong>dd/MM/YYYY</strong> format.&nbsp; A DateTimeFormatter instance is created with this pattern. The parse() method is then invoked with the String date and the DateTimeFormatter instance. So, this code prints the following output:</p>



<p>2015-06-12</p>

<div class="su-spacer" style="height:20px"></div>

<p>You can check out the API documentation for <a href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html">DateTimeFormatter</a> in order to understand the patterns that need to be specified for various date formats.</p>

</div>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>So, in summary, you can use the java.util.Date, java.util.Calendar or the java. time.LocalDate classes to represent a date in Java. You can convert a String date to any of these methods using the methods demonstrated in this article.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20831" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20831" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20831" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20831&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20831" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=20831" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=20831', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/convert-string-to-date/">How To Convert String To Date in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/convert-string-to-date/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Concatenate Strings in Java</title>
		<link>https://www.automationdojos.com/concatenate-strings-in-java/</link>
					<comments>https://www.automationdojos.com/concatenate-strings-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sat, 04 Jan 2020 10:00:00 +0000</pubDate>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-how-to]]></category>
		<category><![CDATA[java-strings]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=20809</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/concatenate-strings-in-java/">How To Concatenate Strings in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Introduction In Java, a String is a sequence of characters. There are often programming situations where you will need to concatenate Strings. There are several ways in which you can achieve this. In the next few sections, I will be going over each method in detail. Using + Operator The + operator is also known [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/concatenate-strings-in-java/">How To Concatenate Strings in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/concatenate-strings-in-java/">How To Concatenate Strings in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_howto_concatenate-java-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>

</li>
<li><a href="#using-operator">Using + Operator</a>

</li>
<li><a href="#using-stringconcat">Using String.concat</a>

</li>
<li><a href="#using-stringbufferstringbuilder">Using StringBuffer/StringBuilder</a>


<ul><li>
<a href="#using-stringbuffer-for-strings">Using StringBuffer for Strings</a>

</li>
<li><a href="#using-stringbuffer-for-other-types-of-data">Using StringBuffer for other types of data</a>

</li>
<li><a href="#using-stringbuilder">Using StringBuilder</a>

</li>
</ul>
<li><a href="#using-stringjoin">Using String.join</a>


<ul><li>
<a href="#stringjoin-for-multiple-strings">String.join for multiple Strings</a>

</li>
<li><a href="#stringjoin-for-a-collection">String.join for a Collection</a>

</li>
</ul>
<li><a href="#using-stringjoiner">Using StringJoiner</a>

</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul>

<h2 class="wp-block-heading" id="introduction">Introduction</h2>


<p>In Java, a String is a sequence of characters. There are often programming situations where you will need to concatenate Strings. There are several ways in which you can achieve this. In the next few sections, I will be going over each method in detail.</p>


<h2 class="wp-block-heading" id="using-operator">Using + Operator</h2>


<p>The + operator is also known as the concatenation operator. It can be used to concatenate Strings. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingPlusOperator() {
String string1 = "Hello ";
	String string2 = "World";
	String result = string1+string2;
	System.out.println(result);
}</pre>



<p>This code creates two Strings called string1 and string2. It uses the + operator to concatenate them and assign the result to a separate variable. So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Hello World</code></pre>



<p>This approach can be used to concatenate more than two values. Not only that, it can be used to concatenate other types of values like primitives as shown below:</p>



<pre class="wp-block-code"><code>String result = “Hello”+” World”+” 2“+” times!”; </code></pre>



<p>So, this code prints the following output:</p>



<pre class="wp-block-code"><code>Hello World 2 times!</code></pre>



<p>The downside of this approach is that it is not very efficient. Since String is immutable, every time you use the + operator, a new String is created.</p>


<h2 class="wp-block-heading" id="using-stringconcat">Using String.concat</h2>


<p>The String class has a method called concat. It accepts as parameter a String value and concatenates it with the current String. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingStringConcat() {
	String string1 = "Hello ";
	String string2 = "World";
	String result = string1.concat(string2);
	System.out.println(result);
}
</pre>



<p>As before, this code creates string1 and string2.&nbsp; It then invokes the concat() method on string1 and passes string2 as an argument. So, this code also prints the same output as before:</p>



<pre class="wp-block-code"><code>Hello World</code></pre>



<p>Performance-wise, this approach is not much better than using the + operator. Also, unlike the + operator, this approach cannot be used for multiple Strings or other types of values.</p>


<h2 class="wp-block-heading" id="using-stringbufferstringbuilder">Using StringBuffer/StringBuilder</h2>


<p>In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings.  The advantage of these classes is that both these classes are mutable, so using them does not result in the creation of new String objects. So StringBuffer and StringBuilder have a better performance as compared to the + operator and concat method.</p>



<div class="rt-block">
<h3 class="wp-block-heading" id="using-stringbuffer-for-strings">Using StringBuffer for Strings</h3>


<p>The StringBuffer class has a method called append which can be used to concatenate Strings. It appends the value passed in with the StringBuffer on which it is invoked. The following code demonstrates this:</p>

<div class="su-spacer" style="height:20px"></div>

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingStringBuffer() {
String string1 = "Hello ";
	String string2 = "World";
	StringBuffer result = new StringBuffer();
	result.append(string1);
	result.append(string2);
	System.out.println(result);
}
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>As before, this code creates string1 and string2.&nbsp; It also creates a StringBuffer called result. It then invokes the append method with both the Strings. So, this code also prints the same output as before: Hello World</p>

<div class="su-spacer" style="height:20px"></div>
<h3 class="wp-block-heading" id="using-stringbuffer-for-other-types-of-data">Using StringBuffer for other types of data</h3>


<p>There are several overloaded versions of the append method that accept data of different types like primitive values, character arrays and Strings. These methods can be used to append different types of data as demonstrated below:</p>

<div class="su-spacer" style="height:20px"></div>

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">StringBuffer result = new StringBuffer();
result.append("Hello").append(" World ").append(2).append(" times!");
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>So, this code prints the following output: Hello World 2 times!</p>

<div class="su-spacer" style="height:20px"></div>
<h3 class="wp-block-heading" id="using-stringbuilder">Using StringBuilder</h3>


<p>The StringBuilder class is very similar to StringBuffer. It also has a method called append which can be used to concatenate Strings. The only difference is that StringBuffer is synchronised while StringBuilder is not. So StringBuilder has a slight performance advantage over StringBuffer.</p>

<div class="su-spacer" style="height:20px"></div>

<p>The following code demonstrates the StringBuilder.append method:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingStringBuilder() {
String string1 = "Hello ";
	String string2 = "World";
	StringBuilder result = new StringBuilder();
	result.append(string1);
	result.append(string2);
	System.out.println(result);
}
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>So, this code is exactly the same as the StringBuffer code and it prints the following output: Hello World</p>

</div>


<h2 class="wp-block-heading" id="using-stringjoin">Using String.join</h2>


<p>Java 8 has added a static join method to the String class. This concatenates Strings using a delimiter. Performance-wise, it is similar to StringBuffer.append, but it has the added advantage that it automatically adds a delimiter.</p>



<div class="rt-block">
<h3 class="wp-block-heading" id="stringjoin-for-multiple-strings">String.join for multiple Strings</h3>


<p>&nbsp;The String.join() method accepts as parameter a variable number of Strings that need to be concatenated as well as a delimiter to separate the Strings. The following code demonstrates this:</p>

<div class="su-spacer" style="height:20px"></div>

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingStringJoin() {

String string1 = "red";
	String string2 = "blue";
	String result = String.join(" ", string1,string2);
	System.out.println(result);

}
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>As before, this code creates string1 and string2.&nbsp; It invokes the join method with these Strings and specifies space (“ “) as a delimiter. So, this code prints the following output: red blue</p>

<div class="su-spacer" style="height:20px"></div>
<h3 class="wp-block-heading" id="stringjoin-for-a-collection">String.join for a Collection</h3>


<p>There is also an overloaded version of the String.join method that accepts as parameter an Iterable implementation. Since all Collection classes implement the Iterable interface, this method can be used to concatenate all the elements in a Collection. The following code demonstrates this:</p>

<div class="su-spacer" style="height:20px"></div>

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingStringJoinWithList() {

List&lt;String> colours = Arrays.asList("red","blue","green","white");
	String result = String.join("/", colours);
	System.out.println(result);

}
</pre>

<div class="su-spacer" style="height:20px"></div>

<p>This code creates a List of String values and passes this List to the String.join method with a “/” as the delimiter. So, this code prints the following output: red/blue/green/white</p>

</div>


<h2 class="wp-block-heading" id="using-stringjoiner">Using StringJoiner</h2>


<p>Java 8 has added a new class called StringJoiner. This concatenates Strings using a delimiter as well as adds a prefix and suffix to the concatenated String. Performance wise, it is similar to StringBuffer.append, but it has the added advantage that it automatically adds a delimiter, prefix and suffix.</p>



<p>The following code demonstrates how the StringJoiner class can be used to concatenate Strings:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private static void usingStringJoiner() {

	StringJoiner strJoiner = new StringJoiner(",","{","}");	strJoiner.add("red");
	strJoiner.add("blue");
	strJoiner.add("green");
	System.out.println(strJoiner);

}
</pre>



<p>This code creates a StringJoiner. It specifies that a comma symbol (,) should be used as the delimiter. It also specifies that the “{“should be used as the prefix and “}” should be used as the suffix. It then invokes the add method with some String values. So, this concatenates the specified Strings using the specified delimiter, prefix and suffix. So, this code prints the following output: {red,blue,green}</p>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>So, as seen in this article, there are several ways in which you can concatenate Strings in Java. The approach that you need to choose depends on your requirements and performance consideration. If you want to simply concatenate Strings without a delimiter, using a StringBuffer or StringBuilder is a better approach. However, if you want to add delimiters/prefix/suffix, consider going for the String.join or StringJoiner method. &nbsp;</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20809" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20809" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20809" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20809&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20809" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=20809" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=20809', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/concatenate-strings-in-java/">How To Concatenate Strings in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/concatenate-strings-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Remove Duplicates From List in Java</title>
		<link>https://www.automationdojos.com/remove-duplicates-from-list-in-java/</link>
					<comments>https://www.automationdojos.com/remove-duplicates-from-list-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sat, 24 Nov 2018 00:49:00 +0000</pubDate>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-how-to]]></category>
		<category><![CDATA[java-list]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=13266</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/remove-duplicates-from-list-in-java/">How To Remove Duplicates From List in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<p>Introduction A List is an interface in the Java collection framework which can be used to store a group of objects. A List is ordered and it allows duplicates. However, there are often scenarios where you would need to eliminate duplicate elements from a List. There are several ways you can achieve this. In the [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/remove-duplicates-from-list-in-java/">How To Remove Duplicates From List in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/remove-duplicates-from-list-in-java/">How To Remove Duplicates From List in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_howto-remove-duplicates001.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>

</li>
<li><a href="#1-using-a-for-loop">1) Using a For Loop</a>

</li>
<li><a href="#2-using-a-set">2) Using a Set</a>


<ul><li>
<a href="#a-using-a-hashset">(a) Using a HashSet</a>

</li>
<li><a href="#b-using-a-linkedhashset">(b) Using a LinkedHashSet</a>

</li>
<li><a href="#c-using-setaddall">(c) Using Set.addAll</a>

</li>
</ul>
<li><a href="#3-using-stream-api">3) Using Stream API</a>
</li></ul>

<h2 class="wp-block-heading" id="introduction">Introduction</h2>


<p>A List is an <strong><em>interface</em></strong> in the Java <strong><em>collection framework</em></strong> which can be used to store a group of objects. A List is ordered and it allows duplicates. However, there are often scenarios where you would need to eliminate duplicate elements from a List.</p>



<p>There are several ways you can achieve this. In the next few sections, I will be going over each method in detail.</p>


<h2 class="wp-block-heading" id="1-using-a-for-loop">1) Using a For Loop</h2>


<p>The simplest way to remove duplicates from a List is to use a for loop. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingForLoop() {
List&lt;Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25);
List&lt;Integer> output = new ArrayList&lt;Integer>();
for(Integer num:input){
if(!output.contains(num)){
  		output.add(num);
  		}
}
for(Integer num:output){
  		System.out.print(num+" ");
 	}
}</pre>



<p>First, the code creates a new ArrayList to store the output, i.e. a List without duplicates. A for loop is then used which iterates through the input List. It first checks if the input element is present in the output List and if not, it adds it to the List. Finally, another for loop is used to print each element in the output list. So this code prints the following <strong>output: 5 10 15 20 35 40 25</strong></p>


<h2 class="wp-block-heading" id="2-using-a-set">2) Using a Set</h2>


<p>Set is also an <strong><em>interface</em></strong> in the <strong><em>Java Collection Framework</em></strong>. Unlike a List, a Set <strong><em>does not allow duplicates</em></strong>. So you can use a Set to eliminate the duplicates in a List. There are several ways in which you can use a Set to eliminate duplicates as demonstrated below:</p>


<h3 class="wp-block-heading" id="a-using-a-hashset">(a) Using a HashSet</h3>


<p>The most commonly used implementation of Set is HashSet. HashSet has a constructor which accepts as parameter a Collection. So basically, if you create a new HashSet using this constructor and pass in the List as input, this will eliminate the duplicates. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingHashSet() {
List&lt;Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25);
Set&lt;Integer> output = new HashSet&lt;Integer>(input);
for(Integer num:output){
System.out.print(num+" ");
}
}</pre>



<p>Here, a new HashSet is created by passing the List as input. So this will automatically create a Set with the elements in the List and eliminate the duplicates. So this code prints the same <strong>output as before: 35 20 5 40 25 10 15</strong></p>



<p>The problem with HashSet is that it does not maintain the order of elements. So you can see that the order of elements in the output is different from that in the input list.</p>


<h3 class="wp-block-heading" id="b-using-a-linkedhashset">(b) Using a LinkedHashSet</h3>


<p>Another implementation of the Set interface is LinkedHashSet. LinkedHashSet <strong><em>maintains the order</em></strong> of elements and helps to overcome the HashSet limitation. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingLinkedHashSet() {
List&lt;Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25);
   Set&lt;Integer> output = new LinkedHashSet&lt;Integer>(input);
   for(Integer num:output){
      System.out.print(num+" ");
   }
}</pre>



<p>This code is quite similar to the one seen earlier, except that it uses a LinkedHashSet. The input List is passed as a parameter to the LinkedHashSet constructor. Since a LinkedHasSet is used, the elements will be ordered in this case as can be seen from the <strong>output below: 5 10 15 20 35 40 25</strong></p>


<h3 class="wp-block-heading" id="c-using-setaddall">(c) Using Set.addAll</h3>


<p>The Set interface has a method called addAll. This accepts as parameter a Collection. So if you invoke this method by passing the List, the duplicates will be eliminated. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingSetAddAll() {
List&lt;Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25);
Set&lt;Integer> output = new LinkedHashSet&lt;Integer>();
output.addAll(input);
for(Integer num:output){
System.out.print(num+" ");
}
}</pre>



<p>Here, a new LinkedHashSet is created and the addAll method is invoked by passing the List as input. So this will automatically create a Set with the elements in the List and eliminate the duplicates. So this code prints the same <strong>output as before: 5 10 15 20 35 40 25</strong></p>


<h2 class="wp-block-heading" id="3-using-stream-api">3) Using Stream API</h2>


<p>Java 8 has added the Stream API that helps to easily perform <strong><em>bulk operations on Collections</em></strong>. A new method called stream() method has been added to all the collection interfaces that returns a Stream corresponding to the underlying collection.</p>



<p>The Stream interface has a <strong><em>method called distinct</em></strong> that can be used to eliminate duplicates. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void usingStream() {
List&lt;Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25);
Stream&lt;Integer> streamWithDuplicates = input.stream();
Stream&lt;Integer> streamWithoutDuplicates = streamWithDuplicates.distinct();
List&lt;Integer> output = streamWithoutDuplicates.collect(Collectors.toList());
   for(Integer num:output){
      System.out.print(num+" ");
   }
}</pre>



<p>&nbsp;<br>Here, first the stream() method is invoked on the input List. This returns a Stream corresponding to the List. Then the distinct() method is invoked on the Stream. This returns a new Stream that has only the unique elements from the input List.</p>



<p>Finally, the <strong><em>collect() method</em></strong> is invoked which converts the Stream to a List back. So this code prints the following <strong>output: 5 10 15 20 35 40 25</strong></p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13266" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13266" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13266" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13266&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13266" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=13266" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=13266', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/remove-duplicates-from-list-in-java/">How To Remove Duplicates From List in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/remove-duplicates-from-list-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Convert List To Array in Java</title>
		<link>https://www.automationdojos.com/how-to-convert-list-to-array/</link>
					<comments>https://www.automationdojos.com/how-to-convert-list-to-array/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Thu, 05 Jul 2018 14:56:13 +0000</pubDate>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[How-To Java]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-array]]></category>
		<category><![CDATA[java-how-to]]></category>
		<category><![CDATA[java-list]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=13158</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/how-to-convert-list-to-array/">How To Convert List To Array in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<p>A common scenario faced by programmers is to convert a Java List to an Array. A &#8216;List&#8217; is part of the Collection Framework and is useful when the number of elements are unknown. An &#8216;Array&#8217;, on the other hand, is useful for fixed-size elements. However, in real-world programming, there are often situations when you would [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/how-to-convert-list-to-array/">How To Convert List To Array in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/how-to-convert-list-to-array/">How To Convert List To Array in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-howto-list2array01.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#1-using-a-for-loop">1. Using a &#8216;For Loop&#8217;</a>

</li>
<li><a href="#2-using-toarray">2. Using &#8216;toArray&#8217;</a>


<ul><li>
<a href="#21-listtoarray-without-parameters">(2.1) List.toArray without parameters</a>

</li>
<li><a href="#22-listtoarray-with-parameters">(2.2) List.toArray with parameters</a>

</li>
</ul>
<li><a href="#3-using-stream-api">3. Using Stream API</a>


<ul><li>
<a href="#31-streamtoarray-without-parameters">(3.1) stream.toArray without parameters</a>

</li>
<li><a href="#32-streamtoarray-with-parameters">(3.2) stream.toArray with parameters</a>

</li>
</ul>
<li><a href="#conclusion">Conclusion</a>
</li></ul>


<p>A common scenario faced by programmers is to convert a Java List to an Array. A &#8216;List&#8217; is part of the <strong>Collection Framework</strong> and is useful when the number of elements are unknown. An &#8216;Array&#8217;, on the other hand, is useful for fixed-size elements.</p>



<p>However, in real-world programming, there are often situations when you would need to convert a List to an array like for example while dealing with methods that expect an array as input. There are several ways that you can achieve this. In the next few sections, I will be going over each method in detail.</p>


<h2 class="wp-block-heading" id="1-using-a-for-loop">1. Using a &#8216;For Loop&#8217;</h2>


<p>The simplest way to convert a List to an array is to use a for loop.&nbsp; The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void convertUsingForLoop(){
List&lt;Integer> inputList = Arrays.asList(2,4,6,8,10);
int[] outputArray = new int[inputList.size()];
for(int i = 0; i &lt; inputList.size();i++){
      outputArray[i] = inputList.get(i);
   }
   //print the elements in the array
   for (int num : outputArray) {
      System.out.print(num + " ");
   }
}</pre>



<p>First, the code creates an array based on the number of elements in the input List. A for-loop is then used which iterates through the input List and adds each element of the List to an array. Finally, another for loop is used to print each element in the array. <strong><em>So this code prints the following output:</em></strong></p>



<p>2 4 6 8 10</p>


<h2 class="wp-block-heading" id="2-using-toarray">2. Using &#8216;toArray&#8217;</h2>


<p>Java provides a toArray method on the List interface. This can be used to convert a List to an array. There are <strong><em>two overloaded versions</em></strong> of this method as explained below.</p>


<h3 class="wp-block-heading" id="21-listtoarray-without-parameters">(2.1) List.toArray without parameters</h3>


<p>This toArray method does not accept any input parameters. It returns an object array. The following code demonstrates this approach:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void convertUsingToArray() {
List&lt;Integer> inputList = Arrays.asList(2, 4, 6, 8, 10);
   Object[] objArray = inputList.toArray();

//print the elements in the array
   for (Object num : objArray) {
      System.out.print(num + " ");
   }
}</pre>



<p>Here, the toArray method is invoked on the input List which returns an object array. So this code prints the same <strong><em>output as before:<br>2 4 6 8 10</em></strong></p>


<h3 class="wp-block-heading" id="22-listtoarray-with-parameters">(2.2) List.toArray with parameters</h3>


<p>There is an overloaded version of the toArray method. This accepts an array as a parameter and returns a result array which is of the same data type as the input array.</p>



<p>So basically, this toArray method can be used to obtain an array which is of the same data type as the input List instead of an object array. The following code demonstrates this approach:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void convertUsingToArray2() {

List&lt;Integer> inputList = Arrays.asList(2, 4, 6, 8, 10);
   Integer[] outputArray = inputList.toArray(new Integer[inputList.size()]);
   
//print the elements in the array
   for (int num : outputArray) {
      System.out.print(num + " ");
   }
}</pre>



<p>So in this case, the toArray method is invoked with an Integer array which is of the same size as the input List. It then returns an Integer array. So this code prints the<strong> <em>same output as before:<br>2 4 6 8 10</em></strong></p>


<h2 class="wp-block-heading" id="3-using-stream-api">3. Using Stream API</h2>


<p>Java 8 has added the Stream API that helps to easily perform bulk operations on Collections. A new method called stream() has been added to all the collection interfaces that returns a Stream corresponding to the underlying collection.</p>



<p>There is a toArray method available on the Stream interface that can be used to convert the Stream to an array. So this method can be used to convert a List to an array. There are <strong><em>two overloaded versions</em></strong> of this method as explained below.</p>


<h3 class="wp-block-heading" id="31-streamtoarray-without-parameters">(3.1) stream.toArray without parameters</h3>


<p>This Stream.toArray method does not accept any input parameter and it returns an object array. The following code demonstrates this approach:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">This Stream.toArray method does not accept any input parameter and it returns an object array. The following code demonstrates this approach:
public static void convertUsingStream() {
List&lt;Integer> inputList = Arrays.asList(2, 4, 6, 8, 10);
   Stream&lt;Integer> inputStream = inputList.stream();
   Object[] objArray = inputStream.toArray();

//print the elements in the array
   for (Object num : objArray) {
      System.out.print(num + " ");
   }
}</pre>



<p>Here, first the stream() method is invoked on the input List. This returns a Stream corresponding to the List. Then the toArray method is invoked on the Stream. This returns an object array. So this code prints the <strong><em>same output as before:<br>2 4 6 8 10</em></strong></p>


<h3 class="wp-block-heading" id="32-streamtoarray-with-parameters">(3.2) stream.toArray with parameters</h3>


<p>There is an overloaded version of the Stream.toArray method which can be used to return a result array which is of the same data type as the input array. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public static void convertUsingStream2() {
List&lt;Integer> inputList = Arrays.asList(2, 4, 6, 8, 10);
Stream&lt;Integer> inputStream = inputList.stream();
   Integer[] intArray = inputStream.toArray((num) -> new Integer[num]);
   
//print the elements in the array
   for (int num : intArray) {
      System.out.print(num + " ");
   }
}</pre>



<p>As before, first the stream() method is invoked on the input List which returns a Stream corresponding to the List. Then the overloaded toArray method is invoked. This method accepts an <strong><em>IntFunction interface</em></strong> as a parameter which is an in-built functional interface that accepts an input of integer data type and returns a result of any data type.</p>



<p>Here, it is <strong><em>implemented via a lambda expression</em></strong> that accepts as input the length of the array and returns an Integer array of the specified length. So this is then used by the Stream.toArray method to create the output array. So this code prints the <strong><em>same output as before:<br>2 4 6 8 10</em></strong></p>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>So this article demonstrates the different ways in which a List can be converted to an array in Java.</p>


<div class="su-spacer" style="height:10px"></div>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13158" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=How-To%20Java&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13158" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13158" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/category/all-how-to/how-to-java/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13158&title=How-To%20Java" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=How-To%20Java https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13158" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=13158" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=13158', 'How-To%20Java', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/how-to-convert-list-to-array/">How To Convert List To Array in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/how-to-convert-list-to-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
