You can collect the entries into a single String as follows:

  String sorted =
        map.entrySet().stream()
           .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
           .map(e-> e.getKey().toString() + "=" + e.getValue().toString())
           .collect(Collectors.joining (","));
Answer from Eran on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › java inputstream to string
Java InputStream to String | Baeldung
January 5, 2024 - In this tutorial, we’ll look at how to convert an InputStream to a String. We’ll start by using plain Java, including Java8/9 solutions, and then look into using the Guava and Apache Commons IO libraries as well.
🌐
Java Guides
javaguides.net › 2024 › 09 › how-to-convert-stream-to-string-in-java.html
How to Convert Stream to String in Java 8
September 10, 2024 - Step 1: A stream of strings is created using Stream.of(). Step 2: The Collectors.joining() method is used to concatenate the elements without any delimiter. Step 3: The resulting string is printed. import java.util.stream.Stream; import ...
🌐
Baeldung
baeldung.com › home › java › java string › converting string to stream of chars
Converting String to Stream of chars | Baeldung
January 8, 2024 - Java 8 introduced the Stream API, with functional-like operations for processing sequences. If you want to read more about it, have a look at this article. In this quick article, we’ll see how to convert a String to a Stream of single characters.
🌐
Coderwall
coderwall.com › p › im4lja › joining-objects-into-a-string-with-java-8-stream-api
Joining Objects into a String with Java 8 Stream API (Example)
July 17, 2023 - #string · #java · #join · #stream · You can leverage Java 8 Collectors to concatenate some objects into a string separated by a delimiter: For example: List<Integer> numbers = Arrays.asList( 4, 8, 15, 16, 23, 42 ); return numbers.stream() ...
🌐
Baeldung
baeldung.com › home › java › java streams › string operations with java streams
String Operations with Java and Stream API | Baeldung
January 8, 2024 - Java 8 has introduced a new Stream API that lets us process data in a declarative manner. In this quick article, we would learn how to use the Stream API to split a comma-separated String into a list of Strings and how to join a String array into a comma-separated String.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-outputstream-to-string
Java Program to Convert OutputStream to String - GeeksforGeeks
July 23, 2025 - // Java program to demonstrate conversion // from outputStream to string import java.io.*; class GFG { // we know that main will throw // IOException so we are ducking it public static void main(String[] args) throws IOException { // declaring ByteArrayOutputStream ByteArrayOutputStream stream = new ByteArrayOutputStream(); // Initializing string String st = "Hello Geek!"; // writing the specified byte to the output stream stream.write(st.getBytes()); // converting stream to byte array // and typecasting into string String finalString = new String(stream.toByteArray()); // printing the final string System.out.println(finalString); } } Output ·
🌐
Vultr Docs
docs.vultr.com › java › examples › convert inputstream to string
Java Program to Convert InputStream to String
April 10, 2025 - Here, IOUtils.toString method is directly used to convert the InputStream into a String specifying the character encoding explicitly. It's crucial to close the stream afterwards to avoid memory leaks.
Find elsewhere
🌐
Level Up Lunch
leveluplunch.com › java › examples › convert-java-util-stream-to-string
Convert stream to String - Java
July 10, 2014 - @Test public void stream_to_set() { String streamToString = Stream.of("a", "b", "c") .collect(joining()); assertEquals("abc", streamToString); }
🌐
Sentry
sentry.io › sentry answers › java › how do i read / convert an inputstream into a string in java?
Java Convert InputStream to String | Sentry
September 3, 2026 - Where only older versions of Java are available, the quickest solution is to read the InputStream in and then write this data to an OutputStream to produce the final String result.
Top answer
1 of 16
3601

To summarize the other answers, I found 11 main ways to do this (see below). And I wrote some performance tests (see results below):

Ways to convert an InputStream to a String:

  1. Using IOUtils.toString (Apache Utils)

     String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    
  2. Using CharStreams (Guava)

     String result = CharStreams.toString(new InputStreamReader(
           inputStream, Charsets.UTF_8));
    
  3. Using Scanner (JDK)

     Scanner s = new Scanner(inputStream).useDelimiter("\\A");
     String result = s.hasNext() ? s.next() : "";
    
  4. Using Stream API (Java 8). Warning: This solution converts different line breaks (like \r\n) to \n.

     String result = new BufferedReader(new InputStreamReader(inputStream))
       .lines().collect(Collectors.joining("\n"));
    
  5. Using parallel Stream API (Java 8). Warning: This solution converts different line breaks (like \r\n) to \n.

     String result = new BufferedReader(new InputStreamReader(inputStream))
        .lines().parallel().collect(Collectors.joining("\n"));
    
  6. Using InputStreamReader and StringBuilder (JDK)

     int bufferSize = 1024;
     char[] buffer = new char[bufferSize];
     StringBuilder out = new StringBuilder();
     Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);
     for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
         out.append(buffer, 0, numRead);
     }
     return out.toString();
    
  7. Using StringWriter and IOUtils.copy (Apache Commons)

     StringWriter writer = new StringWriter();
     IOUtils.copy(inputStream, writer, "UTF-8");
     return writer.toString();
    
  8. Using ByteArrayOutputStream and inputStream.read (JDK)

     ByteArrayOutputStream result = new ByteArrayOutputStream();
     byte[] buffer = new byte[1024];
     for (int length; (length = inputStream.read(buffer)) != -1; ) {
         result.write(buffer, 0, length);
     }
     // StandardCharsets.UTF_8.name() > JDK 7
     return result.toString("UTF-8");
    
  9. Using BufferedReader (JDK). Warning: This solution converts different line breaks (like \n\r) to line.separator system property (for example, in Windows to "\r\n").

     String newLine = System.getProperty("line.separator");
     BufferedReader reader = new BufferedReader(
             new InputStreamReader(inputStream));
     StringBuilder result = new StringBuilder();
     for (String line; (line = reader.readLine()) != null; ) {
         if (result.length() > 0) {
             result.append(newLine);
         }
         result.append(line);
     }
     return result.toString();
    
  10. Using BufferedInputStream and ByteArrayOutputStream (JDK)

    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    for (int result = bis.read(); result != -1; result = bis.read()) {
        buf.write((byte) result);
    }
    // StandardCharsets.UTF_8.name() > JDK 7
    return buf.toString("UTF-8");
    
  11. Using inputStream.read() and StringBuilder (JDK). Warning: This solution has problems with Unicode, for example with Russian text (works correctly only with non-Unicode text)

    StringBuilder sb = new StringBuilder();
    for (int ch; (ch = inputStream.read()) != -1; ) {
        sb.append((char) ch);
    }
    return sb.toString();
    

Warning:

  1. Solutions 4, 5 and 9 convert different line breaks to one.

  2. Solution 11 can't work correctly with Unicode text

Performance tests

Performance tests for small String (length = 175), url in github (mode = Average Time, system = Linux, score 1,343 is the best):

              Benchmark                         Mode  Cnt   Score   Error  Units
 8. ByteArrayOutputStream and read (JDK)        avgt   10   1,343 ± 0,028  us/op
 6. InputStreamReader and StringBuilder (JDK)   avgt   10   6,980 ± 0,404  us/op
10. BufferedInputStream, ByteArrayOutputStream  avgt   10   7,437 ± 0,735  us/op
11. InputStream.read() and StringBuilder (JDK)  avgt   10   8,977 ± 0,328  us/op
 7. StringWriter and IOUtils.copy (Apache)      avgt   10  10,613 ± 0,599  us/op
 1. IOUtils.toString (Apache Utils)             avgt   10  10,605 ± 0,527  us/op
 3. Scanner (JDK)                               avgt   10  12,083 ± 0,293  us/op
 2. CharStreams (guava)                         avgt   10  12,999 ± 0,514  us/op
 4. Stream Api (Java 8)                         avgt   10  15,811 ± 0,605  us/op
 9. BufferedReader (JDK)                        avgt   10  16,038 ± 0,711  us/op
 5. parallel Stream Api (Java 8)                avgt   10  21,544 ± 0,583  us/op

Performance tests for big String (length = 50100), url in github (mode = Average Time, system = Linux, score 200,715 is the best):

               Benchmark                        Mode  Cnt   Score        Error  Units
 8. ByteArrayOutputStream and read (JDK)        avgt   10   200,715 ±   18,103  us/op
 1. IOUtils.toString (Apache Utils)             avgt   10   300,019 ±    8,751  us/op
 6. InputStreamReader and StringBuilder (JDK)   avgt   10   347,616 ±  130,348  us/op
 7. StringWriter and IOUtils.copy (Apache)      avgt   10   352,791 ±  105,337  us/op
 2. CharStreams (guava)                         avgt   10   420,137 ±   59,877  us/op
 9. BufferedReader (JDK)                        avgt   10   632,028 ±   17,002  us/op
 5. parallel Stream Api (Java 8)                avgt   10   662,999 ±   46,199  us/op
 4. Stream Api (Java 8)                         avgt   10   701,269 ±   82,296  us/op
10. BufferedInputStream, ByteArrayOutputStream  avgt   10   740,837 ±    5,613  us/op
 3. Scanner (JDK)                               avgt   10   751,417 ±   62,026  us/op
11. InputStream.read() and StringBuilder (JDK)  avgt   10  2919,350 ± 1101,942  us/op

Graphs (performance tests depending on Input Stream length in Windows 7 system)

Performance test (Average Time) depending on Input Stream length in Windows 7 system:

 length  182    546     1092    3276    9828    29484   58968

 test8  0.38    0.938   1.868   4.448   13.412  36.459  72.708
 test4  2.362   3.609   5.573   12.769  40.74   81.415  159.864
 test5  3.881   5.075   6.904   14.123  50.258  129.937 166.162
 test9  2.237   3.493   5.422   11.977  45.98   89.336  177.39
 test6  1.261   2.12    4.38    10.698  31.821  86.106  186.636
 test7  1.601   2.391   3.646   8.367   38.196  110.221 211.016
 test1  1.529   2.381   3.527   8.411   40.551  105.16  212.573
 test3  3.035   3.934   8.606   20.858  61.571  118.744 235.428
 test2  3.136   6.238   10.508  33.48   43.532  118.044 239.481
 test10 1.593   4.736   7.527   20.557  59.856  162.907 323.147
 test11 3.913   11.506  23.26   68.644  207.591 600.444 1211.545
2 of 16
2762

A nice way to do this is using Apache Commons IOUtils to copy the InputStream into a StringWriter... Something like

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();

or even

// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding);

Alternatively, you could use ByteArrayOutputStream if you don't want to mix your Streams and Writers.

🌐
Medium
neesri.medium.com › string-to-a-stream-character-in-java-8-884c9b5e00f7
Convert String to a Stream<Character> in Java 8 | by A cup of JAVA coffee with NeeSri | Medium
July 20, 2025 - You can either consume the Stream<Character> using operations like forEach or collect the characters into a collection (e.g., List) using collect(Collectors.toList()). This approach allows you to work with the characters in the string using the functional programming capabilities provided by Java 8 Streams.
🌐
Stack Overflow
stackoverflow.com › questions › 26320910 › how-to-convert-a-string-to-a-java-8-stream-of-characters
How to convert a String to a Java 8 Stream of Characters? - Stack Overflow
I found this question about getting a java.util.streams.IntStream from a java String but I have not found this method now that I'm using Java 8. Correction: As you guys pointed, I was using Java 7...
🌐
Mkyong
mkyong.com › home › java › how to convert inputstream to string in java
How to convert InputStream to String in Java | mkyong.com
February 13, 2022 - 2.1 In Java 9, we can use the new InputStream#readAllBytes() API to convert the input stream to bytes, later we use the new String() to create a new String object.
🌐
DZone
dzone.com › data engineering › data › a complete guide on how to convert inputstream to string in java
How to Convert InputStream to String In Java - DZone
March 2, 2023 - We then use the useDelimiter() method to set the delimiter to \\A, which is a regular expression that matches the beginning of the input. This tells the Scanner to read the entire input stream as a single token. Finally, we return the token as a String.
🌐
MindStick
mindstick.com › forum › 159209 › how-can-i-read-convert-an-input-stream-into-a-string-in-java
How can I read/convert an Input Stream into a String in Java? – MindStick
July 21, 2023 - It then checks if there is a next token and returns it as a String. ... import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class InputStreamToString { public static String convertToString(InputStream inputStream) throws IOException { StringBuilder result = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = reader.readLine()) != null) { result.append(line).append("\n"); } } return result.toString(); } public static void main(String[] args) { // Example of usage InputStream inputStream = // your input stream here try { String result = convertToString(inputStream); System.out.println(result); } catch (IOException e) { e.printStackTrace(); } } }
🌐
Scaler
scaler.com › home › topics › java inputstream to string
Java InputStream to String - Scaler Topics
December 27, 2023 - With minimal conversion, support for input streams with vast amounts of data, preservation of the original line breaks, and compatibility with all Java versions, this ByteArrayOutputStream solution is the quickest way to convert an InputStream to a String.
🌐
LabEx
labex.io › tutorials › java-convert-inputstream-to-string-117396
Convert InputStream to String in Java | LabEx
Learn different ways to convert an InputStream to a String in Java, including using InputStreamReader and BufferedReader with StringBuilder.