Java Articles - Page 237 of 745

Dictionary Methods in Java

AmitDiwan
Updated on 24-Sep-2019 07:47:44

1K+ Views

Dictionary is an abstract class that represents a key/value storage repository and operates much like Map. Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key. Thus, like a map, a dictionary can be thought of as a list of key/value pairs.Following are the methods defined by Dictionary are listed below −Sr.NoMethod & Description1Enumeration elements( )Returns an enumeration of the values contained in the dictionary.2Object get(Object key)Returns the object that contains the value associated with the key. If the key is not in ... Read More

Ints toArray() function in Java

AmitDiwan
Updated on 24-Sep-2019 07:41:09

304 Views

The toArray() method of the Ints class returns an array containing each value of collection, converted to a int value in the manner of Number.intValue(). Following is the syntax −public static int[]    toArray(Collection

Ints max() function in Java

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

171 Views

The max() method of the Ints class returns the greatest value present in array. Following is the syntax −static int max(int... array)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, 20, 80, 20, 100 };       System.out.println(Ints.join("-", myArr));       System.out.println("Maximum value from the array = " + Ints.max(myArr));       // finding last index of element 20       int index = Ints.lastIndexOf(myArr, 20);       if (index != -1) { ... Read More

Ints lastIndexOf() function in Java

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

156 Views

The lastIndexOf() method of the Ints class returns the index of the last appearance of the value target in array. The syntax is as follows −public static int lastIndexOf(int[] arr, int target)Here, arr is the array of int values, whereas target is the int value for which we are finding the last index.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, 20, 80, 20, 100 };       System.out.println(Ints.join("-", myArr));       // finding last index ... Read More

Implement Quintet Class with Quartet Class in Java using JavaTuples

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

108 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

123 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

Ints join() function in Java

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

328 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

Program to convert Stream to an Array in Java

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

203 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]

Advertisements