<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emmanuel Ogbinaka</title>
    <description>The latest articles on DEV Community by Emmanuel Ogbinaka (@imanuel).</description>
    <link>https://dev.to/imanuel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F328829%2F650a4a04-6975-4bd9-af57-027c6238128b.jpg</url>
      <title>DEV Community: Emmanuel Ogbinaka</title>
      <link>https://dev.to/imanuel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imanuel"/>
    <language>en</language>
    <item>
      <title>The Java `.boxed()` Method</title>
      <dc:creator>Emmanuel Ogbinaka</dc:creator>
      <pubDate>Sun, 19 Jun 2022 14:56:16 +0000</pubDate>
      <link>https://dev.to/imanuel/the-java-boxed-method-571n</link>
      <guid>https://dev.to/imanuel/the-java-boxed-method-571n</guid>
      <description>&lt;p&gt;In this post I'll be talking about the &lt;code&gt;boxed()&lt;/code&gt; method of Java streams.  &lt;/p&gt;

&lt;h4&gt;
  
  
  What is the &lt;code&gt;boxed()&lt;/code&gt; method?
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;The method boxed() is designed only for streams of some primitive types (IntStream, DoubleStream, and LongStream) to box each primitive value of the stream into the corresponding wrapper class (Integer, Double, and Long respectively).&lt;br&gt;
[source][&lt;a href="https://stackoverflow.com/a/47126809" rel="noopener noreferrer"&gt;https://stackoverflow.com/a/47126809&lt;/a&gt;]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;what this means is that whenever we use any of the primitive generators the generated values are corresponding primitives to the stream. that is :&lt;/p&gt;

&lt;p&gt;IntStream - int&lt;br&gt;
LongStream - long&lt;br&gt;
DoubleStream - double&lt;/p&gt;

&lt;p&gt;Although, we can perform intermediary operations on the generated values but we cannot perform some terminal operations on the generated values. &lt;/p&gt;

&lt;p&gt;for example, &lt;br&gt;
Generate a list of numbers from 1 to 100.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Integer&amp;gt; numbers = IntStream.rangeClosed(1 , 100).collect(Collectors.toList());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above operation will throw a compile time exception of &lt;br&gt;
_ java.util.function.BiConsumer )' in 'java.util.stream.IntStream' cannot be applied to '(java.util.stream.Collector ?&amp;gt;,java.util.List &amp;gt;)'_&lt;/p&gt;

&lt;p&gt;This is where the &lt;em&gt;&lt;strong&gt;.boxed()&lt;/strong&gt;&lt;/em&gt; method comes in. &lt;br&gt;
Notice the generic &lt;em&gt;List&lt;/em&gt;  uses the wrapper class for the primitive &lt;em&gt;int&lt;/em&gt; type. What the .boxed() method does is  that it returns a Stream consisting of the elements of the given stream, each boxed to an object of the corresponding wrapper class.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Or simply put, the .boxed() method puts each value in the stream in its corresponding wrapper class.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;IntStream.boxed() - Integer&lt;br&gt;
LongStream.boxed() - Long&lt;br&gt;
DoubleStream.boxed() - Double&lt;/p&gt;

&lt;p&gt;So a good solution to the example problem is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Integer&amp;gt; numbers = IntStream.rangeClosed(1 , 100).boxed().collect(Collectors.toList());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you find this info useful. Thanks.&lt;/p&gt;

</description>
      <category>java</category>
      <category>javastreams</category>
      <category>streams</category>
      <category>functional</category>
    </item>
    <item>
      <title>Loading Initial Data with Spring Boot</title>
      <dc:creator>Emmanuel Ogbinaka</dc:creator>
      <pubDate>Mon, 23 Aug 2021 23:08:30 +0000</pubDate>
      <link>https://dev.to/imanuel/loading-initial-data-with-spring-boot-58mi</link>
      <guid>https://dev.to/imanuel/loading-initial-data-with-spring-boot-58mi</guid>
      <description>&lt;p&gt;With SpringBoot + Hibernate we can easily manage our database. By default, all classes marked with the &lt;code&gt;@Entity&lt;/code&gt; annotation in our packages are used to create tables automatically.&lt;/p&gt;

&lt;p&gt;Well, there are times we need more fine-grained control over the database alterations according to our requirements.&lt;br&gt;
In this post I'll be sharing lights on one of those scenarios: &lt;strong&gt;Auto-populating or loading initial values into our tables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;SpringBoot has some reservations for situations as this. We make use of the &lt;code&gt;data.sql&lt;/code&gt; file in Spring to actualise this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This file should be saved in the directory:&lt;br&gt;
&lt;code&gt;src/main/resources/&lt;/code&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Hint: Same location as &lt;code&gt;application.properties&lt;/code&gt; file.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When we run the project with this file on the resources directory, Spring will pick it up and use it for populating the database.&lt;/p&gt;

&lt;p&gt;As an example, we can decide to load initial values for the &lt;code&gt;Role&lt;/code&gt; entity as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Role (name) VALUES ('USER');
INSERT INTO Role (name) VALUES ('ADMIN');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you start your application, spring will attempt to load these data into the &lt;code&gt;Role&lt;/code&gt; table which at this time does not exist which will cause the program to fail, so we need to add the following config to &lt;code&gt;application.properties&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.jpa.defer-datasource-initialization=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells spring to delay the data initialisation process. Now start your spring application, and you should be able to see the values 'USER' &amp;amp; 'ADMIN' in the Role table.&lt;/p&gt;

&lt;p&gt;Hope you find this helpful. You can leave a comment to contribute. Thanks!&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>hibernate</category>
      <category>jpa</category>
    </item>
    <item>
      <title>Auto-Reload SpringBoot in IntelliJ IDEA</title>
      <dc:creator>Emmanuel Ogbinaka</dc:creator>
      <pubDate>Tue, 10 Aug 2021 11:49:34 +0000</pubDate>
      <link>https://dev.to/imanuel/auto-reload-springboot-in-intellij-idea-1l65</link>
      <guid>https://dev.to/imanuel/auto-reload-springboot-in-intellij-idea-1l65</guid>
      <description>&lt;p&gt;Having to click on the &lt;code&gt;Re-run&lt;/code&gt; to restart your Springboot application after making changes can get tiring at some point. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note, the &lt;code&gt;spring-dev-tools&lt;/code&gt; provides this functionality out-of-the-box, but couldn't get to make it work for myself. So, in this post I'll show how I hacked my way around it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first step is to add the &lt;strong&gt;Spring-dev-tools&lt;/strong&gt; dependencies to your project pom.xml file within the &lt;strong&gt;&lt;/strong&gt; tag. &lt;br&gt;
&lt;strong&gt;NB:&lt;/strong&gt; &lt;em&gt;This is a key component for this to work&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;spring-boot-devtools&amp;lt;/artifactId&amp;gt;
    &amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
    &amp;lt;optional&amp;gt;true&amp;lt;/optional&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By this I believe your IDE (IntelliJ) is open;&lt;br&gt;
Open the IDE preferences by using the shortcut &lt;code&gt;cmd + ,&lt;/code&gt; for macs from your keyboard or from the menu bar click on &lt;code&gt;IntelliJ Menu &amp;gt;&amp;gt; Preferences&lt;/code&gt;. This will open the Preferences dialog.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnzez70m89ucuimof8tmd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnzez70m89ucuimof8tmd.png" alt="IntelliJ IDEA Preferences window" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the left pane, click on the &lt;strong&gt;Build, Execution, Deployment&lt;/strong&gt; option to reveal it's sub-menus. Click on &lt;strong&gt;Compiler&lt;/strong&gt; and on the right check the &lt;strong&gt;Build Project Automatically&lt;/strong&gt; option and click ok;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnzez70m89ucuimof8tmd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnzez70m89ucuimof8tmd.png" alt="IntelliJ IDEA Preferences window" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Double tap on the Shift key on your keyboard to open IntelliJ search dialog and type &lt;code&gt;registry&lt;/code&gt; to open the registry settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdf5krpri01d9tithhqxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdf5krpri01d9tithhqxk.png" alt="IntelliJ IDEA search dialog" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find &lt;code&gt;compiler.automake.allow.when.app.running&lt;/code&gt; option and check it true; then restart your Spring-boot application and enjoy. Now upon any change in your source code spring restarts the application. Hope you find this helpful, Cheers!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn6fq73yfqkt4iqqmxml6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn6fq73yfqkt4iqqmxml6.png" alt="IntelliJ IDEA registry dialog" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>intellij</category>
      <category>springboot</category>
      <category>springdevtools</category>
      <category>java</category>
    </item>
    <item>
      <title>How to Copy Array in Java</title>
      <dc:creator>Emmanuel Ogbinaka</dc:creator>
      <pubDate>Mon, 09 Aug 2021 16:17:35 +0000</pubDate>
      <link>https://dev.to/imanuel/how-to-copy-array-in-java-56go</link>
      <guid>https://dev.to/imanuel/how-to-copy-array-in-java-56go</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;So you have two arrays &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt;, and you need to copy the  elements of A into B. Well there are various ways you can do this in Java and in this post I'll be showing you a couple of ways you can.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Method One :  ForLoop&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This good old &lt;em&gt;for-loop&lt;/em&gt; to the rescue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] A = {1,2,4,4};
int[] B = new int[];

 for (int i = 0; i &amp;lt; A.length; i++){
      B[i] = A[i];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Method Two :  &lt;code&gt;.clone()&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The clone method of array can help you simply achieve this.&lt;/p&gt;
&lt;h5&gt;
  
  
  Usage
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] A = {1,2,4,4};
int[] B = A.clone();//the clone method copies the content of A into B;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Method Three : &lt;code&gt;System.arraycopy()&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The next option is to use System.arraycopy() method present in the &lt;code&gt;java.lang&lt;/code&gt; package. Before we get into it's usage, let's discuss it's signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void arraycopy(
    Object src, //:source array, in this case A
    int srcPos, //:the start index for copy, typically 0
    Object dest, //:destination object in this case B. 
    int destPos, //:the index to place the copied elements
    int length //:the length of the contents to be copied
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Usage
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] A = {1,2,4,4};
int[] B = new int[];

System.arraycopy(A, 0, B, 0, A.length);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Method Four : &lt;code&gt;Arrays.copyOf()&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The next option we'll be discussing is from the Arrays class present in the &lt;code&gt;java.utils&lt;/code&gt; package. Once again, let's discuss it's signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static int[] copyOf( 
    int[] original, // :source array in this case A
    int newLength // :the length of the contents to be copied
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Usage
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] A = {1,2,4,4};
int[] B = Arrays.copyOf(A, 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Method Five : &lt;code&gt;Arrays.copyOfRange()&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
So, this will be the last option we'll be discussing for this post and from the Arrays class present in the &lt;code&gt;java.utils&lt;/code&gt; package. Once again, let's see it's signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static int[] copyOfRange​(
    int[] original, // :source array in this case A
    int from,  //:the start index for copy, typically 0
    int to // the end index exclusive
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Usage
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] A = {1,2,3,4,5,6,7};
int[] B = Arrays.copyOfRange(A, 0, A.length);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>array</category>
      <category>datastructuresalgorithms</category>
      <category>copy</category>
    </item>
  </channel>
</rss>
