AmitDiwan has Published 10740 Articles

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 09:52:03

642 Views

To iterate over a Stream with Indices in Java 8, the code is as follows −Example Live Demoimport 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" };   ... Read More

Java program to sort words of sentence in ascending order

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 09:41:56

5K+ Views

To sort words of sentence in ascending order, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    static void sort_elements(String []my_str, int n){       for (int i=1 ;i= 0 && temp.length() < my_str[j].length()){             my_str[j+1] = my_str[j];     ... Read More

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

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 09:16:21

879 Views

To split the Even and Odd elements into two different lists, the Java code is as follows −Example Live Demoimport java.util.Scanner; public class Demo{    public static void main(String[] args){       int n, j = 0, k = 0;       Scanner s = new Scanner(System.in);     ... Read More

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

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 09:15:25

434 Views

To generate random numbers in a given range, the Java code is as follows −Example Live Demoimport 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 = ... Read More

Convert one base to other bases in a single Java Program

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 08:27:06

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 −Example Live Demopublic class Demo{    public static String base_convert(String num, int source, int destination){       return Integer.toString(Integer.parseInt(num, source), destination);    }    public static void ... Read More

How to write an empty function in Java

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 08:24:48

3K+ Views

Let us see how to write an empty function in Java −Example Live Demoimport 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 ... Read More

Run-time Stack mechanism in Java

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 08:23:32

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 ... Read More

Merge two sets in Java

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 08:21:48

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 ... Read More

Merge arrays into a new object array in Java

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 08:20:12

363 Views

Following is the Java program to merge array into a new object array in Java −Example Live Demoimport java.util.stream.Stream; import java.util.Arrays; import java.io.*; public class Demo{    public static Object[] concat_fun(T[] my_obj_1, T[] my_obj_2){       return Stream.concat(Arrays.stream(my_obj_1), Arrays.stream(my_obj_2)).toArray();    }    public static void main (String[] args){   ... Read More

Memory leaks in Java

AmitDiwan

AmitDiwan

Updated on 07-Jul-2020 08:19:01

535 Views

In Java, garbage collection (the work of destructor) is done automatically using garbage collection. But what if there are objects that have references to them in the code? It can’t be de-allocated, i.e their memory can’t be cleared. If such a situation occurs again and again, and the created or ... Read More

Advertisements