Object Oriented Programming Articles

Page 335 of 589

How to pretty print JSON using the Gson library in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 4K+ Views

JSON is a data interchange format that is easy to read and write. It is lightweight and gets parsed easily. It is commonly used in web applications for sending and receiving data. To know more about JSON, refer to JSON. Pretty print is a type of formatting that formats data in a more easily readable way by adding indentation and line breaks. It is useful for debugging and logging purposes. Enabling pretty print Using Gson Library Gson is a JSON library for Java, which was created by Google. By using Gson, we can generate JSON and convert JSON to Java objects. ...

Read More

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 92K+ Views

Sorting a string alphabetically means rearranging its characters in order, from A to Z. For example, the string "java" becomes "aajv" after sorting. Different ways to sort a String Alphabetically There are two ways to do this in Java using different approaches - Using toCharArray() and Arrays.sort() Method Sorting the array manually Using toCharArray() and Arrays.sort() Method The toCharArray() method of this ...

Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 3K+ Views

In Java, the collections framework provides various classes like ArrayList, HashSet, and LinkedList to store groups of elements. But once data is stored, how do you access or retrieve it? Java provides multiple ways to retrieve elements from collections, depending on whether you're reading values, modifying them during traversal, or iterating forward or backward. You can retrieve the elements of a collection in four ways- Using For-Loop Using For-Each Loop ...

Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 468 Views

When working with collections in Java, you might need to convert an array to an ArrayList. This is useful because ArrayList offers more flexibility, such as dynamic sizing and built-in methods for adding, removing, and searching elements. Need for Converting an Array to an ArrayList Arrays in Java have a fixed size and provide limited functionality. ArrayList, on the other hand, is part of the Java Collections Framework and provides methods to modify data dynamically. It is useful when - Need to use dynamic Collections. ...

Read More

Remove all non-alphabetical characters of a String in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 2K+ Views

When you are working with strings in Java, you may want to keep only the alphabetic characters (A-Z, a-z) and remove everything else, like numbers, punctuation, and symbols. Java gives you multiple ways to do this for different cases or scenarios. Removing non-alphabetical characters from a string There are different methods, and they are - Using split() method Using replaceAll() method ...

Read More

How to construct a JSON object from a subset of another JSON object in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 21-Apr-2025 4K+ Views

In this article, we will learn how to construct a JSON object from a subset of another JSON object in Java. If you are not familiar with JSON and its usage in Java, you can refer to our JSON Overview tutorial. JSON Object From a Subset of Another Object There is more than one way to construct a JSON object from a subset of another JSON object in Java. Some of the popular libraries that we can use are - Using org.json library Using Gson library Using ...

Read More

What are the differences between a MouseListener and a MouseMotionListener in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 21-Apr-2025 2K+ Views

In this article, we will learn about the differences between a MouseListener and a MouseMotionListener in Java. We can implement a MouseListener interface when the mouse is stable while handling the mouse event, whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event. Mouse Listener A MouseListener is fired when we press, release or click (press followed by release) a mouse button (left or right button) at the source object or position the mouse pointer at (enter) and away (exit) from the source object. Abstract Methods A MouseListener interface declares the following five ...

Read More

Difference Between LinkedList and LinkedHashSet in Java

Aishwarya Naglot
Aishwarya Naglot
Updated on 21-Apr-2025 2K+ Views

LinkedList and LinkedHashSet are two important classes of Java's Collection framework. They are used to store groups of items, but they work in different ways and have their own unique features. What is a LinkedList in Java? Java LinkedList is a linear data structure that is used to store the same type of elements. It is a part of the Java Collections Framework, and it implements the List as well as the Deque interfaces. It has a dynamic size, which means it can grow and shrink as needed. Example The following is an example of how we write code for ...

Read More

Why can\'t a Java class be both abstract and final?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Apr-2025 4K+ Views

In Java, both abstract and final are class modifiers but they are completely opposite to each other. That's why Java class cannot be both abstract and final. Abstract class An abstract class in Java is a class that may contain both abstract methods (without implementation) and concrete methods (with implementation). If a class has even one abstract method, it must be declared abstract. You cannot create objects of an abstract class directly. If you want to use the concrete method in an abstract class you need to inherit the class, provide implementation to the abstract methods (if any) and ...

Read More

Can Enum extend any class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Apr-2025 2K+ Views

If you’ve been coding in Java for some time, you might have wondered why you can't make an enum extend another class. It seems like it should, but Java won't let you. Let's break down why this happens Enumeration in Java Enumeration (enum) in Java is a user-defined datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year, and seasons, etc. If we had to store all seven days of a week into one single variable then we might tend to use data ...

Read More
Showing 3341–3350 of 5,881 articles
« Prev 1 333 334 335 336 337 589 Next »
Advertisements