Tagged: #stream

Java – Convert InputStream to String

This tutorial shows several ways to convert an InputStream to a String in Java. 1- BufferedReader The common way for generating a String out of an InputStream is through using BufferedReader as the following:

2- Scanner Another common way is to use a Scanner object as the following:

3- Java 8 With Java 8, you can generate a...

Java – Convert List to Array

This tutorial shows several ways to convert a List to Array in Java. 1- toArray() List provides a utility method called toArray() which accepts an empty array and populates it with the elements of the array list.

In the above example, we initialize an array with the same number of elements as the input list, then we populate it...

Java – Convert Array to List

This tutorial shows several ways to convert Array to List in Java. 1- Arrays.asList You can convert an Array to List using Arrays.asList() utility method. This method accepts an array as input and returns a list as output.

The limitation of using this method is that it returns a fixed size list. You can just read and overwrite its...

Java – Convert List to Map

This tutorial shows the different ways to convert a List to Map in Java. 1- Java 7 & before With Java 7 and older releases, the only way to convert a List to Map is to iterate over the list and populate the map manually. In the following example, we implement a utility method which accepts a list of Student...