Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Java Stream to a Scala Stream?

As a part of an effort of converting Java code to Scala code, I need to convert the Java stream Files.walk(Paths.get(ROOT)) to Scala. I can't find a solution by googling. asScala won't do it. Any hints?

Here is the related code:

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

// ...snip...

Files.walk(Paths.get(ROOT))
     .filter(path -> !path.equals(Paths.get(ROOT)))
     .map(path -> Paths.get(ROOT).relativize(path))
     .map(path -> linkTo(methodOn(FileUploadController.class).getFile(path.toString())).withRel(path.toString()))
     .collect(Collectors.toList()))

The Files.walk(Paths.get(ROOT)) return type is Stream<Path> in Java.

like image 914
TeeKai Avatar asked Jul 12 '16 21:07

TeeKai


People also ask

How do I convert a stream to an array in Java?

Convert Stream into an IntStream using Stream. mapToInt() method. Convert the obtained IntStream into int[] using toArray() The obtained array is of type Integer.

How do you define a stream in Scala?

In scala a List can be constructed with :: operator, whereas a Stream can be constructed with the #:: operator method, using Stream. empty at the end of the expression. In above syntax the head of this stream is 1, and the tail of it has 2 and 3.

What is Strem in Java?

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are – A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.

Can you reuse a Java stream?

1. Can we reuse stream? No. Java streams, once consumed, can not be reused by default.


2 Answers

There is a slightly nicer way without needing the compat layer or experimental 2.11 features mentioned here by @marcospereira

Basically just use an iterator:

import java.nio.file.{Files, Paths}
import scala.collection.JavaConverters._

Files.list(Paths.get(".")).iterator().asScala
like image 103
mirosval Avatar answered Oct 19 '22 03:10

mirosval


Starting Scala 2.13, the standard library includes scala.jdk.StreamConverters which provides Java to Scala implicit stream conversions:

import scala.jdk.StreamConverters._

val javaStream = Files.walk(Paths.get("."))
// javaStream: java.util.stream.Stream[java.nio.file.Path] = java.util.stream.ReferencePipeline$3@51b1d486
javaStream.toScala(LazyList)
// scala.collection.immutable.LazyList[java.nio.file.Path] = LazyList(?)
javaStream.toScala(Iterator)
// Iterator[java.nio.file.Path] = <iterator>

Note the usage of LazyList (as opposed to Stream) as Streams are deprecated in Scala 2.13. LazyList is the supported replacement type.

like image 26
Xavier Guihot Avatar answered Oct 19 '22 02:10

Xavier Guihot