Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 184 of 450
Run-time Stack mechanism in Java
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 MoreMerge two sets in Java
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 MoreMax Heap in Java
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 MoreJava Virtual Machine (JVM) Stack Area
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 MoreJava Program for Reversal algorithm for array rotation
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 MoreJava Program for Largest K digit number divisible by X
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 MoreHow to parse a JSON string using Streaming API in Java?
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 MoreHow to create a JSON Object using Object Model in Java?
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 MoreImportance of the accumulate() method of JSONObject in Java?
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 MoreHow to convert a JSON object to an enum using Jackson in Java?
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