-2

I have problem importing java.util.stream.*;
Compiling my code gives me a stream()

"cannot find symbol error".

This is my import list

import java.util.stream.*;
import java.util.*;
import java.lang.String;
import java.util.Arrays;
import java.nio.file.*;
import java.io.IOException;  

and this is the code i'm compiling

List<Beverage> l = cantine.stream()
                                .filter(p -> p.name.equals(nam))
                                .collect(Collectors.toList());

IMPORTANT: I do know what a "cannot find symbol error" is, so please do not blindly close this question.

full code for reference

import java.util.stream.*;
import java.util.*;
import java.lang.String;
import java.util.Arrays;
import java.nio.file.*;
import java.io.IOException;

public class Enoteca{

    Map<String,Beverage> cantine;

    public Enoteca(){
        this.cantine = new HashMap<String,Beverage>();
    }


    public List<Beverage> byName(String nam){

        List<Beverage> l = cantine.stream()
                                .filter(p -> p.name.equals(nam))
                                .collect(Collectors.toList());
    }

    public static void main(String[] args){

        Enoteca e = new Enoteca();

        for(String s: args){
            Beverage b = new Beverage(s,"1987");
            e.cantine.put(s,b);
        }
        System.out.println(e.cantine);
    }
}

class Beverage{

    String name;
    String year;

    public Beverage(String name,String year){
        this.name = name;
        this.year = year;
    }

    public String getName(){
        return name;
    }

    @Override
    public String toString(){
        return name + " " +year;
    }
}
Jens
  • 67,715
  • 15
  • 98
  • 113
toploz
  • 21
  • 1
  • 4
  • How are you compiling, mention the details? Using `javac` or any framework or any `IDE`? Mention their version details. What is the type of `cantine`? – Naman Jun 30 '20 at 19:37
  • Which IDE you are using? are you making the correct configuration in your IDE? – Youcef LAIDANI Jun 30 '20 at 19:37
  • What is the type of `cantine`? – Jacob G. Jun 30 '20 at 19:37
  • Sorry, uploaded full code for reference. I'm using javac version 1.0.8_252 – toploz Jun 30 '20 at 19:46
  • 8
    `Map` does not have a `stream()` method. The Collections returned by a Map’s keySet, values, and entrySet methods do, but Map itself does not. – VGR Jun 30 '20 at 19:48
  • 2
    @VGR, that sounds like it should be an answer! – Louis Wasserman Jun 30 '20 at 19:48
  • Duplicate of [What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – Naman Jun 30 '20 at 19:50
  • @VGR thanks! I fixed it by calling stream on the values set as suggested. – toploz Jun 30 '20 at 20:10

1 Answers1

8

The compiler is correct. Map does not have a stream() method. The Collections returned by a Map’s keySet, values, and entrySet methods do, but Map itself does not.

Since you want a List<Beverage>, I’m guessing you want cantine.values().stream().

VGR
  • 40,506
  • 4
  • 48
  • 63
  • 3
    It’s worth mentioning that contrary to the OP’s assumption, the inclusion of an `import java.util.stream.*;` statement is entirely irrelevant. Likewise, as a side note, there is no reason to include `import java.lang.String;`. – Holger Jul 01 '20 at 11:45
  • i Included all of the above imports because a previous thread was closed by a moderator telling me to include java.util.stream.*; even tho it was irrelevant i mentioned i included it just to avoid the thread being closed another time without an answer. – toploz Jul 12 '20 at 17:10