1

I am working on a big project. So, it's not possible to copy the whole code here. But my problem is that by evaluating an expression, I can try the following code and I get true back:

((HashMap.Node)((HashMap)((FiFiClass)objectTerm.getValue()).getFiFiObjects()).entrySet().toArray()[0]).getKey().equals(FiFiObjectType.AAA)

However, when I copy exactly the same code in my code, Node is in red color and when I write HashMap., I don't get Node as a possible extension of the phrase. Can anyone help me please?

I am usingJava8.

Thanks in advance,

user1419243
  • 1,655
  • 3
  • 19
  • 33

1 Answers1

3

It's package-private (more formally known as "default access"). That means it's a package implementation detail, and not available to classes outside of the package. See the source code.

If you're just trying to get the first key (keeping in mind that HashMaps aren't ordered, so "first" is really "some random key"), then you can just use keySet().iterator().next(). And if you're not positive that there is at least one key, you should probably store that Iterator as a local variable, and then call hasNext() before you call next().

If you're trying to figure out whether the key set contains FiFiObjectType.AAA, then you should just call ... getFiFiObjects().containsKey(FiFiObjectType.AAA) (javadoc).

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Thanks a lot. It helped. As I have just one element in the HashMap in this specific case, it was also enough to check the first element. – user1419243 Mar 07 '19 at 08:56
  • 2
    For completeness, the internal package-private class implements the [`Map.Entry`](https://docs.oracle.com/javase/8/docs/api/?java/util/Map.Entry.html) interface, which is the element type of the `entrySet()`, in case it’s ever needed (not when you’re interested in the key only). So you also can copy the entry set into a `Map.Entry[]` array or a `List`, to get a fixed order for subsequent processing, but it will work much smoother whens not using *raw types*… – Holger Mar 07 '19 at 10:20