- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 2616 Articles for Java
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
6K+ Views
By default, the Gson object does not serialize the fields with null values to JSON. If a field in a Java object is null, Gson excludes it. We can force Gson to serialize null values via the GsonBuilder class. We need to call the serializeNulls() method on the GsonBuilder instance before creating the Gson object. Once serializeNulls() has been called the Gson instance created by the GsonBuilder can include null fields in the serialized JSON.Syntaxpublic GsonBuilder serializeNulls()Exampleimport com.google.gson.*; import com.google.gson.annotations.*; public class NullFieldTest { public static void main(String args[]) { GsonBuilder builder = new GsonBuilder(); builder.serializeNulls(); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
179 Views
Let’s say the following is our Iterable −Iterable i = Arrays.asList("K", "L", "M", "N", "O", "P");Now, create a Collection −Stream s = convertIterable(i);Above, we have a custom method convertIterable() for conversion. Following is the method −public static Stream convertIterable(Iterable iterable) { return StreamSupport.stream(iterable.spliterator(), false); }ExampleFollowing is the program to convert an Iterable to Stream in Java − Live Demoimport java.util.*; import java.util.stream.*; public class Demo { public static Stream convertIterable(Iterable iterable) { return StreamSupport.stream(iterable.spliterator(), false); } public static void main(String[] args) { Iterable i = Arrays.asList("K", "L", "M", "N", ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
186 Views
Let’s say the following is our Iterable −Iterable i = Arrays.asList(50, 100, 150, 200, 250, 300, 500, 800, 1000);Now, create a Collection −Collection c = convertIterable(i);Above, we have a custom method convertIterable() for conversion. Following is the method −public static Collection convertIterable(Iterable iterable) { if (iterable instanceof List) { return (List) iterable; } return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()); }ExampleFollowing is the program to convert an Iterable to Collection in Java − Live Demoimport java.util.*; import java.util.stream.*; public class Demo { public static Collection convertIterable(Iterable iterable) { if (iterable instanceof List) { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
303 Views
Let’s say the following is our string −String str = "55.2";Now, convert the above string to double using parseDouble() in Java −double res = Double.parseDouble("23.6");ExampleFollowing is the program to convert String to Double in Java − Live Demopublic class Demo { public static void main(String args[]){ String str = "55.2"; double res = Double.parseDouble("23.6"); System.out.println("Double (String to Double) = "+res); } }OutputDouble (String to Double) = 23.6
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
512 Views
Let’s say the following are our strings −String date1 ="11/10/2018"; String date2 = "15-Mar-2019 21:11:35";Now, convert the above string to dates −SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); Date formattedDate1 = dateFormat1.parse(date1); Date formattedDate2 = dateFormat2.parse(date2);ExampleFollowing is the program to convert String to Date in Java − Live Demoimport java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] args)throws Exception { String date1 ="11/10/2018"; String date2 = "15-Mar-2019 21:11:35"; SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
830 Views
Let’s say the following is our string −String str = "Website!";Now, convert the above string to a list of characters −Listlist = str.chars().mapToObj(n -> (char)n).collect(Collectors.toList());ExampleFollowing is the program to convert a string to a list of characters in Java − Live Demoimport java.util.*; import java.util.stream.Collectors; public class Demo { public static void main(String[] args){ String str = "Website!"; System.out.println("String = "+str); Listlist = str.chars().mapToObj(n -> (char)n).collect(Collectors.toList()); System.out.println("List of Characters = "+list); } }OutputString = Website! List of Characters = [W, e, b, s, i, t, e, !]Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
Try and catch in JavaA method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception.Following is the syntax for try and catch −try { // Protected code } catch (ExceptionName e1) { // Catch block }A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
103 Views
The Gson library provides a simple versioning system for the Java objects that it reads and writes and also provides an annotation named @Since for the versioning concept @Since(versionnumber).We can create a Gson instance with versioning using the GsonBuilder().setVersion() method. If we mentioned like setVersion(2.0), means that all the fields having 2.0 or less are eligible to parse.Syntaxpublic GsonBuilder setVersion(double ignoreVersionsAfter)Exampleimport com.google.gson.*; import com.google.gson.annotations.*; public class VersionSupportTest { public static void main(String[] args) { Person person = new Person(); person.firstName = "Raja"; person.lastName = "Ramesh"; Gson gson1 = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create(); ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
5K+ Views
A Gson is a JSON library for Java, which is created by Google. By using Gson, we can generate JSON and convert JSON to java objects. We can create a Gson instance by creating a GsonBuilder instance and calling with the create() method. The GsonBuilder().setDateFormat() method configures Gson to serialize Date objects according to the pattern provided.Syntaxpublic GsonBuilder setDateFormat(java.lang.String pattern)Exampleimport java.util.Date; import com.google.gson.*; public class DateformatTest { public static void main(String[] args) { Employee emp = new Employee(115, "Surya", new Date(), 25000.00); Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); String result = gson.toJson(emp); System.out.println(result); ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
9K+ Views
The JSON is a lightweight, text-based and language-independent data exchange format. A.JSONObject can parse text from a string to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The files in the org.json package implement JSON encoders/decoders in Java. It also includes the capability to convert between JSON, XML, HTTP headers, Cookies, and CDL.We can pretty-print a JSON using the toString(int indentFactor) method of org.json.JSONObject class, where indentFactor is the number of spaces to add to each level of indentation.Syntaxpublic java.lang.String toString(int indentFactor) throws JSONExceptionExampleimport org.json.*; public class JSONPrettyPrintTest { public static void main(String args[]) throws JSONException ... Read More