Posts

Showing posts with the label Java

Deploying a Spring Boot application on an AWS EC2 instance

Image
Deploying a Spring Boot application on an AWS EC2 instance involves several steps. Below is a comprehensive guide to get your application up and running: 1. Prepare Your Spring Boot Application 1. Build the JAR File: Use Maven or Gradle to build your Spring Boot application. mvn clean package This generates a target/*.jar file. 2.  Test Locally: Ensure the application runs locally without issues:   java -jar target/your-application.jar 2. Launch an EC2 Instance 1. Log in to AWS Management Console: Navigate to the EC2 Dashboard. 2. Launch an Instance: Select an AMI (Amazon Linux 2 or Ubuntu is recommended). Choose an instance type (t2.micro for small apps under free tier). Configure instance details (e.g., security group, VPC). 3. Configure Security Group: Open ports:           1. 22 for SSH access.           2. 8080 or the port your Spring Boot app listens on. Add rules:           1. Source: Use your ...

How to convert a List of String to a comma separated String in Java

In this section, we will show you how to  convert a List of String to a comma separated String in Java. 1. List 1.1 Using String.join() join (CharSequence delimiter, Iterable<? extends CharSequence> elements) : This method is useful when we have to join iterable elements. Some of the common Iterables are – List, Set, Queue, and Stack. import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "Java" , "Kotlin" , "Python" , "C" ); String result = String . join ( "," , list ); System . out .println( result ); } } Console Output: Java,Kotlin,Python,C 1.2 Stream Collectors.joining() joining(CharSequence delimiter) :We pass a delimiter to get back a Collector, which concatenates the elements separated by the specified delimiter. import java.util.Arrays ; import java.util.List ; import jav...

Java 8 Stream – How to sort a List with stream.sorted()

In this section, we will show you how to sort list using stream.sorted() in Java 8. 1. List 1.1 Sort a List in natural order. import java.util.Arrays ; import java.util.Comparator ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "8" , "B" , "A" , "1" , "Z" , "Y" , "2" , "b" , "d" ); List < String > sortedList1 = list .stream() .sorted( Comparator . naturalOrder ()) .collect( Collectors . toList ()); System . out .println( sortedList1 ); List < String > sortedList2 = list .stream() .sorted((o1,o2)-> o1.compareTo(o2)) .collect( Collectors . toList ()); System . out .println( sortedList2 ); List < String > sortedList3 = list .stream(). sorted()....

How to convert an int to a String in Java

In this section, we will show you how to convert an int to a String in Java. Following ways can be used for converting int to String: 1. By using  Integer.toString()  method 2. By using String.valueOf()  method  Note :  Integer.toString() method is faster than String.valueOf(). String.valueOf() method internally invokes the Integer.toString() method. public static String valueOf ( int i) { return Integer . toString (i); } Example 1: Using Integer.toString() method The java.lang.Integer.toString() is an inbuilt method in Java which is used to returns the string object of the particular Integer value. By default, the argument is converted to signed decimal (radix 10) in string format. public class Main { // Driver code public static void main ( String [] args) { int val = 9 ; String str = Integer . toString ( val ); System . out .println( "String => " + str ); } } Console Output: String => 9 Example 2: Using...