darklord
darklord

Reputation: 5167

Package private visibility related change in java 8?

In an Android app I changed the java compiler to java 8 then the following piece of code stopped working:

final Map<String, String> allRecords = new LinkedHashMap<String, String>() {
    @Override
    protected boolean removeEldestEntry(Entry eldest) {
        return size() > 10;
    }
};

It says: Entry is not public in LinkedHashMap; cannot be accessed from outside package

However it compiles originally. I am not aware of any package private visibility related change in java 8. Is there any reference to this behavior change in java 8?

Upvotes: 0

Views: 145

Answers (2)

younes
younes

Reputation: 832

You must take the map from the Entry

final Map<String, String> allRecords = new LinkedHashMap<String, String>() {
    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > 10;
    }
};

this line Map.Entry

Changing the package and folder caused this

Upvotes: 0

Wayne
Wayne

Reputation: 343

You're referencing LinkedHashMap.Entry, which is private in LinkedHashMap, according to that errror. Using Map.Entry instead will resolve this issue, although I'm unsure why changing Java platforms would affect this as the access should be the same in all versions.

By looking into the API documentation, Entry is not declared in LinkedHashMap, whereas it is in Map, so this may simply be a change to the hierarchy loading in java 8, Although this may be incorrect.

See Documents... https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html, https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html

Upvotes: 3

Related Questions