Found 4338 Articles for Java 8

Ints contains() function in Java

AmitDiwan
Updated on 23-Sep-2019 12:54:46

3K+ Views

The contains() function of the Ints class is used to check whether an element is present in the array or not.Following is the syntax −public static boolean contains(int[] arr, int target)Here, arr is the array wherein the element is to be checked. The target is the element to be checked.Following is an example to implement the contains() method of the Ints class −Exampleimport com.google.common.primitives.Ints; import java.util.*; class Demo {    public static void main(String[] args) {       int[] myArr1 = { 100, 150, 230, 300, 400 };       int[] myArr2 = { 450, 550, 700, 800, ... Read More

Ints concat() function in Java

AmitDiwan
Updated on 23-Sep-2019 12:52:54

130 Views

The concat() function in Ints class is used to concatenate the arrays passed as parameter. The syntax is as follows −public static int[] concat(int[]... arr)Here, parameter arr is zero or more integer arrays.Let us see an example −Exampleimport com.google.common.primitives.Ints; import java.util.*; class Demo {    public static void main(String[] args) {       int[] myArr1 = { 100, 150, 230, 300, 400 };       int[] myArr2 = { 450, 550, 700, 800, 1000 };       System.out.println("Array 1 = ");       for(int i=0; i < myArr1.length; i++) {          System.out.println(myArr1[i]);   ... Read More

Ints asList() function in Java

AmitDiwan
Updated on 23-Sep-2019 12:50:56

164 Views

The asList() method of the Ints class in Java Guava’s returns a fixed-size list backed by the specified array. The syntax is as follows −public static List    asList(int[] arr)Here, arr is the array to back the list.Let us see an example to implement the asList() method of the Ints class −Exampleimport com.google.common.primitives.Ints; import java.util.List; class Demo {    public static void main(String[] args) {       int arr[] = { 20, 30, 40, 60, 80, 90, 120, 150 };       System.out.println("Array elements = ");       for(int i = 0; i < arr.length; i++) { ... Read More

Java lang Integer.toHexString() Method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:48:42

108 Views

The java.lang.Integer.toHexString() method returns a string representation of the integer argument as an unsigned integer in base 16.Let us now see an example −Exampleimport java.lang.*; public class Main {    public static void main(String[] args) {       int i = 160;       System.out.println("Number = " + i);       System.out.println("Hex = " + Integer.toHexString(i));    } }OutputNumber = 160 Hex = a0ExampleLet us now see another example for negative number −import java.lang.*; public class Main {    public static void main(String[] args) {       int i = -160;       System.out.println("Number = " + i);       System.out.println("Hex = " + Integer.toHexString(i));    } }OutputNumber = -160 Hex = ffffff60

Java signum() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:46:47

116 Views

The java.lang.Math.signum(float f) returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       // get two float numbers       float x = 50.14f;       float y = -4f;       // call signum for both floats and print the result       System.out.println("Math.signum(" + x + ")=" + Math.signum(x));       ... Read More

Java Signature toString() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:42:17

89 Views

The string representation for the signature object can be obtained using the method getString() in the class java.security.Signature. This includes information such as the object state, algorithm name etcLet us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String str = signature.toString();          System.out.println(str);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!!! NoSuchAlgorithmException");       }    } }OutputSignature object: SHA256withRSALet us now ... Read More

Java Signature initSign() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:40:30

297 Views

The initSign() method initialize this object for signing. If this method is called again with a different argument, it negates the effect of this call.Let us now see an example −Exampleimport java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.Signature; import java.util.Scanner; public class Main {    public static void main(String args[]) throws Exception {       //Creating KeyPair generator object       KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DSA");       //Initializing the key pair generator       keyPairGen.initialize(2048);       //Generate the pair of keys       KeyPair pair = keyPairGen.generateKeyPair();       //Getting the privatekey ... Read More

Java Signature getProvider() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:38:17

104 Views

The provider for the signature object can be obtained using the method getProvider() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          Provider provider = signature.getProvider();          System.out.println("The Provider is: " + provider);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!!! NoSuchAlgorithmException");       }    } }OutputThe Provider is: SunRsaSign version 11ExampleLet us now see another example −import ... Read More

The abstract keyword in Java

AmitDiwan
Updated on 23-Sep-2019 12:35:35

238 Views

A class which contains the abstract keyword in its declaration is known as abstract class. Abstract classes may or may not contain abstract methods, i.e., methods without body. But, if a class has at least one abstract method, then the class must be declared abstract.If a class is declared abstract, it cannot be instantiated. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.Let us see a simple example −Exampleabstract class CricketLeague {    abstract void ... Read More

Traverse through a HashSet in Java

AmitDiwan
Updated on 23-Sep-2019 12:33:48

617 Views

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage.A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.To traverse through a HashSet, you can use the Iterator in Java. At first, create a HashSet with string values −HashSet hashSet = new HashSet(); hashSet.add("Jack"); hashSet.add("Tom"); hashSet.add("David"); hashSet.add("John"); hashSet.add("Steve");Now, traverse using Iterator −Iterator iterator = hashSet.iterator(); while(iterator.hasNext()){    System.out.println(iterator.next()); }Let us see a simple example wherein we have some HashSet elements and we ... Read More

Advertisements