0

this code is copied from a reference but it shows compilation error in Intellj:

Stream locales = Stream.of(Locale.getAvailableLocales());
            Map<String, Set<String>> countryToLanguages = locales.collect(
                    groupingBy(l -> l.getDisplayCountry(),
                            mapping(l -> l.getDisplayLanguage(),
                                    toSet())));

the compiler is seeing object named "l" and a java Object not as a locale; so it cann't understand the methods l.getDisplayCountry() and l.getDisplayLanguage().

also the Project SDK is java 8 and project language level is 8- lambdas, type annotations etc.

Ahmed Abbas
  • 118
  • 8
  • 4
    You are using a raw type, it should be `Stream locales`. – Alexis C. Oct 14 '16 at 15:56
  • 1
    Or just don’t store streams in local variables at all. There’s no benefit in doing that, it only open the possibility to accidentally using a Stream twice. Further, if you have an array rather than varargs, prefer `Arrays.stream` over `Stream.of`: `Map> countryToLanguages = Arrays.stream(Locale.getAvailableLocales()).collect( groupingBy(l -> l.getDisplayCountry(), mapping(l -> l.getDisplayLanguage(), toSet())));` – Holger Oct 14 '16 at 16:20
  • 2
    By the way, you can use method reference, then, there is no variable `l` to worry about its type: `Map> countryToLanguages = Arrays.stream( Locale.getAvailableLocales()) .collect( groupingBy(Locale::getDisplayCountry, mapping(Locale::getDisplayLanguage, toSet())));` – Holger Oct 14 '16 at 16:22
  • Possible duplicate of [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Alexis C. Oct 14 '16 at 17:39
  • @AlexisC. yes, i wrote this part and forget to specify the stream type. – Ahmed Abbas Oct 14 '16 at 23:13
  • @AlexisC. i think this question is about fixing a compilation problem not about the general rule of raw type. do you still think it's a duplicate/ – Ahmed Abbas Oct 14 '16 at 23:14
  • If you understand what a raw type is (which the duplicate is about), you'll understand why you have this compilation problem and how to fix it. – Alexis C. Oct 15 '16 at 08:30

1 Answers1

0

In intellij, You need to set Language Level (8) for each module as well. Project Structure -> Module -> Select Module -> Sources -> Language Level = 8

And if you want to change in POM directly

First- Add Properties

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

second- Add Plugin

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

Got from, thanks to Anuj stop IntelliJ IDEA to switch java language level everytime the pom is reloaded (or change the default project language level)

Manish
  • 1,452
  • 15
  • 25