Java Articles

Page 184 of 450

Run-time Stack mechanism in Java

AmitDiwan
AmitDiwan
Updated on 07-Jul-2020 1K+ Views

Eeverytime a process or a code or a thread needs to run in Java, a runtime stack is created so as to store the operations performed while executing the thread.Every entry in the run-time stack is known as stack frame or activation record. Once a function has been called by the process, its associated data is deleted from the runtime stack.Once all the functions have been called, the runtime stack will be empty. This means it needs to be removed from the memory.At this point in time, the runtime stack is destroyed and then the thread is also terminated.A termination ...

Read More

Merge two sets in Java

AmitDiwan
AmitDiwan
Updated on 07-Jul-2020 3K+ Views

To merge two sets in Java, the code is as follows −Example Live Demoimport java.util.stream.*; import java.util.*; import java.io.*; public class Demo{    public static Set set_merge(Set set_1, Set set_2){       Set my_set = set_1.stream().collect(Collectors.toSet());       my_set.addAll(set_2);       return my_set;    }    public static void main(String[] args){       Set my_set_1 = new HashSet();       my_set_1.addAll(Arrays.asList(new Integer[] { 34, 67, 89, 102 }));       Set my_set_2 = new HashSet();       my_set_2.addAll(Arrays.asList(new Integer[] { 77, 11, 0 , -33}));       System.out.println("The first set contains " ...

Read More

Max Heap in Java

AmitDiwan
AmitDiwan
Updated on 07-Jul-2020 3K+ Views

Max heap is a complete binary tree, wherein the value of a root node at every step is greater than or equal to value at the child node.Below is an implementation of Max Heap using library functions.Example Live Demoimport java.util.*; public class Demo{    public static void main(String args[]){       PriorityQueue my_p_queue = new PriorityQueue(Collections.reverseOrder());       my_p_queue.add(43);       my_p_queue.add(56);       my_p_queue.add(99);       System.out.println("The elements in the priority queue are : ");       Iterator my_iter = my_p_queue.iterator();       while (my_iter.hasNext())       System.out.println(my_iter.next());       my_p_queue.poll(); ...

Read More

Java Virtual Machine (JVM) Stack Area

AmitDiwan
AmitDiwan
Updated on 07-Jul-2020 1K+ Views

Following are some key points to undertstand JVM Stack Area −During the creation of a thread, the Java Virtual Machine creates a separate stack.The JVM performs only two operations upon this stack. The operations are push (i.e insert) and pop (i.e delete).When a thread is currently in execution, the stack associated with it is known as runtime stack.Every method call done by the thread, intermediate calculations, assignment of local variables, calling parameters etc, are stored as an operation in the runtime stack.Once the thread stops or completes executing, the respective part from the stack is deleted.Once all the calls by ...

Read More

Java Program for Reversal algorithm for array rotation

AmitDiwan
AmitDiwan
Updated on 07-Jul-2020 387 Views

Following is the Java program to implement Reversal algorithm for array rotation −Example Live Demoimport java.io.*; public class Demo{    static void rotate_left(int my_arr[], int no_of_rotation){       int n = my_arr.length;       array_reversal(my_arr, 0, no_of_rotation - 1);       array_reversal(my_arr, no_of_rotation, n - 1);       array_reversal(my_arr, 0, n - 1);    }    static void array_reversal(int my_arr[], int start, int end){       int temp;       while (start < end) {          temp = my_arr[start];          my_arr[start] = my_arr[end];          my_arr[end] = ...

Read More

Java Program for Largest K digit number divisible by X

AmitDiwan
AmitDiwan
Updated on 07-Jul-2020 230 Views

Following is the Java program for largest K digit number divisible by X −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static int largest_k(int val_1, int val_2){       int i = 10;       int MAX = (int)Math.pow(i, val_2) - 1;       return (MAX - (MAX % val_1));    }    public static void main(String[] args){       int val_1 = 25;       int val_2 = 2;       System.out.println("The largest 2 digit number divisible by 25 is ");       System.out.println((int)largest_k(val_1, val_2));    } }OutputThe largest 2 ...

Read More

How to parse a JSON string using Streaming API in Java?

raja
raja
Updated on 07-Jul-2020 3K+ Views

The Streaming API consists of an important interface JsonParser and this interface contains methods to parse JSON in a streaming way and provides forward, read-only access to JSON data. The Json class contains the methods to create parsers from input sources. We can parse a JSON using the static method createParser() of Json class.Syntaxpublic static JsonParser createParser(Reader reader)Exampleimport java.io.*; import javax.json.Json; import javax.json.stream.JsonParser; import javax.json.stream.JsonParser.Event; public class JSONParseringTest {    public static void main(String[] args) {       String jsonString = "{\"name\":\"Adithya\", \"employeeId\":\"115\", \"age\":\"30\"}";       JsonParser parser = Json.createParser(new StringReader(jsonString));       while(parser.hasNext()) {          Event event = ...

Read More

How to create a JSON Object using Object Model in Java?

raja
raja
Updated on 07-Jul-2020 3K+ Views

The javax.json.JsonObject interface can represent an immutable JSON object value and provides an unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using the static method readObject() of javax.json.JsonReader class and also can be created using the static method createObjectBuilder() of javax.json.Json class.Syntaxpublic static JsonObjectBuilder createObjectBuilder()Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest {    public static void main(String[] args) throws Exception {       JsonObjectBuilder builder = Json.createObjectBuilder();       builder.add("Name", "Adithya");       builder.add("Designation", "Python Developer");       builder.add("Company", "TutorialsPoint");       builder.add("Location", "Hyderabad");       JsonObject data = builder.build();     ...

Read More

Importance of the accumulate() method of JSONObject in Java?

raja
raja
Updated on 07-Jul-2020 2K+ Views

A JSONObject is an unordered collection of a name and value pairs. A few important methods of JSONArray are accumulate(), put(), opt(), append(), write() and etc. The accumulate() method accumulates the values under a key and this method similar to the put() method except if there is an existing object stored under a key then a JSONArray can be stored under a key to hold all of the accumulated values. If there is an existing JSONArray then a new value can be added.Syntaxpublic JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONExceptionExampleimport org.json.*; public class JSONAccumulateMethodTest {    public static void main(String[] args) throws JSONException { ...

Read More

How to convert a JSON object to an enum using Jackson in Java?

raja
raja
Updated on 07-Jul-2020 5K+ Views

A JSONObject can parse the text from String to produce a Map kind of an object. An Enum can be used to define a collection of constants, when we need a predefined list of values which do not represent some kind of numeric or textual data then we can use an enum. We can convert a JSON object to an enum using the readValue() method of ObjectMapper class.In the below example, we can convert/deserialize a JSON object to Java enum using the Jackson library.Exampleimport com.fasterxml.jackson.databind.*; public class JSONToEnumTest {    public static void main(String arg[]) throws Exception {       ObjectMapper mapper ...

Read More
Showing 1831–1840 of 4,496 articles
« Prev 1 182 183 184 185 186 450 Next »
Advertisements