Found 7442 Articles for Java

How to access the JSON fields, arrays and nested objects of JsonNode in Java?

Aishwarya Naglot
Updated on 13-May-2025 17:15:34

33K+ Views

What are JSON and JsonNode? JSON is a format that is used to store data and exchange data between a server and a client. To know more about JSON, you can refer to the JSON Tutorial. A JsonNode is Jackson's tree model for JSON. To read JSON into a JsonNode with Jackson by creating an ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of the JsonNode class. We can return a valid string representation using the asText() method and ... Read More

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

Aishwarya Naglot
Updated on 12-May-2025 10:12:16

678 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

Importance of a JSONTokener in Java?

Aishwarya Naglot
Updated on 12-May-2025 11:03:10

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

Streams on Arrays in Java 8

AmitDiwan
Updated on 25-Sep-2019 09:16:50

464 Views

Stream method of array class introduced in Java 8. Let us see an example wherein we are counting empty strings, eliminating empty strings, getting a list of square of distinct numbers, displaying random numbers, etc −Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.Random; import java.util.stream.Collectors; import java.util.Map; public class Java8Tester {    public static void main(String args[]) {       System.out.println("Using Java 7: ");       // Count empty strings       List strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");       System.out.println("List: " +strings);       long count = getCountEmptyStringUsingJava7(strings); ... Read More

Java program to count the occurrences of each character

AmitDiwan
Updated on 04-Jul-2024 17:35:45

4K+ Views

In Java, counting the occurrences of each character in a string is a common task that can be efficiently performed using a HashMap. A HashMap allows us to store key-value pairs, where each unique character in the string is a key, and the value is the count of its occurrences. Problem Statement Given a string, we need to count how many times each character appears in the string. For example, in the string thisisit, the character t appears twice, h appears once, i appears three times, and s appears twice. Input String myStr = "thisisit"; Output Counting ... Read More

Java program to check palindrome

Aishwarya Naglot
Updated on 13-May-2025 14:24:53

5K+ Views

What is a Palindrome? A palindrome is a number, string or any data value, which reads the same from backwards. i.e. If we reverse a value and compare it with its original value, the result should be true. For example, strings like "ABA", "RADAR", "BABAB" and numbers like 121, 12321 are palindromes. Checking for Palindrome Using Java Program One way to verify if a string, number or, an alphanumeric value is a palindrome is by reversing the given it. Following are the steps - Get the value to be verified. Reverse the contents of the given value. Compare ... Read More

Convert List of Characters to String in Java

Aishwarya Naglot
Updated on 06-May-2025 16:36:36

963 Views

In this article, we will learn to convert a list of characters to strings in Java. Sometimes, when we handle character-based data structures, we need to convert a list of characters into a string. Problem Statement Given a list of characters, the task is to convert it into a single string where the order of characters remains unchanged. Input:  list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e'); Output: Welcome Methods to Convert a List of Characters to a String The following are the different approaches to convert a list of characters to a string in Java - ... Read More

Convert Iterator to Iterable in Java

AmitDiwan
Updated on 25-Sep-2019 08:54:51

963 Views

Let’s say the following is our Iterator with Integer values −Iteratoriterator = Arrays.asList(20, 40, 60, 80, 100, 120, 150, 200).iterator();Now, convert the Iterator to Iterable −Iterableiterable = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0),false).collect(Collectors.toList());ExampleFollowing is the program to convert Iterator to Iterable in Java −import java.util.*; import java.util.stream.Collectors; import java.util.stream.StreamSupport; public class Demo {    public static void main(String[] args) {       Iteratoriterator = Arrays.asList(20, 40, 60, 80, 100, 120, 150, 200).iterator();       Iterableiterable = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).collect(Collectors.toList());       System.out.println("Iterable = ");       iterable.forEach(System.out::println);    } }OutputIterable = 20 40 60 80 100 120 150 200

Convert HashSet to TreeSet in Java

Alshifa Hasnain
Updated on 29-Mar-2025 02:56:23

1K+ Views

In this article, we will learn to convert a HashSet to a TreeSet in Java. Converting from one type of collection to another is a simple process when we require them to change their behavior or the nature of our data structure. Why Convert HashSet to TreeSet? For the following operations, we can convert a HashSet to a TreeSet − Sorted Order: TreeSet maintains elements in ascending order. Range-Based Operations: It supports operations like headSet(), tailSet(), and subSet(). NavigableSet Features: TreeSet provides methods like higher(), lower(), ceiling(), ... Read More

Convert Double to Integer in Java

AmitDiwan
Updated on 25-Sep-2019 08:40:49

771 Views

At first, initialize a double value −double val = 978.65;Now, convert the Double to Integer value using intValue() method −Double d = new Double(val); int res = d.intValue();ExampleFollowing is the program to convert Double to Integer in Java −public class Demo {    public static void main(String args[]) {       double val = 978.65;       System.out.println("Double = "+val);       Double d = new Double(val);       int res = d.intValue();       System.out.println("Double to Integer value = "+res);    } }OutputDouble = 978.65 Double to Integer value = 978

Advertisements