LinkedHashMap missing?

Perhaps this is another case of user error.

Even if I “import java.util” I am told by the IDE that “the class LinkedHashMap does not exist”.

Why?

When I try that on my system (macos) I get this:
Only a type can be imported. java.util resolves to a package

1 Like

Just import java.util; is incomplete b/c it’s only the package part.

In order to actually import class LinkedHashMap, we have to include it after the package path:
import java.util.LinkedHashMap;

Alternatively, though not a good practice, we can import the whole “java.util” package via *:
import java.util.*;

For more type flexibility, it’s advisable to declare a variable w/ the corresponding interface type instead; for this case a Map:

import java.util.Map;
import java.util.LinkedHashMap;

public final Map<String, String> dict = new LinkedHashMap<>();

By doing so, your LinkedHashMap can be more easily passed around to other functions as if it were any other Map; but still keeping its features.

3 Likes

BtW, if your Map’s generics are <String, String>, <String, Int> or <String, Float>, Processing already got us covered w/ simpler alternative container classes w/ even more useful features like sorting:

4 Likes

Thanks for a good answer! I had actually tried both of these (individually) with the same error:

import java.util.*;
import java.util.LinkedHashMap;

I had also tried restarted Processing but had not rebooted my system until today. And… now it works!

Addendum:
Turned out that what I wanted was best served by a custom class, since I needed key-value pairs but also an identifier that I could use to group them. In many ways, a class is simpler than remembering all the different nomenclature for maps and composites.