How-to articles, tricks, and solutions about JAVA

Spring Boot - Error creating bean with name 'dataSource' defined in class path resource

The error "Error creating bean with name 'dataSource' defined in class path resource" in Spring Boot typically indicates that there is a problem with the configuration of the dataSource bean.

Converting a string to an integer on Android

To convert a string to an integer in Android, you can use the Integer.parseInt method.

How do I load a file from resource folder?

To load a file from the resource folder in a Java application, you can use the ClassLoader and getResourceAsStream method.

Getting the IP address of the current machine using Java

To get the IP address of the current machine using Java, you can use the InetAddress.getLocalHost method of the java.net package.

How to convert Milliseconds to "X mins, x seconds" in Java?

To convert milliseconds to "X mins, X seconds" in Java, you can use the TimeUnit class from the java.util.concurrent package.

How do I reverse an int array in Java?

To reverse an int array in Java, you can use a loop to swap the elements at the beginning and end of the array, and then move the pointers inward until they meet in the middle of the array.

How do I use a PriorityQueue?

A PriorityQueue is a queue data structure that pops the element with the highest priority first.

How set background drawable programmatically in Android

To set a background drawable programmatically in Android, you can use the setBackgroundDrawable method of the View class.

Convert JSON to Map

You can use the Gson library to convert a JSON string to a Map in Java.

Difference between HashMap, LinkedHashMap and TreeMap

In Java, HashMap, LinkedHashMap, and TreeMap are all implementations of the Map interface.

Is there a destructor for Java?

Java does not have a destructor like some other programming languages.

How to return 2 values from a Java method?

There are a few ways you can return multiple values from a Java method:

How to initialize an array of objects in Java

Here is an example of how you can initialize an array of objects in Java:

Using env variable in Spring Boot's application.properties

You can use environment variables in your application.properties file by using the spring.profiles.active property.

HashMap with multiple values under the same key

To store multiple values under the same key in a HashMap in Java, you can use a List or an array as the value for the key.

Regex pattern including all special characters

To create a regular expression that includes all special characters, you can use the following pattern:

Split Java String by New Line

To split a string in Java by new line, you can use the split method of the String class and pass it the regular expression "\n" or "\r\n", depending on the platform you are running on.

Using two values for one switch case statement

To use two values in a switch case statement in Java, you can use the case label for each value, or you can use the case label with a range of values.

How to use Comparator in Java to sort

To use a Comparator in Java to sort a list of objects, you can use the Collections.sort method and pass it a List and a Comparator.

Easiest way to convert a List to a Set in Java

To convert a List to a Set in Java, you can use the new HashSet<>(list) constructor to create a new HashSet and initialize it with the elements of the List.

java.util.Date to XMLGregorianCalendar

To convert a java.util.Date object to an XMLGregorianCalendar in Java, you can use the toGregorianCalendar method of the DatatypeFactory class.

How can I clear or empty a StringBuilder?

To clear or empty a StringBuilder in Java, you can use the setLength method and set the length to 0.

What is the difference between Set and List?

In Java, a Set is an interface that extends the Collection interface.

How to programmatically close a JFrame

To programmatically close a JFrame in Java, you can use the dispose method.