flatmap

Java Stream collect after flatMap returns List<Object> instead of List<String>

烂漫一生 提交于 2019-12-10 21:14:51
问题 I tried the following code using Java 8 streams: Arrays.asList("A", "B").stream() .flatMap(s -> Arrays.asList("X", "Y").stream().map(s1 -> s + s1)).collect(Collectors.toList()); What I get is a List<Object> while I would expect a List<String> . If I remove the collect and I try: Arrays.asList("A", "B").stream().flatMap(s -> Arrays.asList("X", "Y").stream().map(s1 -> s + s1)); I correctly get a Stream<String> . Where am I wrong? Can someone help me? Many thanks in advance. Edit: The problem is

Usage of Spark DataFrame.flatMap in java

心已入冬 提交于 2019-12-10 18:47:27
问题 I have worked with RDD.flatMap function in java. Now trying my hands on DataFrames. They say: public <R> RDD<R> flatMap(scala.Function1<org.apache.spark.sql.Row, scala.collection.TraversableOnce<R>> f, scala.reflect.ClassTag<R> evidence$4) Returns a new RDD by first applying a function to all rows of this DataFrame, and then flattening the results. Specified by: flatMap in interface RDDApi But when I tried this, Function1 , is forcing me to override lots and lots of unimplemented methods.

PySpark flatmap should return tuples with typed values

蹲街弑〆低调 提交于 2019-12-10 12:18:34
问题 I'm using Jupyter Notebook with PySpark. Within that I have a have a dataframe that has a schema with column names and types (integer, ...) for those columns. Now I use methods like flatMap but this returns a list of tuples that have no fixed type anymore. Is there a way to achieve that? df.printSchema() root |-- name: string (nullable = true) |-- ... |-- ... |-- ratings: integer (nullable = true) Then I use flatMap to do some calculations with the rating values (obfuscated here): df.flatMap

How to assign value into a breeze Matrix in flatMap Scala-Spark?

倖福魔咒の 提交于 2019-12-08 13:37:08
问题 i want to initialize a matrix using data in flatMap , this is my data: -4,0,1.0 ### horrible . not-work install dozen scanner umax ofcourse . tech-support everytime call . fresh install work error . crummy product crummy tech-support crummy experience . 2,1,1.0 ### scanner run . grant product run windows . live fact driver windows lose performance . setup program alert support promptly quits . amazon . website product package requirement listing compatible windows . 1,2,1.0 ### conversion kit

how use Optional with cascaded objects not created with Optional

拥有回忆 提交于 2019-12-08 12:37:29
问题 Context: I have chained objects generated by wsdl2java so none of them contains java.util.Optional. There is an already created method that call the soap web services, receives the xml and unmarshalling it in cascaded objects. Desire: I want to use Optional in order to avoid null tests. I have already posted my dificults to accomplish it (how to use optional and filter along to retrieve an object from chained/cascaded objects generated by wsdl2java) but, after 3 days searching and reading I

How to use the 'flatmap' operator conditionally ? (Angular 2/rxjs)

眉间皱痕 提交于 2019-12-06 05:16:32
问题 What I'm trying to achieve is to run a series of observables conditionally. return observable.map(response => response) .flatmap(response1 => observable1(response1)) .flatmap(response2 => observable2(response2)) .flatmap(response3 => observable3(response3)) I need to check the response1 and invoke the remaining observables if needed, else I need to return response1 and break the execution and so on. I've gone through the following SO questions but they doesn't seem to answer my question

第十章 Scala 容器基础(十六):flatMap一个把flatten和map结合起来的方法

爷,独闯天下 提交于 2019-12-05 06:55:24
Problem 当你第一次来到Scala世界的时候,flatMap放方法看起来是很特别的,所以你需要理解他是如何工作的,还有它是应用在什么地方的。 Solution 在你调用map方法后紧接着调用flatten方法的情况下,你就可以尝试调用flatMap来解决这个问题了。满足如下情况: 使用map方法来从原集合基础上创建一个新的集合 map方法的返回结果是一个嵌套集合,或者元素为Some和None 你在map方法后紧接着调用了flatten方法 如果你的程序正好符合上面的情况,那么你就是可以使用flatMap来代替collection.map.flatten了 下面这个例子中,你会看到如何使用flatMap来处理一个Option。在这个例子中,我们要计算集合中数字类型元素的和。又一个问题:数字都是字符串类型的,并且好多元素并不是数字,也不能转换为Int型。先来看下这个集合: scala> val bag = List("1", "2", "three", "4", "one hundred seventy five") bag: List[String] = List(1, 2, three, 4, one hundred seventy five) 为了解决这个问题,你开始创建一个字符串到整形的转换函数来返回Some[Int]或者None: scala> def toInt(in

Swift: Lazily encapsulating chains of map, filter, flatMap

自古美人都是妖i 提交于 2019-12-04 22:35:19
问题 I have a list of animals: let animals = ["bear", "dog", "cat"] And some ways to transform that list: typealias Transform = (String) -> [String] let containsA: Transform = { $0.contains("a") ? [$0] : [] } let plural: Transform = { [$0 + "s"] } let double: Transform = { [$0, $0] } As a slight aside, these are analogous to filter (outputs 0 or 1 element), map (exactly 1 element) and flatmap (more than 1 element) respectively but defined in a uniform way so that they can be handled consistently.

Using flatMap in RxJava 2.x

时光怂恿深爱的人放手 提交于 2019-12-04 09:03:06
I'm working with an API of my own and a i'm hoping to chain a few paginated results using RxJava. I use cursor based pagination. (imagine there are 50 users in this first request): { "data":{ "status":"ok", "total":988, //users total "has_next_page":true, "end_cursor":"AQAxd8QPGHum7LSDz8DnwIh7yHJDM22nEjd", "users":[{"id":"91273813", "username":"codergirl", "full_name":"Code Girl", "picture_url":"https://cdn.com/21603182_7904715668509949952_n.jpg", }, ... ] } } Right now, I'm getting the first 50 results like this, using retrofit: public class DataResponse { @SerializedName("end_cursor")

Why is this usage of Stream::flatMap wrong?

徘徊边缘 提交于 2019-12-03 23:39:36
问题 I expected to be able to use Stream::flatMap like this public static List<String> duplicate(String s) { List<String> l = new ArrayList<String>(); l.add(s); l.add(s); return l; } listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList()); But I get the following compiler error Test.java:25: error: incompatible types: cannot infer type-variable(s) R listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList()); (argument mismatch; bad return type in