-7
HashMap<String,Object> other = new HashMap<>();
new ArrayList<String>().stream().map(s -> new HashMap<String, Object>() {{
  }}).findFirst().orElse(other);
Stream.generate(() -> new HashMap<String, Object>()).findFirst().orElse(other);

The second statement of the code has a compilation error

enter image description here

Please tell me where there is a problem

Holger
  • 285,553
  • 42
  • 434
  • 765
wysnxzm
  • 1
  • 1
  • 6
    Please don't link to pictures of code, include the code itself in your post. – daniu Jun 08 '20 at 13:14
  • 1
    You probably don't have your JDK set properly, or you have defined your own `String` and `Object` classes – user Jun 08 '20 at 13:18
  • I did not use other String and Object, JDK is also set correctly – wysnxzm Jun 08 '20 at 13:38
  • 1
    Read the answers to [What is Double Brace initialization in Java?](https://stackoverflow.com/q/1958636/2711488), especially the reasons why you shouldn’t use it. One of the reasons will guide you to your current problem. – Holger Jun 09 '20 at 12:47

1 Answers1

0

In the second statement, new HashMap<String, Object>() {{ }} statement creates an anonymous inner class. The result is not a direct instance of HashMap<String, Object> anymore, but a class derived from it.

You should remove the curly brackets:

HashMap<String,Object> other = new HashMap<>();
new ArrayList<String>().stream().map(s -> new HashMap<String, Object>()).findFirst().orElse(other);
Kartal Tabak
  • 760
  • 1
  • 7
  • 18