Found 9150 Articles for Object Oriented Programming

Stream.distinct() in Java

AmitDiwan
Updated on 25-Sep-2019 07:32:21

569 Views

The distinct() method of the stream class returns a stream consisting of the distinct elements of this stream. The syntax is as following −Stream distinct()ExampleFollowing is an example to implement the distinct() method in the Stream class −import java.util.*; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList(10, 30, 40, 40, 50, 70, 90, 90, 100);       System.out.println("List = "+list);       System.out.println("Displaying only the distinct elements = ");       list.stream().distinct().forEach(System.out::println);    } }OutputList = [10, 30, 40, 40, 50, 70, 90, 90, 100] Displaying only ... Read More

Stream.concat() in Java

AmitDiwan
Updated on 25-Sep-2019 07:30:33

263 Views

The concat() method of the Stream class in Java creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. The syntax is as follows −concat(Stream

How can we add a JSONArray within JSONObject in Java?

raja
Updated on 04-Jul-2020 08:50:58

5K+ Views

A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class.Syntaxpublic JSONObject put(java.lang.String key, java.util.Collection value) throws JSONExceptionExampleimport org.json.*; public class AddJSONArrayTest {    public static void main(String[] args) throws JSONException {       JSONArray array = new JSONArray();       array.put("INDIA");       array.put("AUSTRALIA");       array.put("ENGLAND");       JSONObject obj = ... Read More

How to implement custom JSON serialization with Gson in Java?

Aishwarya Naglot
Updated on 12-May-2025 10:38:49

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

How to convert XML to JSON array in Java?

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

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

How to write a JSON string to file using the Gson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 10:22:09

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

How to get all the keys of a JSON object using GSON in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 18:31:43

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

Stream.Builder build() in Java

AmitDiwan
Updated on 24-Sep-2019 08:46:12

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

Stream sorted() in Java

AmitDiwan
Updated on 24-Sep-2019 08:44:05

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

Java floor() method with Examples

AmitDiwan
Updated on 24-Sep-2019 08:42:09

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

Advertisements