Found 7442 Articles for Java

How to serialize a null field using Gson library in Java?

Aishwarya Naglot
Updated on 13-May-2025 16:41:06

8K+ Views

Gson is a library in the Java that is mainly used for converting Java objects to JSON and vice versa. By default, the Gson object does not serialize the fields with null values to JSON. If a field in a Java object is null, Gson excludes it.Serializing a Null Field Using Gson We can force Gson to serialize null values via the GsonBuilder class. We need to call the serializeNulls() method on the GsonBuilder instance before creating the Gson object. Once serializeNulls() has been called, the Gson instance created by the GsonBuilder can include null fields in the serialized JSON. ... Read More

Convert an Iterable to Stream in Java

Aishwarya Naglot
Updated on 12-May-2025 12:56:05

327 Views

What is an Iterable?Iterable is an Interface that is used for iterating over a Collection of objects. It is part of the Java Collections Framework. The Iterable interface is implemented by all the collection classes in Java. For example, ArrayList, HashSet, etc. The Iterable interface has a method named iterator() which returns an Iterator object.What is a Stream? Stream is an Interface that is used for processing sequences of elements. It is part of the Java Collections Framework. The Stream interface is a newly added abstraction in Java 8. It is mainly used for sequential operations on collections, for example, ... Read More

Convert an Iterable to Collection in Java

Aishwarya Naglot
Updated on 12-May-2025 12:53:31

341 Views

In Java, we can store objects within other objects. A collection is an object that stores other objects. Some examples of collections are ArrayList, HashSet. Iterable is an Interface that is used for iterating over a Collection of objects. It is part of the Java Collections Framework. The Iterable interface is implemented by all the collection classes in Java. The Iterable interface has a method named iterator() which returns an Iterator object. In this article, we will learn how to convert an Iterable to a Collection in Java using multiple ways. Those are - Using for-each ... Read More

Convert String to Double in Java

Aishwarya Naglot
Updated on 29-May-2025 19:21:03

541 Views

In this article, we will learn to convert a string to a double in Java.  Problem Statement Given a string representing a decimal number, convert it into a double in Java. The input string is guaranteed to be a valid numeric representation. Input: String str = "23.6"; Output: 23  Converting String to Double in Java The following are the different approaches for converting a string to a double in Java - Using Double.parseDouble() Using Double.valueOf() Using String to Double Constructor Using DecimalFormat ... Read More

Convert String to Date in Java

Alshifa Hasnain
Updated on 21-Feb-2025 17:32:24

831 Views

In this article, we will learn to convert string to date in Java. In Java, dates are provided as strings, and it becomes necessary to convert these strings into Date objects for further processing. Problem Statement The goal is to convert the date strings in various formats into Date or LocalDateTime objects. Input − String date1 ="11/10/2018"; String date2 = "15-Mar-2019 21:11:35"; Output − Thu Oct 11 00:00:00 UTC 2018 Fri Mar 15 21:11:35 UTC 2019 Different Approaches The following are the two different approaches to converting string to date in Java − Using SimpleDateFormat ... Read More

Convert a String to a List of Characters in Java

Aishwarya Naglot
Updated on 12-May-2025 12:41:14

1K+ Views

In this article, we will learn how to convert a string to a list of characters in Java. Sometimes, when we handle character-based data structures, we need to convert a string into a list of characters. In Java, a string is a sequence of characters and is represented by the String class. A list is a collection of elements and is represented by the List interface. Converting a String to a List of Characters The following are the different approaches to convert a string to a list of characters in Java - Using String.toCharArray() ... Read More

Try, catch, throw and throws in Java

Aishwarya Naglot
Updated on 12-May-2025 12:36:18

2K+ Views

Try-catch, throw, and throws are keywords that are used for handling exceptions in Java. Exceptions are the problems that occur during the execution of a program. When a problem occurs, the program you are trying to run will terminate abnormally. So, to handle these exceptions, Java has a mechanism called exception handling. We handle different types of exceptions, such as runtime exceptions, compile-time exceptions, using try-catch, throw, and throws keywords. Types of Exceptions There are two types of exceptions in Java: Checked Exceptions: These are the exceptions that are checked at compile time. For example, ClassNotFountException, ... Read More

How to configure Gson to enable versioning support in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:35:43

165 Views

Enabling version support means that the Gson library will be able to handle multiple versions of the same class. We use this when we have a class that has been modified and we want to be able to deserialize JSON data that was created using an older version of the class. Enabling Versioning Support in Java Gson Library The Gson library provides a simple versioning system for the Java objects that it reads and writes, and also provides an annotation named @Since for the versioning concept @Since(versionnumber). We can create a Gson instance with versioning using the GsonBuilder().setVersion() method. If ... Read More

How to format a date using the Gson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 12:22:07

7K+ Views

Gson is a JSON for Java, which is developed by Google. We can format a date using the Gson library in Java. The Gson library provides a GsonBuilder class that allows us to create a Gson instance with custom settings. We call the create() method of the GsonBuilder class to create an instance of the Gson class. Then we use the method setDateFormat() to configure Gson to serialize Date objects according to the pattern provided. In order to use the Gson library, we need to add it to our project. If you are using Maven, add this to your ... Read More

Pretty print JSON using org.json library in Java?

Aishwarya Naglot
Updated on 12-May-2025 11:06:55

11K+ Views

Pretty Printing JSON using "org.json" Library Pretty printing something means formatting it in a way that is easy to read, understand, and also looks good. Here we will format JSON data in a good-looking as well as readable way. We will be using the org.json library to pretty print JSON data in Java. The method toString(int indentFactor) of the JSONObject class is used to pretty print JSON data. The indentFactor parameter specifies the number of spaces to add for each level of indentation. To use the org.json library, we need to add it to our project. If you are using ... Read More

Advertisements