0

This is my method which used to populate a map, and there's other data will be added in the future.

public class myClass{
  public Map populateMap(client: RedissonClient) {
   Map map = client.getMap("map");
   map.put(k1, v1);
   map.put(k2, v2);
   ...
   
   return map;
 }
}

The problem is, each time when I calling this method in my main function, it will re-create the same map content again and again, is there a way to define it as a constant map object, not in a method.

Map map = myClass.populateMap(client);
...
Alex
  • 137
  • 1
  • 9

4 Answers4

2

From your question, it's not completely clear whether the map should belong to the instance or to the class.

To create a class constant map, use Map::of:

class MyClass {

    static final Map<String, String> MAP = Map.of("k1", "v1", "k2", "v2");

}

The abovementioned map is an unmodifiable map: it is guaranteed that no modifications could ever be made to the map1. If you want to get the contents of this map into a new map, in order to add additional data to it, just use

Map<String, String> newMap = new HashMap<>(MyClass.MAP);
newMap.put(...);

1 Well, perhaps it is possible with reflection. But for reflection applies: only use it if you know what you're doing.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

if you make the method static, you can initialize a static map:

public class MyClass{
  public static Map<String, String> map = populateMap();

  private static Map<String, String> populateMap() {
   Map<String, String> map = new HashMap<>();
   map.put("k1", "v1");
   map.put("k2", "v2");
   ...
  
   return map;
 }
}

now the populated MyClass.map is accessible to all the other classes

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • 3
    Note: with Map.of() resp. Map.ofEntries() there really isnt much need any more to have a helper method like that. Also note that you repeat the mistake of using raw types. Never do that. There are too many people literally copying code form this place, so make sure your code isnt getting such essential things as raw types wrong. – GhostCat Mar 31 '21 at 07:37
  • @GhostCat, fixed the example , thanks for the comment – Sharon Ben Asher Mar 31 '21 at 07:41
  • Well, thinking about the: the above wouldnt even compile, if `Map` is the java library interface. Worst case, the op has their OWN Map class without generics. Out of dubious questions, comes dubious code. ;-( – GhostCat Mar 31 '21 at 07:55
  • @SharonBenAsher, thanks for you answer, I edited the code, I'm using get() method in RedissonClient to get the map, what should the code be like now? – Alex Mar 31 '21 at 07:56
  • @Alex Please note: your question here has been answered. Please dont start asking more questions in comments. I rather suggest: step back, and pick up a good book. It almost feels like you want to do programming ... by just picking things from the internet, and instead of understanding **what** you are doing, you hope for people to just explain the *next* step to you. That is not a good strategy. You really have to **understand** what it means to have that field in your code. It matters whether other classes access that map in parallel. If the want to read it, or write, or both, ... – GhostCat Mar 31 '21 at 08:21
  • Meaning: in the end, there is **much** more to this than just "replacing a method call with a constant. – GhostCat Mar 31 '21 at 08:21
1

It is actually already said, make the map a global constant, or in java terms: a static final field.

public class myClass {
    private static final Map<String, String> MAP = new HashMap<>();
    static {
        MAP.put(k1, v1);
        MAP.put(k2, v2);
    }

Or since java 9 easier done as (thanks to @GhostCat):

    private static final Map<String, String> MAP =
        Map.of(k1, v1,
               k2, v2);

Use typing, like the interface Map<Integer, Object>. And for the creation pick the implementing class.

static { ... } is called a static initializer.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 3
    Eeeeg. It is 2021. At least mention that java **9** introduced Map.of() and Map.ofEntries() like years ago. https://www.concretepage.com/java/java-9/java-map-of-and-map-ofentries#of ... using static blocks is so 2005. – GhostCat Mar 31 '21 at 07:35
  • 1
    @GhostCat I wanted to demonstrate the correction of `Map map = new Map()`. But as shown by the readers' upvotes on your comment, one should use `Map.of.`. Good you commented. – Joop Eggen Mar 31 '21 at 08:09
-1

You can make it as a global variable like mentioned above.

public final static Map<String, String> MAP = populateMap();
Rohith V
  • 1,089
  • 1
  • 9
  • 23