Java Articles

Page 148 of 450

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

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

C/C++ Pointers vs Java references\\n

Nishu Kumari
Nishu Kumari
Updated on 09-May-2025 2K+ Views

In this article, we will show you the difference between C/C++ pointers and Java references. C/C++ use pointers to manually control how memory is used and accessed. Java, on the other hand, does not support pointers and uses references instead, which manage memory automatically. Pointer in C/C++ A pointer is a variable that holds the address of another variable in memory. It gives you direct access to that memory, which is powerful but can lead to errors if not used carefully. Syntax Here's the syntax where we declare a pointer with an asterisk(*) ...

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

How to print the maximum occurred character of a string in Java?

Vivek Verma
Vivek Verma
Updated on 08-May-2025 893 Views

A String class can be used to represent character strings; all the string literals in Java programs are implemented as instances of the String Class. In Java, Strings are constants and their values cannot be changed (immutable) once declared.Printing Maximum Occurred Character The maximum occurring character of a string refers to the character that appears multiple times (more than any other character in a string) in a string. To count the maximum occurring character of a string, we have the following approaches - Using an Array Using HashMap Using an Array Using an array is one way to ...

Read More

Difference between ArrayList.clear() and ArrayList.removeAll() in java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 08-May-2025 1K+ Views

The ArrayList class in Java is a Resizable-array implementation of the List interface. It allows null values. Java clear() Method V/S removeAll() Method There are some important differences between the clear() and removeAll (Collection c) methods of the ArrayList class. This table compares these two methods. Key clear() removeAll() ...

Read More

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-May-2025 4K+ Views

No, HashMap does not allow duplicate keys. The HashMap is a class that implements the Map interface. It is based on the Hash table. It is used to store key-value pairs. It allows null keys and values, but it does not allow duplicate keys. You can store key-value pairs in the HashMap object. Once you do so, you can retrieve the values of the respective keys, but the values we use for keys should be unique. Let's understand what will happen if we try to add a duplicate key into a HashMap.Addng a duplicate key to a HashMapThe put() method ...

Read More
Showing 1471–1480 of 4,496 articles
« Prev 1 146 147 148 149 150 450 Next »
Advertisements