Java Articles

Page 15 of 450

Max Heap in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 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.Exampleimport 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

Merge two sets in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

To merge two sets in Java, the code is as follows −Exampleimport 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

Run-time Stack mechanism in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 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

How to write an empty function in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

Let us see how to write an empty function in Java −Exampleimport java.util.Vector; public class Demo{    public static void my_empty_fun(){    }    public static void main(String[] args){       System.out.println("In the main function");       my_empty_fun();    }   }OutputIn the main functionAn empty function is basically creating a function without defining any operations inside it. A class named Demo contains an empty function named ‘my_empty_fun’ which is just completed by placing two flower brackets, without adding any functionality into it. In the main function, a print statement is written after which the empty function is ...

Read More

Convert one base to other bases in a single Java Program

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

Let’s say we have an Octal number. To convert Octal to other bases like binary, hexadecimal, etc, the Java code is as follows −Examplepublic class Demo{    public static String base_convert(String num, int source, int destination){       return Integer.toString(Integer.parseInt(num, source), destination);    }    public static void main(String[] args){       String my_num = "345";       int source = 8;       int destination = 2;       System.out.println("Converting the number from octal to binary: "+ base_convert (my_num, source, destination));       destination = 10;       System.out.println("Converting the number from ...

Read More

Java program to generate random numbers within a given range and store in a list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 444 Views

To generate random numbers in a given range, the Java code is as follows −Exampleimport java.util.Random; import java.util.*; public class Demo{    public static void main(String args[]){       Random my_rand = new Random();       List my_list_1 = new ArrayList();       int v_1 = my_rand.nextInt(1000);       int v_2 = my_rand.nextInt(967);       int v_3 = my_rand.nextInt(1050);       int v_4 = my_rand.nextInt(10000);       int v_5 = my_rand.nextInt(100);       my_list_1.add(v_1);       my_list_1.add(v_2);       my_list_1.add(v_3);       my_list_1.add(v_4);       my_list_1.add(v_5);       System.out.println("The random values in the list are : ");       for(int i=0; i

Read More

Java program to split the Even and Odd elements into two different lists

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 899 Views

To split the Even and Odd elements into two different lists, the Java code is as follows −Exampleimport java.util.Scanner; public class Demo{    public static void main(String[] args){       int n, j = 0, k = 0;       Scanner s = new Scanner(System.in);       System.out.println("Enter the number of elements required :");       n = s.nextInt();       int my_arr[] = new int[n];       int odd_vals[] = new int[n];       int even_vals[] = new int[n];       System.out.println("Enter the elements of the array(even and add numbers) :"); ...

Read More

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 668 Views

To iterate over a Stream with Indices in Java 8, the code is as follows −Exampleimport java.util.stream.IntStream; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class Demo{    public static void main(String[] args){       String[] my_array = { "T", "h", "i", "s", "s", "a", "m", "p", "l", "e" };       AtomicInteger my_index = new AtomicInteger();       System.out.println("The elements in the string array are :");       Arrays.stream(my_array).map(str -> my_index.getAndIncrement() + " -> " + str).forEach(System.out::println);    } }OutputThe elements in the string array are : 0 -> T 1 -> h 2 -> i 3 -> ...

Read More

Java program to find Maximum and minimum element’s position in a list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 446 Views

To find the maximum and minimum element’s position in a list, the Java program is as follows −Exampleimport java.util.*; import java.util.Arrays; import java.util.Collections; public class Demo{    public static int index_val(int my_arr[], int t){       if (my_arr == null){          return -1;       }       int len = my_arr.length;       int i = 0;       while (i < len){          if (my_arr[i] == t){             return i;          } else {           ...

Read More

Find the uncommon values concatenated from both the strings in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 314 Views

To find the uncommon values concatenated from both the strings in Java, the code is as follows −Exampleimport java.util.*; import java.lang.*; import java.io.*; public class Demo{    public static String concat_str(String str_1, String str_2){       String result = "";       int i;       HashMap my_map = new HashMap();       for (i = 0; i < str_2.length(); i++)       my_map.put(str_2.charAt(i), 1);       for (i = 0; i < str_1.length(); i++)       if (!my_map.containsKey(str_1.charAt(i)))       result += str_1.charAt(i);       else       my_map.put(str_1.charAt(i), ...

Read More
Showing 141–150 of 4,496 articles
« Prev 1 13 14 15 16 17 450 Next »
Advertisements