
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

1K+ Views
Custom JSON serialization Custom JSON Serialization means defining how your Java objects should be converted to JSON format. This is useful when you want to control the JSON output, such as changing field names, excluding certain fields, or formatting the data in a specific way. In this tutorial, we will create a custom serializer for a Java object using the Gson library. We will define a class Person with fields for name and age, and then create a custom serializer to control how this object is converted to JSON. To use Gson library, we need to add the Gson library ... Read More

9K+ Views
Xml is a markup language that is used to store or transport data. It uses tags to define the data. There are libraries also available for converting XML to JSON. Those are: org.json Jackson Gson Let's learn how to convert XML to JSON using various libraries. Converting XML to JSON using org.json For converting XML to JSON, we will use the org.json library. It is a simple library that is used for converting XML to JSON and vice versa. We can use the XML.toJSONObject() method to ... Read More

2K+ Views
Let's learn how to write a JSON string to a file using the Gson library in Java. If you are not familiar with JSON, you can refer to JSON Overview. Gson is a library that can be used to convert Java Objects to a JSON representation. To know more about Gson, refer to the Gson tutorial. GsonBuilder Class The primary class to use is Gson, which we can create by calling the new Gson(), and the GsonBuilder class can be used to create a Gson instance. We can write a JSON string to a file using the toJson() method of ... Read More

4K+ Views
JSON is the data interchange format that stores values in key-value pairs. (To learn more about JSON, you can refer to the JSON tutorial). So, as we have key and value in JSON, we will try to get all those keys using the Gson library. Gson Library: Retrieving all the keys of a JSON Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. To know more about Gson, refer to the Gson tutorial. We can use the keySet() method of JsonObject class to get all the keys of a ... Read More

1K+ Views
The build() method of the Stream.Builder class builds the stream, transitioning this builder to the built state. The syntax is as follows −Stream build()Following is an example to implement the build() method of the Stream.Builder class −Exampleimport java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream.Builder builder = Stream.builder(); builder.add("Production"); builder.add("Marketing"); builder.add("Finance"); builder.add("Sales"); builder.add("Operations"); Stream stream = builder.build(); stream.forEach(System.out::println); } }OutputProduction Marketing Finance Sales OperationsExampleLet us see another example of build() ... Read More

467 Views
The Stream sorted() in Java returns a stream consisting of the elements of this stream, sorted according to natural order.Following is the syntax −Stream sorted()Here, Stream is an interface in java.util.stream and is the type parameter in stream. This method returns the new stream.Following is an example to implement the sorted() method in stream class −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { List list = Arrays.asList("Jack", "Tom", "Kevin", "David", "Tim", "Nathan", "John", "Ryan", "Robbie"); System.out.println("Sorted stream... "); list.stream().sorted().forEach(System.out::println); } }OutputThe sorted stream is ... Read More

260 Views
The java.lang.Math.floor() returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases −If the argument value is already equal to a mathematical integer, then the result is the same as the argument.If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.Let us now see an example to implement the floor() method in Java −Exampleimport java.lang.*; public class Demo { public static void main(String[] args) { // get two ... Read More

279 Views
The java.lang.Math.sqrt(double a) returns the correctly rounded positive square root of a double value. Special cases −If the argument is NaN or less than zero, then the result is NaN.If the argument is positive infinity, then the result is positive infinity.If the argument is positive zero or negative zero, then the result is the same as the argument.Following is an example to implement the sqrt() method of the Math class in Java −Exampleimport java.lang.*; public class Demo { public static void main(String[] args) { // get two double numbers numbers double x = 9; ... Read More

486 Views
The java.util.HashMap class is the Hash table based implementation of the Map interface. To clone a Map in Java, use the clone() method.ExampleLet us see an example to clone a Map −import java.util.*; public class HashMapDemo { public static void main(String args[]) { // create two hash maps HashMap newmap1 = new HashMap(); HashMap newmap2 = new HashMap(); // populate 1st map newmap1.put(1, "This"); newmap1.put(2, "is"); newmap1.put(3, "it!"); // clone 1st map newmap2 = ... Read More

416 Views
To clone a list in Java, the easiest way is using the ArrayList.clone() method −Exampleimport java.util.ArrayList; public class Demo { public static void main(String args[]) { // create an empty array list ArrayList arrlist1 = new ArrayList(); // use add for new value arrlist1.add(new StringBuilder("Learning-")); // using clone to affect the objects pointed to by the references. ArrayList arrlist2 = (ArrayList) arrlist1.clone(); // appending the string StringBuilder strbuilder = arrlist1.get(0); strbuilder.append("list1, list2-both ... Read More