Found 7442 Articles for Java

Convert a Map to JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:54:04

4K+ Views

Converting a map to a JSON object is our task here. Let's see how to convert a map to a JSON object in Java using the Gson library. Converting a Map to JSON using the Gson library JSON is a simple and lightweight data interchange format. It stores data in key-value pairs. A map is also a collection of key-value pairs. So, we can easily convert a map to a JSON object. If you are not familiar with JSON, refer JSON. And if you want to know more about Map, refer Map. Gson is developed by Google. It is an ... Read More

Convert a list of objects to JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:58:08

8K+ Views

In this article, we will be discussing how to convert a list of objects to JSON using the Gson library in Java. JSON is an interchangeable format that is used for data exchange. Values in JSON are represented as key-value pairs. To know more about JSON, refer JSON. Java GSON Library: Converting a list of objects to JSON Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. In the Gson library, we can convert a list of objects to JSON using the toJson() method. We cannot use the ... Read More

Check if a string contains only alphabets in Java using ASCII values

AmitDiwan
Updated on 20-Sep-2019 08:29:26

482 Views

Let’s say we have set out inut string in myStr variable. Now loop through until the length of string and check for alphabets with ASCII values −for (int i = 0; i < myStr.length(); i++) {    char c = myStr.charAt(i);    if (!(c >= 'A' && c = 'a' && c = 'A' && c = 'a' && c

Check if a string contains only alphabets in Java using Regex

AmitDiwan
Updated on 20-Sep-2019 08:24:32

976 Views

At first, convert the string into character array. Here, name is our string −char[] ch = name.toCharArray();Now, loop through and find whether the string contains only alphabets or not. Here, we are checking for not equal to a letter for every character in the string −for (char c : ch) {    if(!Character.isLetter(c)) {       return false;    }Following is an example to check if a string contains only alphabets using RegexExample Live Demopublic class Main {    public static boolean checkAlphabet(String name) {       char[] ch = name.toCharArray();       for (char c : ch) { ... Read More

Check if a string contains only alphabets in Java using Lambda expression

AmitDiwan
Updated on 20-Sep-2019 08:19:04

782 Views

Let’s say our string is −String str = "Amit123";Now, using allMatch() method, get the boolean result whether the string has only alphabets or now −boolean result = str.chars().allMatch(Character::isLetter);Following is an example to check if a string contains only alphabets using Lambda Expressions −Exampleclass Main {    public static void main(String[] args) {       String str = "Amit123";       boolean result = str.chars().allMatch(Character::isLetter);       System.out.println("String contains only alphabets? = "+result);    } }OutputLet us see another example with a different input −String contains only alphabets? = falseExampleclass Main {    public static void main(String[] args) ... Read More

Insert a string into another string in Java

AmitDiwan
Updated on 27-Aug-2024 18:46:59

4K+ Views

In this article, we will explore how to insert a string into another string at a specific position using Java. To do so we will be using the StringBuffer class.  StringBuffer class: StringBuffer class creates and manipulates strings that can be changed. It changes the contents of the string without creating a new object each time. Problem Statement Write a program in Java to insert a string into another string − Input That's good! Output Index where new string will be inserted = 6Resultant String = That's no good! Steps to insert a string into ... Read More

Initialize HashMap in Java

AmitDiwan
Updated on 20-Sep-2019 07:57:58

711 Views

The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the list of constructors supported by the HashMap class.Sr.NoConstructor & Description1HashMap( )This constructor constructs a default HashMap.2HashMap(Map m)This constructor initializes the hash map by using the elements of the given Map object m.3HashMap(int capacity)This constructor initializes the capacity of the hash map to the given integer value, capacity.4HashMap(int capacity, float fillRatio)This constructor initializes both the capacity and fill ratio of the hash map by using its ... Read More

Initialize an ArrayList in Java

Alshifa Hasnain
Updated on 18-Mar-2025 17:55:13

1K+ Views

In this article, we will learn to initialize an ArrayList in Java. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Different Approaches The following are the two different approaches to initialize an ArrayList in Java − Using add() Method Using asList() method Using add() Method One of the most frequent methods of ... Read More

Implement Triplet Class with Pair Class in Java using JavaTuples

AmitDiwan
Updated on 20-Sep-2019 07:52:17

104 Views

At first, create a Pair class −Pairpair = new Pair(Integer.valueOf(25), "green");Now, implement Triplet class with Pair class −Triplet triplet = pair.add("magenta");Following is an example to implement Triplet class with Pair class in Java −Exampleimport org.javatuples.Pair; import org.javatuples.Triplet; public class MyDemo {    public static void main(String[] args) {       Pairpair = new Pair(Integer.valueOf(25), "green");       System.out.println("Pair class = " + pair);       Triplettriplet = pair.add("magenta");       System.out.println("Triplet class elements (Implemented from Pair class) = " + triplet);    } }OutputPair class = [25, green] Triplet class elements (Implemented from Pair class) = [25, green, magenta]

Convert JSON object to Java object using Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 14:24:14

9K+ Views

Let's see learn how to change a JSON object into a Java object using the Gson library. Gson is a tool made by Google for converting Java objects into JSON and back. JSON stands for JavaScript Object Notation. In the JSON format, data is stored in key-value pairs. Mainly, it is used for sharing data between a server and a web app. Gson is a Java tool that can turn Java objects into JSON and JSON back into Java objects. It is used by many developers because it is simple and easy. Why Convert JSON Object to Java Object? Here ... Read More

Advertisements