• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Map.Entry & getKey(), getValue()

 
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line #2 and #3, how does the interface Map.Entry know what implementation to use for getKey() and getValue()?
getKey() and getValue() are abstract methods inside the interface Map.Entry.

 
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of java are you using?  That code does not compile with java 8.
It does compile with java 14
 
henry leu
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What version of java are you using?  That code does not compile with java 8.
It does compile with java 14



I'm using Java 15.
 
Saloon Keeper
Posts: 23263
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Map.entry() takes 2 positional parameters. The first one is the key, the second one is the value to be associated with it.

As for "which implementation to use", Map.Entry is an Interface, not a class. So for the logic to actually function, you need to have a concrete class instance implementing it, and that concrete instance is responsible for the method implementations, either directly, or, in the case of building on an abstract mapping class, by the parent class.
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see what class is involved, add this print statement after line 1:
That class has the implementations for the methods you are asking about.
 
henry leu
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because they are abstract classes, I would like to know where are their implementations? Where can I find the implementations?
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look in the src.zip file in the JDK's lib folder
 
Marshal
Posts: 26381
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Look in the src.zip file in the JDK's lib folder



Or alternatively, look in the API documentation, if that's what you meant.
 
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . look in the API documentation . . .

That class doesn't show up; it must have default/package private access.
 
Paul Clapham
Marshal
Posts: 26381
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . look in the API documentation . . .

That class doesn't show up; it must have default/package private access.



Which just underlines the fact that when you use an interface as the type of a variable, it's unnecessary to ask what specific implementation will be used when the code is run. The API documentation for that interface provides a description of what its methods should do, and that's what the methods of the class which implements the interface will do.

Unless some smart guy decides to write an implementation of Map which does some unusual things. Then you might need to get hold of that smart guy and read their code. But there's no need to read the code for the standard API. Not to find out what it does, anyway.
 
Marshal
Posts: 3408
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:Where can I find the implementations?


Is this what you are looking for?
 
Campbell Ritchie
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's it, and it says in the /** comments */ that it isn't a public class and to create instances with the factory method shown in the first post.
Where is that Entry class used? It isn't used by HashMap because its constructor won't accept nulls. It also seems to be recent, if it uses Objects.requireNonNull. Yes, the @since tag says 9. The documentation comment also says,

...
. . . immutable . . .
...
This class differs from AbstractMap.SimpleImmutableEntry in the following ways: it is not serializable, it is final, and its key and value must be non-null.
...

It isn't truly immutable because it fails to take defensive copies of its K and V.
 
Tim Holloway
Saloon Keeper
Posts: 23263
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:Because they are abstract classes, I would like to know where are their implementations? Where can I find the implementations?



Just because something's an abstract class doesn't change how implementations are found.

Anyway, I think it was pretty obvious that you didn't want to know the location of the implementations as much as you wanted to know what their logic was.
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic