background img
Aug 9, 2020
Comments Off on MapIterable.getOrDefault() : New however not so new API
0 0

MapIterable.getOrDefault() : New however not so new API

Written by
Nikhil Nanivadekar
Sundown at Port Hardy (June 2019)

Eclipse Collections comes with it’s personal Listing, Set, and Map implementations. These implementations lengthen the JDK Listing, Set, and Map implementations for simple interoperability. In Eclipse Collections, I launched a brand new API . In Java eight, was launched, so what makes it a brand new API for Eclipse Collections Technically, it’s new however not so new API! Take into account the code snippets beneath, previous to Eclipse Collections

MutableMap.getOrDefault() compiles and works superb
ImmutableMap.getOrDefault() doesn’t compile

As you may see within the code, has accessible, nonetheless doesn’t have it. However there is no such thing as a purpose why mustn’t have this read-only API. I discovered that already had which has the identical conduct. Then why did I nonetheless add to ?

I added primarily for simple interoperability. Firstly, most Java builders will pay attention to the technique, solely Eclipse Collections customers would pay attention to . By offering the API similar because the JDK it reduces the need to study a brand new API. Secondly, although is offered on , it’s not accessible on the very best interface of Eclipse Collections. Thirdly, I obtained to study a Java compiler test which I had not skilled earlier than. I’ll elaborate this test a bit extra intimately as a result of I discover it attention-grabbing.

After I added to , numerous interfaces in Eclipse Collections began giving compiler errors with messages like: . This I assumed was cool, as a result of at compile time, the Java compiler is guaranteeing that if there’s an API with default implementation in a couple of interface in a multi-interface situation, then Java is not going to resolve which implementation to choose however reasonably throw compiler errors. Therefore, Java ensures at compile time that there is no such thing as a ambiguity concerning which implementation might be used at runtime. How superior is that?!? With the intention to repair the compile time errors, I had so as to add a default implementations on the interfaces which gave the errors. I at all times consider in Compiler Errors are higher than Runtime Exceptions.

Stuart Marks has put collectively an superior weblog which covers the specifics of such eventualities. I recommend studying that for in-depth understanding of how and why this conduct is noticed.

Put up Eclipse Collections the beneath code samples will work:

MapIterable.getOrDefault() compiles and works superb
MutableMap.getOrDefault() compiles and works superb
ImmutableMap.getOrDefault() compiles and works superb

Eclipse Collections was launched on 08/08/2020 and is one in every of our most function packed releases. The discharge constitutes quite a few contributions from the Java neighborhood.

I’m a Undertaking Lead and Committer for the Eclipse Collections OSS venture on the Eclipse Basis. Eclipse Collections is open for contributions.

Present your help star us on GitHub.

Article Categories:
Sunshine & Good Vibes | Swim to wear for those warm sunny days ahead! news WW

Comments are closed.