Forum Stats

  • 3,722,780 Users
  • 2,244,412 Discussions
  • 7,850,084 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

stream generics

User_1LUYM
User_1LUYM Member Posts: 2 Red Ribbon
edited December 2016 in Java Lambda MOOC

in the code below I need to specify the type of Stream to String. But if I create an stream from an list of String should't the compiler have enough information.

    List<String> wordList = new ArrayList();

    Stream<String>wordStream;

    if (parallel) {

        wordStream = wordList.parallelStream();

    } else {

      wordStream = wordList.stream();

    }

User_1LUYMeudriscabrera-JavaNet

Answers

  • Manoel Campos
    Manoel Campos Member Posts: 1
    edited December 2016

    If you call directly wordList.stream() or wordList.parallelStream() the compiler will have enough information about the generic type.

    But once you declare a variable wordStream, you must inform the generic type.

    Only when you use Lambda expressions the compiler will infer the type.

    Inference of explicit declared variables outside a Lambda is just planned for future JDK versions.

    See https://youtu.be/oGll155-vuQ?t=8m25s  for an evolution of Type inference in Java and proposed new features for upcoming versions.

    User_1LUYMUser_1LUYM
  • User_1LUYM
    User_1LUYM Member Posts: 2 Red Ribbon
    edited December 2016

    thanks for the good answer, so waiting for "var" type

    eudriscabrera-JavaNet
Sign In or Register to comment.