82167

Java Stream collect after flatMap returns List instead of List

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.

<strong>Edit:</strong>

The problem is due to Eclipse (now using Kepler SR2 with java 8 patch 1.0.0.v20140317-1956). The problem does non appear if compiling using javac or, as commented by Holger, using Netbeans

Answer1:

Type inference is a new feature. Until tools and IDEs are fully developed I recommend using explicitly typed lambdas. There ware cases where Eclipse even crashed if an explicit cast was missing, but that is fixed now.

Here's a workaround:

With a typed "s1":

asList.stream() .flatMap(s -> Arrays.asList("X", "Y").stream().map((String s1) -> s + s1)) .collect(Collectors.toList());

Or with a genric parameter:

asList.stream() .flatMap(s -> Arrays.asList("X", "Y").stream().<String>map(s1 -> s + s1)) .collect(Collectors.toList());

The same is true if you add the parameter before flatMap instead of map.

But I suggest you use s::concat:

asList.stream() .flatMap(s -> Arrays.asList("X", "Y").stream().map(s::concat)) .collect(Collectors.toList());

Recommend

  • Glassfish Tools plugin for Eclipse Luna SR2 not working
  • Android Annotations and MonkeyTalk?
  • How do I set breakpoints using the Photran IDE in Eclipse?
  • Unzip archive in .Net CORE 1.0
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Multicolored edittext hint
  • Redux Form - Not able to type anything in input
  • Eloquent paginate function in Slim 3 project using twig
  • How can I sort a a table with VBA with given text condition?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Meteor: Do Something On Email Verification Confirmation
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Django: Count of Group Elements
  • All Classes Conforming to Protocol Inherit Default Implementation
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Fetching methods from BroadcastReceiver to update UI
  • Cross-Platform Protobuf Serialization
  • Where to put my custom functions in Wordpress?
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • R: gsub and capture
  • RestKit - RKRequestDelegate does not exist
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • How to disable jQuery.jplayer autoplay?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • JaxB to read class hierarchy
  • Sorting a 2D array using the second column C++
  • Net Present Value in Excel for Grouped Recurring CF