Object Oriented Programming Articles

Page 27 of 588

Convert JSON to/from Map using Jackson library in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 12-May-2025 3K+ Views

JSON is a format that is used to exchange data between a server and a client. It is lightweight and easy to read and write. In Java, we can convert JSON to/from Map using various libraries. In this article, we will discuss how to convert JSON to/from Map using the Jackson library. The Jackson is a library for Java, and it has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can convert JSON to/from Map using readValue() and writeValueAsString() methods of com.fasterxml.jackson.databind.ObjectMapper class. To ...

Read More

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

Aishwarya Naglot
Aishwarya Naglot
Updated on 12-May-2025 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

How to solve an IllegalArgumentException in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 12-May-2025 30K+ Views

In Java, an IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. An illegal argument is one that does not meet the required input from user. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method's or a constructor's throws clause. Reasons for java.lang.IllegalArgumentException Some of the reasons for IllegalArgumentException in Java is as follows: ...

Read More

Importance of a JSONTokener in Java?\\n

Aishwarya Naglot
Aishwarya Naglot
Updated on 12-May-2025 2K+ Views

If you have ever worked with JSON in Java, you might have used classes like JSONObject, JSONArray, JsonParser, etc. These classes are part of the org.json library. We use these classes to build JSON or parse JSON. But behind the scenes, these classes use a class called JSONTokener. In this article, we will learn about the JSONTokener class and its importance in Java. What is JSONTokener? The JSONTokener class is like a tool that reads a JSON string step by step, breaking it into smaller parts called tokens. It is like a helper that makes it easier to understand and ...

Read More

How to implement custom JSON de-serialization with Gson in Java?\\n

Aishwarya Naglot
Aishwarya Naglot
Updated on 12-May-2025 760 Views

Deserialization is the process of converting JSON data back into Java objects. Gson provides a simple way to do this using the fromJson() method. Custom JSON de-serialization with Gson Gson is a Java library developed by Google to convert Java objects into their JSON format and vice versa. Custom JSON is a way we can modify or extend the standard JSON format so that it can suit our specific needs. To use the Gson library, we need to add the Gson library to our project. If you are using Maven, add this to your pom.xml file: com.google.code.gson gson 2.8.9 ...

Read More

Java Stream findAny() Method with Examples

Aishwarya Naglot
Aishwarya Naglot
Updated on 09-May-2025 2K+ Views

In this article, we will learn the findAny() method with examples in Java. The findAny() method in Java Streams is a tool that is used for fetching an arbitrary element from a stream. It provides a quick and easy way to retrieve any element without writing any conditions. This method returns a container that may or may not contain a non-null value, which is called an Optional object. What is the findAny() Method? The findAny() method retrieves any element from the stream, and the result is wrapped in an Optional object. If the stream is empty, the Optional object will ...

Read More

How to select one item at a time from JCheckBox in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 08-May-2025 2K+ Views

In this article, we will learn to select one item at a time from a JCheckBox in Java. When creating Java Swing applications, you might have situations where you need checkboxes to behave like radio buttons, such that a box can be checked singly at any given time. JCheckBox A JCheckBox can extend JToggleButton, and it can be a small box that is either checked or unchecked. When we click on a JCheckBox, it changes from checked to unchecked or vice versa automatically. Syntax The following is the syntax for JCheckBox initialization: JCheckBox checkBox = new JCheckBox("Option"); A JCheckBox can ...

Read More

How can we set the background color to a JPanel in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 08-May-2025 16K+ Views

In this article, we will learn to set the background color of a JPanel in Java. When designing GUIs in Swing, changing the background color of panels is important for creating visually appealing applications. What is a JPanel? A JPanel is a container, and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components, like buttons, text fields, labels, tables, lists, trees, etc., into a JPanel. Syntax The following is the syntax for JPanel initialization: JPanel panel = new JPanel(); Methods The important methods of JPanel are: ...

Read More

How can we make JTextField accept only numbers in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 08-May-2025 15K+ Views

In this article, we will learn to make a JTextField accept only numbers in Java. By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int. Different Approaches The following are the two different approaches for making a JTextField accept only numbers in Java: Using a KeyListener Using DocumentFilter Using a KeyListener The KeyListener interface handles keyboard events, making it straightforward to ...

Read More

Java streams counting() method with examples

Aishwarya Naglot
Aishwarya Naglot
Updated on 08-May-2025 613 Views

In this article, we will learn how to count the number of elements in a stream using the counting() method in Java Streams. Java Streams provide an efficient way to process data collections, and the Collectors.counting() method is useful for counting the number of elements in a stream. The counting() method is a static method of the Collectors class, which is part of the java.util.stream package. It returns a long value representing the number of elements in the stream. Counting the Number of Elements in the Stream The following are the steps to count the number of elements in the ...

Read More
Showing 261–270 of 5,878 articles
« Prev 1 25 26 27 28 29 588 Next »
Advertisements