Found 7442 Articles for Java

Program to convert Stream to an Array in Java

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

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

How to serialize and de-serialize generic types using the Gson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 09:15:57

2K+ Views

If a Java class is a generic type, and we are using it with the Gson library for JSON serialization and deserialization, the TypeToken class from com.google.gson.reflect package is used for preserving the generic type information at runtime. What are Generic Types in Java? Generic types in Java mean we don't set any method or class to a specific type, like int, String, etc. Instead, we use a type parameter (like T) to represent the type. This allows us to create classes and methods that can operate on objects of different types, and also provides compile-time safety at the same time. ... Read More

Program to convert Set to List in Java

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

265 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
Updated on 23-Sep-2019 14:02:02

483 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
Updated on 23-Sep-2019 14:00:16

254 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
Updated on 23-Sep-2019 13:57:11

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

Program to convert Milliseconds to Date Format in Java

AmitDiwan
Updated on 23-Sep-2019 13:55:13

1K+ Views

Let us first declare and initialize milliseconds value variable −long milliSeconds = 656478;Now, convert Milliseconds to Date format −DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z"); Date date = new Date(milliSeconds);Following is the program to convert Milliseconds to Date Format in Java −Exampleimport java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Demo {    public static void main(String args[]) {       long milliSeconds = 656478;       DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");       Date date = new Date(milliSeconds);       System.out.println(dateFormat.format(date));    } }Output01 Jan 1970 00:10:56:478 +0000

Program to convert List to Stream in Java

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

839 Views

Here’s our List with string values −List myList = Arrays.asList("One", "Two", "Three", "Four", "Five");Now, let us convert this List to Stream −Stream s = myList.stream();Following is the program to convert List to Stream in Java −Exampleimport java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       List myList = Arrays.asList("One", "Two", "Three", "Four", "Five");       System.out.println("List: " + myList);       Stream s = myList.stream();       System.out.println("Stream (List to Stream) = " + Arrays.toString(s.toArray()));    } }OutputList: [One, Two, Three, Four, Five] Stream (List to Stream) ... Read More

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

AmitDiwan
Updated on 23-Sep-2019 12:58:22

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
Updated on 23-Sep-2019 12:56:52

778 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

Advertisements