Implement Quintet Class with Quartet Class in Java using Javatuples

AmitDiwan
Updated on 24-Sep-2019 07:28:39

104 Views

Following is an example to implement Quintet class from Quartet class in Java −Exampleimport org.javatuples.Quartet; import org.javatuples.Quintet; public class MyDemo {    public static void main(String[] args) {       Quartetquartet = new Quartet(          "Table", "Chair", "Pen", "Paper");       System.out.println("Quartet elements = " + quartet);       Quintetquintet = quartet.add("Notebook");       System.out.println("Quintet (implemented from Quartet) elements = " + quintet);    } }OutputQuartet elements = [Table, Chair, Pen, Paper] Quintet (implemented from Quartet) elements = [Table, Chair, Pen, Paper, Notebook]Let us now see another example to implement Quintet class from ... Read More

Division Operators in Java

AmitDiwan
Updated on 24-Sep-2019 07:17:36

14K+ Views

The division operator in Java includes the Division, Modulus, and the Divide And Assignment operator. Let us work with them one by one −Divison OperatorThe division operator divides left-hand operand by right-hand operand.Modulus OperatorThe modulus operator divides left-hand operand by right-hand operand and returns remainder.Divide And Assignment OperatorThis operator divides left operand with the right operand and assign the result to left operand.Let us now see an example −Examplepublic class Demo {    public static void main( String args[] ) {       int a = 10;       int b = 20;       int c ... Read More

Implement Quartet Class with Triplet Class in Java using Javatuples

AmitDiwan
Updated on 24-Sep-2019 07:15:56

119 Views

Following is an example to implement Quartet class from Triplet class in Java −Exampleimport org.javatuples.Triplet; import org.javatuples.Quartet; public class MyDemo {    public static void main(String[] args) {       Triplettriplet = new Triplet("Gray", "Blue", "Gray100");       System.out.println("Triplet elements = " + triplet);       Quartetquartet = triplet.add("Blue30");       System.out.println("Quartet (Implemented from Triplet) = " + quartet);    } }OutputTriplet elements = [Gray, Blue, Gray100] Quartet (Implemented from Triplet) = [Gray, Blue, Gray100, Blue30]Let us now see another example to implement Quartet class from Triplet. This inserts element at position 3rd −Exampleimport org.javatuples.Triplet; import ... Read More

Join Function in Java

AmitDiwan
Updated on 24-Sep-2019 07:13:04

326 Views

The join() method of Ints class returns a string containing the supplied int values separated by separator. The syntax is as follows −public static String    join(String separator, int[] arr)Here, separator parameter is something that should appear between consecutive values, whereas arr is an array of int values.Let us first see an example −Exampleimport com.google.common.primitives.Ints; class Demo {    public static void main(String[] args) {       int[] myArr = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };       System.out.println(Ints.join("-", myArr));       int index = Ints.indexOf(myArr, 40);       if ... Read More

ints indexOf Function in Java

AmitDiwan
Updated on 24-Sep-2019 07:10:34

1K+ Views

The indexOf() method of the Ints class returns the index of the first appearance of the value target in array. The syntax is as follows −public static int indexOf(int[] arr, int target)Here, parameter arr is an array of int values and target is the value to be checked for first appearance.Let us now see an example −Exampleimport com.google.common.primitives.Ints; class Demo {    public static void main(String[] args) {       int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };       int index = Ints.indexOf(arr, 40);       if (index != ... Read More

Convert Stream to Array in Java

AmitDiwan
Updated on 24-Sep-2019 07:08:29

199 Views

Let’s say the following is our stream −Stream stream = Stream.of(50, 100, 200, 400, 800, 1000, 2000);Now, convert stream to an array using toArray() −Object[] objArr = stream.toArray(Object[] ::new);Following is the program to convert Stream to an Array in Java −Exampleimport java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       Stream stream = Stream.of(50, 100, 200, 400, 800, 1000, 2000);       Object[] objArr = stream.toArray(Object[] ::new);       System.out.println("Array = "+ Arrays.toString(objArr));    } }OutputArray = [50, 100, 200, 400, 800, 1000, 2000]

Convert Set to List in Java

AmitDiwan
Updated on 23-Sep-2019 14:03:15

266 Views

Let’s say the following is our Set with string values −Set set = new HashSet(); set.add("Laptop"); set.add("Mobile"); set.add("Tablet"); set.add("LCD"); set.add("LED");Now, let us convert it to List −List list = new ArrayList(set);Following is the program to convert Set to List in Java −Exampleimport java.util.*; import java.util.stream.*; public class Demo {    public static void main(String args[]) {       Set set = new HashSet();       set.add("Laptop");       set.add("Mobile");       set.add("Tablet");       set.add("LCD");       set.add("LED");       set.add("Desktop");       System.out.println("Set = " + set);       List ... Read More

Convert Set of String to Set of Integer in Java

AmitDiwan
Updated on 23-Sep-2019 14:02:02

484 Views

Let’s say the following is our set of string −Set setStr = new HashSet(Arrays.asList("50", "100", "150", "200", "250", "300", "500"));Now, convert it to set of Integer −Set setInteger = setStr.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toSet());ExampleFollowing is the program to convert set of String to set of Integer in Java −import java.util.*; import java.util.stream.*; public class Demo {    public static void main(String args[]) {       Set setStr = new HashSet(Arrays.asList("50", "100", "150", "200", "250", "300", "500"));       System.out.println("Set (String) = " + setStr);       Set setInteger = setStr.stream().map(s -> Integer.parseInt(s))          .collect(Collectors.toSet());     ... Read More

Convert Set of Integer to Set of String in Java

AmitDiwan
Updated on 23-Sep-2019 14:00:16

255 Views

Let’s say the following is our Set of Integer −Set setInteger = new HashSet(Arrays.asList(100, 200, 300, 500, 600, 800, 1000));Now, let us convert this to Set of String −Set setStr = setInteger.stream().map(String::valueOf).collect(Collectors.toSet());ExampleFollowing is the program to convert set of Integer to Set of String in Java −import java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       Set setInteger = new HashSet(Arrays.asList(100, 200, 300, 500, 600, 800, 1000));       System.out.println("Set = " + setInteger);       Set setStr = setInteger.stream().map(String::valueOf).collect(Collectors.toSet());       System.out.println("New Set (String) = " ... Read More

Convert Set of Integer to Array of Integer in Java

AmitDiwan
Updated on 23-Sep-2019 13:57:11

309 Views

Let’s say the following is our set of Integer −Set set = new HashSet(Arrays.asList(50, 100, 150, 200, 400, 600, 800, 1000, 1200, 1500));Now, convert it to Array of Integer −int[] arr = set.stream()    .mapToInt(Integer::intValue)    .toArray();ExampleFollowing is the program to convert set of Integer to Array of Integer in Java −import java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       Set set = new HashSet(Arrays.asList(50, 100, 150, 200, 400, 600, 800, 1000, 1200, 1500));       System.out.println("Set = " + set);       int[] arr = set.stream().mapToInt(Integer::intValue).toArray();       System.out.println("Array = "+ Arrays.toString(arr));    } }OutputSet = [400, 800, 1200, 50, 100, 150, 200, 600, 1000, 1500] Array = [400, 800, 1200, 50, 100, 150, 200, 600, 1000, 1500]

Advertisements