Found 4747 Articles for Java 8

Java program to swap first and last characters of words in a sentence

AmitDiwan
Updated on 14-Sep-2020 08:31:31
Following is the Java program to swap first and last characters of word in a sentence −Example Live Demopublic class Demo {    static String swap_chars(String my_str) {       char[] my_ch = my_str.toCharArray();       for (int i = 0; i < my_ch.length; i++) {          int k = i;          while (i < my_ch.length && my_ch[i] != ' ')             i++;             char temp = my_ch[k];             my_ch[k] = my_ch[i - 1];         ... Read More

Java program to merge two files into a third file

AmitDiwan
Updated on 14-Sep-2020 08:29:28
Following is the Java program to merge two files into a third file −Exampleimport java.io.*; public class Demo {    public static void main(String[] args) throws IOException {       PrintWriter my_pw = new PrintWriter("path to third .txt file");       BufferedReader my_br = new BufferedReader(new FileReader("path to first .txt file"));       String my_line = my_br.readLine();       while (my_line != null) {          my_pw.println(my_line);          my_line = my_br.readLine();       }       my_br = new BufferedReader(new FileReader("path to second .txt file"));       my_line ... Read More

Java Program for ShellSort

AmitDiwan
Updated on 14-Sep-2020 08:26:42
Shell sort is a sorting technique that is similar to insertion sort, wherein elements are sorted which are at far ends (either ends) of the array. This way, the interval size between the next and the second to last element reduces. This happens for all the elements in the array until the interval distance is reduced to 0.ExampleFollowing is an example for ShellSort in Java − Live Demopublic class Demo {    int shell_sort(int my_arr[]) {       int arr_len = my_arr.length;       for (int gap = arr_len / 2; gap > 0; gap /= 2) {   ... Read More

Java Program for Pigeonhole Sort

AmitDiwan
Updated on 14-Sep-2020 08:23:57
As the name suggests, pigeon holes are created, where the number of pigeon holes created is calculated using ‘max-min+1’, which is also known as the range of the array elements. The elements in the original array are iterated over and placed in the pigeonholes based on a specific condition.Further, after all elements are put in the pigeonhole, they are put back into the array in the same order in which they were placed in the pigeon holes.ExampleFollowing is an example for Pigeonhole sort in Java − Live Demoimport java.lang.*; import java.util.*; public class Demo {    public static void pigeonhole_sorting(int my_arr[], ... Read More

Java Program for Radix Sort

AmitDiwan
Updated on 14-Sep-2020 08:21:15
Radix sort is a sorting technique that sorts the elements based on every digit in every element (or number). Based on the ones place digit (which is also known as least significant digit) and tens place digit (which is also known as most significant digit), hundreds place digit, and so on, the elements are sorted.ExampleFollowing is an example for Radix Sort in Java − Live Demoimport java.util.*; public class my_radix_sorting {    static int get_max_val(int my_arr[], int arr_len) {       int max_val = my_arr[0];       for (int i = 1; i < arr_len; i++)       ... Read More

Java Program for Pancake sorting

AmitDiwan
Updated on 14-Sep-2020 08:18:19
Pancake sorting is a sorting technique that resembles selection sort, i.e. sorting the largest element first thereby reducing the size of the array and eventually sorting all the elements. In pancake sorting, the idea is to sort the array elements by making the least number of reversals.Following is an example for Pancake sorting in Java −Example Live Demoimport java.io.*; public class pancake_sorting {    static void flip_array(int my_arr[], int i) {       int temp, beg = 0;       while (beg < i) {          temp = my_arr[beg];          my_arr[beg] = my_arr[i]; ... Read More

Reference to a static method using method references in Java8

Maruthi Krishna
Updated on 08-Apr-2020 14:08:04
Lambda expressions In Java allows you to pass functionality as an argument to a method. You can also call an existing method using lambda expressions.list.forEach(n -> System.out.println(n));Method references are simple, easy-to-read lambda expressions to call/refer and the existing method by name in a lambda expression. You can refer to a static method defined in the class using method references.SyntaxFollowing is the syntax to reference a static method in JavaClassName:methodNameExampleThe following Java example references a static method in Java.interface myInterface{    void greet(); } public class MethodReferences {    public static void demo() {       System.out.println("Sample method");    } ... Read More

How to implement LongToDoubleFunction using lambda and method reference in Java?

raja
Updated on 15-Jul-2020 04:58:24
LongToDoubleFunction is a built-in functional interface and part of java.util.function package. This functional interface accepts a long-valued parameter as input and produces a double-valued result. LongToDoubleFunction can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsDouble().Syntax@FunctionalInterface interface LongToDoubleFunction {  double applyAsDouble(long value); }Example of Lambda Exampleimport java.util.function.LongToDoubleFunction; public class LongToDoubleLambdaTest {    public static void main(String args[]) {       LongToDoubleFunction getDouble = longVal -> { // lambda expression          double doubleVal = longVal;          return doubleVal;       };       long input = ... Read More

How to implement LongToIntFunction using lambda and method reference in Java?

raja
Updated on 15-Jul-2020 04:59:08
LongToIntFunction is a functional interface from java.util.function package introduced in Java 8. This functional interface accepts a long-valued parameter as input and produces an int-valued result. LongToIntFunction interface can be used as an assignment target for a lambda expression or method reference. This interface contains only one abstract method: applyAsInt() and doesn't contain any default and abstract methods.Syntax@FunctionalInterface interface LongToIntFunction {    int applyAsInt(long value); }Example of Lambda Expressionimport java.util.function.LongToIntFunction; public class LongToIntLambdaTest {    public static void main(String args[]) {       LongToIntFunction getInt = longVal -> {     // lambda expression          int intVal = (int)longVal;       ... Read More

How to implement IntToLongFunction using lambda and method reference in Java?

raja
Updated on 15-Jul-2020 04:56:16
IntToLongFunction is a built-in functional interface from java.util.function package. This functional interface accepts an int-valued parameter and produces a long-valued result. IntToLongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsLong().Syntax@FunctionalInterface interface IntToLongFunction {  long applyAsLong(int value); }Example of Lambda Expressionimport java.util.function.IntToLongFunction; public class IntToLongFunctionLambdaTest {    public static void main(String args[]) {       IntToLongFunction getLong = intVal -> {      // lambda expression          long longVal = intVal;          return longVal;       };           int input = 40; ... Read More
Advertisements