Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Streams: IntStream to String [duplicate]

In Java 8 streams API, calling chars() on any String object returns an IntStream object containing all the characters.

What would be the correct way to convert the returned IntStream object back to a String? Calling toArray() would give me an int[], which is not accepted by any of the String constructor.

like image 884
yyoon Avatar asked Feb 02 '15 15:02

yyoon


People also ask

How do I find duplicates in a string in Java 8?

In Java 8 Stream, filter with Set. Add() is the fastest algorithm to find duplicate elements, because it loops only one time. Set<T> items = new HashSet<>(); return list. stream() .

What is the function used to duplicate a stream?

CopyTo(Stream) Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.

How do you remove duplicate characters in a string in Java using streams?

Java 8 Streams - Remove Duplicate Characters From String stream() method converts List<String> into Stream<String>. Next, distinct() method deletes all duplicates strings from it. Here, distinct() method is a intermediate operation as result Stream<String> is returned.

How do I filter duplicates in Java 8?

You can use the Stream. distinct() method to remove duplicates from a Stream in Java 8 and beyond. The distinct() method behaves like a distinct clause of SQL, which eliminates duplicate rows from the result set.

How to convert an InputStream to string in Java 8 stream?

The tutorial show you many ways to convert an InputStream to String, including using Java 8 Stream. 1. Using IOUtils 2. Using CharStreams 3. Using Scanner 4. Using StringWriter 5. Using ByteArrayOutputStream 6. Using BufferedInputStream 7. Using Java 8 Stream The method: java.io.BufferedReader.lines () returns a Stream . II.

How to get streams in Java?

Using CharStreams 3. Using Scanner 4. Using StringWriter 5. Using ByteArrayOutputStream 6. Using BufferedInputStream 7. Using Java 8 Stream The method: java.io.BufferedReader.lines () returns a Stream . II.

How do you convert an InputStream to a bytearrayoutputstream?

Finally, let's look at another plain Java example, this time using the ByteArrayOutputStream class: In this example, first, the InputStream is converted to a ByteArrayOutputStream by reading and writing byte blocks, then the OutputStream is transformed to a byte array, which is used to create a String.

Is it possible to duplicate an input stream in Java?

It is not possible in general. If you want to duplicate an input stream, or input iterator, you have two options: Suppose you duplicate a stream into two streams s1 and s2. If you have advanced n1 elements in s1 and n2 elements with s2, you must keep |n2 - n1| elements in memory, just to keep pace.


1 Answers

using StringBuilder's appendCodePoint method would do the trick as well,

IntStream in = "Convert me to a String".codePoints();  String intStreamToString = in.collect(StringBuilder::new,         StringBuilder::appendCodePoint, StringBuilder::append)         .toString();  System.out.println(intStreamToString); 
like image 157
Sufiyan Ghori Avatar answered Sep 20 '22 06:09

Sufiyan Ghori