Search code examples
javadictionarybluej

How to create a map instance variable


I've been trying to create a class that will model a scenario I've come up with. It will involve a map with string keys and values.

I need to create an instance variable used to reference the map object, and a constructor that creates the empty map and assigns it to the map instance variable.

I've been messing around with map objects but not creating a class using them, and I've hit a mental block!

What's the proper way to actually get a map object?


Solution

  • public class TheClass {    
    
      private Map<String, String> theMap;
    
      public TheClass() {
        theMap = new HashMap<>();
      }
    }