Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to convert Set to List in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 323 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

Program to convert set of String to set of Integer in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 547 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

Program to convert Set of Integer to Set of String in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 309 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

Program to convert set of Integer to Array of Integer in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 369 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]

Read More

Program to convert List of String to List of Integer in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 1K+ Views

Here’s our List of String −List listString = Arrays.asList("25", "50", "75", "100", "125", "150");Now, convert the list of string to list of integer −List listInteger = listString.stream().map(Integer::parseInt).collect(Collectors.toList());Following is the program to convert List of String to List of Integer in Java −Exampleimport java.util.*; import java.util.stream.*; import java.util.function.*; public class Demo {    public static void main(String[] args) {       List listString = Arrays.asList("25", "50", "75", "100", "125", "150");       System.out.println("List of String = " + listString);       List listInteger = listString.stream().map(Integer::parseInt)          .collect(Collectors.toList());       System.out.println("List of Integer (converted from ...

Read More

Program to convert List of Integer to List of String in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 861 Views

Here’s our List of Integer −List listInteger = Arrays.asList(25, 50, 75, 100, 125, 150);Now, convert List of Integer to List of String −List listString = listInteger.stream()    .map(s -> String.valueOf(s))    .collect(Collectors.toList());Following is the program to convert List of Integer to List of String in Java −Exampleimport java.util.*; import java.util.stream.*; import java.util.function.*; public class Demo {    public static void main(String[] args) {       List listInteger = Arrays.asList(25, 50, 75, 100, 125, 150);       System.out.println("List of Integer = " + listInteger);       List listString = listInteger.stream()          .map(s -> String.valueOf(s))   ...

Read More

Ints contains() function in Java

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 4K+ 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
AmitDiwan
Updated on 23-Sep-2019 283 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

Java Signature toString() method with Examples

AmitDiwan
AmitDiwan
Updated on 23-Sep-2019 252 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
AmitDiwan
Updated on 23-Sep-2019 544 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
Showing 57321–57330 of 61,297 articles
Advertisements