Posts

Showing posts from January, 2023

Java - How to create an ArrayList from an Array?

In this section, we will show you how to create an ArrayList from an Array .  Following ways can be used for converting Array to  ArrayList : By using Arrays.asList() method By using Collections.addAll() method By adding each element of the Array to ArrayList explicitly Example 1: By using Arrays.asList() method  Using asList() method from an Arrays class will convert the given Array to an ArrayList . import java.util.ArrayList ; import java.util.Arrays ; public class Main { public static void main ( String []args) { String [] strArray = { "Java" , "Kotlin" , "C" , "Go" , "Python" }; ArrayList < String > list = new ArrayList< String >( Arrays . asList ( strArray )); System . out .println( list ); } } Console Output: [Java, Kotlin, C, Go, Python] Example 2: By using Collections.addAll() method Using addAll() method from the Collections class will convert ...

Spring Boot @ConditionalOnMissingClass Annotation Example

Image
In this section we will learn about @ConditionalOnMissingClass  Annotation. The  @ConditionalOnMissingClass  can be used to register a bean only when the given class(es) cannot be found on the class-path.  For example, when providing library/framework classes to other developers, this annotation can be used to active certain beans only based on the absence of specific classes. The  @ConditionalOnMissingClass  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnMissingClass on @Bean method To illustrate the use of  @ConditionalOnMissingClass , we will  create one simple service classe,  SMSNotificationService  class. public class SMSNotificationService { public SMSNotificationService() { System.out.println( "Inside SMSNotificationService Constructor" ); } public void se...

Java - Convert negative number to positive

In this section, we will write a Java program to convert negative number to positive (absolute value ) using  Math.abs() . public class Main { public static void main ( String []args) { //Example 1 int sum = 2 + 2 + 2 + 2 + Math . abs (- 2 ); System . out .println( "Sum (absolute value) : " + sum ); //Example 2 int num1 =- 9 ; int num2 = Math . abs ( num1 ); System . out .println( num2 ); } } Console Output: Total 1 (absolute value) : 10 9 More related topics, Java - Convert comma-separated String to a List and List to a comma-separated String Java - Convert hyphen-separated String to a List and List to a hyphen-separated String Java - Convert colon-separated String to a List and List to a colon-separated String Java - How to get current timestamps Java - How to count duplicated items in a List Java – How to Convert Integer to Long Java - Program to Find Largest Element in an Array Java - How to loop an enum...

Spring Boot @ConditionalOnClass Annotation Example

Image
In this section we will learn about @ConditionalOnClass Annotation. The  @ConditionalOnClass  can be used to register a bean only when the given class(es) can be found on the class-path. We can define the fully qualified class name either via a String or via a Class<?> reference. For example, when providing library/framework classes to other developers, this annotation can be used to active certain beans only based on the existing of specific classes, e.g. driver-classes etc. The  @ConditionalOnClass  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnClass on @Bean method To illustrate the use of  @ConditionalOnClass , we will  create two simple service classes, SMSNotificationService class and  EmailNotificationService class. public class EmailNotificationService { public EmailNotificatio...