How-to articles, tricks, and solutions about JAVA

Convert JSON to Map

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

Convert JsonObject to String

To convert a JsonObject to a String in Java, you can use the JsonObject.toString method.

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.

Convert Long into Integer

To convert a Long object to an Integer object in Java, you can use the intValue() method of the Long class, which returns the value of the Long as an int.

Convert Set to List without creating new List

To convert a Set to a List in Java without creating a new List object, you can use the List constructor that takes a Collection as an argument.

Convert String array to ArrayList

To convert a String array to an ArrayList in Java, you can use the following steps:

Converting 'ArrayList<String> to 'String[]' in Java

You can use the toArray() method of the ArrayList class to convert an ArrayList of strings to a string array. Here's an example:

Converting a string to an integer on Android

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

Converting A String To Hexadecimal In Java

To convert a string to a hexadecimal string in Java, you can use the encodeHexString method of the org.apache.commons.codec.binary.Hex class.

Converting double to integer in Java

To convert a double to an int in Java, you can use the intValue() method of the Double class.

Converting double to string

In Java, you can convert a double to a string using the Double.toString() method or the String.valueOf() method.

Converting Integer to Long

To convert an Integer object to a Long object in Java, you can use the longValue() method of the Integer class, which returns the value of the Integer object as a long.

Converting ISO 8601-compliant String to java.util.Date

To convert an ISO 8601-compliant string to a java.util.Date object, you can use the java.text.SimpleDateFormat class and specify the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format, which is the ISO 8601 format for dates with a time and time zone.

Converting JSON data to Java object

To convert JSON data to a Java object, you can use the fromJson method of the Gson class.

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.

Copying files from one directory to another in Java

To copy a file from one directory to another in Java, you can use the Files.copy method from the java.nio.file package.

Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

To add external JAR files to an IntelliJ IDEA project, you can follow these steps:

could not extract ResultSet in hibernate

If you are trying to execute a query using Hibernate and are getting an error saying "could not extract ResultSet," it could be due to a few different issues:

Create a directory if it does not exist and then create the files in that directory as well

To create a directory in Java if it does not exist, you can use the java.nio.file.Files class and its createDirectory method.

Create a Maven project in Eclipse complains "Could not resolve archetype"

There could be several reasons why you are getting the error "Could not resolve archetype" when trying to create a Maven project in Eclipse.

Create instance of generic type in Java?

To create an instance of a generic type in Java, you can use the newInstance() method of the Class class, along with the Type and TypeVariable classes.

Creating an instance using the class name and calling constructor

To create an instance of a class using its class name and calling its constructor, you can use the Class.forName method and the newInstance method.

Data access object (DAO) in Java

In Java, a data access object (DAO) is a design pattern that provides an abstract interface for accessing data from a database.

Date format Mapping to JSON Jackson

You can use the @JsonFormat annotation to specify the format of date fields in a JSON payload when serializing or deserializing using Jackson.