How-to articles, tricks, and solutions about JAVA

How to append text to an existing file in Java?

To append text to the end of an existing file in Java, you can use the Files.write() method from the java.nio.file package. This method allows you to write a sequence of characters to a file in a single operation.

Convert list to array in Java

To convert a List to an array in Java, you can use the toArray() method of the List interface. This method returns an array containing all of the elements in the list in the proper order.

Ways to iterate over a list in Java

There are several ways to iterate over a List in Java. Here are some of the most common approaches:

How to convert comma-separated String to List?

To convert a comma-separated String to a List in Java, you can use the String.split() method to split the string into an array of substrings, and then use the Arrays.asList() method to create a list from that array.

How to check certificate name and alias in keystore files?

To check the certificate name and alias in a keystore file, you can use the keytool utility that comes with the Java Development Kit (JDK).

Java int to String - Integer.toString(i) vs new Integer(i).toString()

In Java, there are several ways to convert an int value to a String:

What is "String args[]"? parameter in main method Java

String args[] is a parameter in the main method of a Java program. It is an array of strings that can be used to pass command-line arguments to the program when it is executed.

Convert java.util.Date to java.time.LocalDate

To convert a java.util.Date object to a java.time.LocalDate object, you can use the java.time.Instant class to represent the date as an instant in time, and then use the java.time.LocalDateTime class to convert the instant to a date and time in the local

How do I find where JDK is installed on my windows machine?

To find where the JDK is installed on your Windows machine, you can follow these steps:<br>

Java List.contains(Object with field value equal to x)

To check if a Java List contains an object with a specific field value, you can use the List.stream().anyMatch() method along with a lambda expression to filter the elements in the list based on the field value.

Unable to find valid certification path to requested target - error even after cert imported

If you are getting the "unable to find valid certification path to requested target" error even after importing the certificate, there are a few possible causes and solutions:<br>

How can I check if a single character appears in a string?

To check if a single character appears in a string in Java, you can use the indexOf method of the String class.

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

This error is usually encountered when trying to parse a JSON string that does not start with a JSON array, but rather a JSON object.

How to set JAVA_HOME in Linux for all users

To set the JAVA_HOME environment variable in Linux for all users, you will need to add a line to the /etc/environment file.

round up to 2 decimal places in java?

To round a number up to two decimal places in Java, you can use the Math.ceil method and multiply the number by 100, then divide the result by 100.

How to print color in console using System.out.println?

To print colored text to the console using System.out.println, you will need to use special escape sequences in your string to specify the desired color.

Convert character to ASCII numeric value in java

To convert a character to its ASCII numeric value in Java, you can use the charAt method of the String class and the intValue method of the Character class.

When to use LinkedList over ArrayList in Java?

In Java, both ArrayList and LinkedList are implementations of the List interface that allow you to store a collection of objects in a specific order. However, they are implemented differently and have different performance characteristics.

Downloading a file from spring controllers

To download a file from a Spring controller, you can use the ResponseEntity class along with the InputStreamResource class.

How can I concatenate two arrays in Java?

To concatenate two arrays in Java, you can use the System.arraycopy method. Here's an example of how you can do this:

Converting String to "Character" array in Java

To convert a string to a character array in Java, you can use the toCharArray method of the String class. This method returns a new character array that represents the same sequence of characters as the string.

Sort ArrayList of custom Objects by property

To sort an ArrayList of custom objects by a property, you can use the Collections.sort method and pass it a custom Comparator.

How to install Java 8 on Mac

To install Java 8 on macOS, follow these steps:

Warning about SSL connection when connecting to MySQL database

If you are getting a warning about an SSL connection when connecting to a MySQL database, it may be because you are using an older version of the MySQL client library that does not support SSL connections by default.

How can I turn a List of Lists into a List in Java 8?

You can use the flatMap method from the Stream API to turn a List&lt;List&lt;T&gt;&gt; (a list of lists) into a List&lt;T&gt; in Java 8. Here's an example of how you can do this: