0

So I am trying to create a text-based adventure/dungeon crawler game. I am trying to use a similar system to what I used in a text-based Pokemon game for the storage of items, monsters, etc. Here is my functional code for the storage system in C#.

public static Dictionary<string, pokemonTrainer> pokemonTrainers = new Dictionary<string, pokemonTrainer>()
        {
            {"Robert", new pokemonTrainer{name="Robert",typesOfPokemon={"Plant","Normal"}, levelMultiplier=0.4}},
            {"James", new pokemonTrainer{name="James",typesOfPokemon={"Plant","Normal","Water","Fire","Ice","Flying","Electric","Poison","Psychic","Fighting","Dark","Dragon","Fairy","Rock","Ghost","Ground","Bug"},levelMultiplier=0.8}}
        };

I would like to create my hashtable with certain key-value pairs inside of it like what is being done in the C# code instead of just adding them with .put() is this possible? If so how would I do it? And if not what would be the best way to create a storage system like this?

If it helps at all here's my declaration for the hashtable in java,

public static Hashtable<Integer, monster> monster_data_base = new Hashtable<Integer, monster>()
    {
        
    };

and here's the monster class I would like to be the value part of the key-value pairs.

class monster
    {
        private int health;
        private int damage;
        private String name;
        private int level;
        monster(int health, int damage, String name, int level)
        {
            this.health=health;
            this.damage=damage;
            this.name=name;
            this.level=level;
        }
        String get_name() 
        {
            return name;
        }
        int get_health() 
        {
            return health;
        }
        int get_damage() 
        {
            return damage;
        }
        void change_health(int change_in_durabilty) 
        {
            health=health+change_in_durabilty;
        }
        int get_level() 
        {
            return level;
        }
    }
  • 1
    Have you check this answer? https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way – Chayne P. S. Sep 09 '20 at 23:17

1 Answers1

1

Create a static method to initialize your static hashtable object. This will be initialized when the class is loaded.

public class Main {
    public static Hashtable<Integer,String> mytable = new Hashtable<>();
    
    static {
        initializeMytable();
    }

    // static initializer where you can initiate your static object
    static void initializeMytable() {
        mytable.put(2,"sam");
    }
    
    public static void main(String[] args) {
 
        // use your table where you need it later
        System.out.println(mytable);
    }
}
megatune
  • 41
  • 3
  • I looked at the link provided and that does answer my question, and the static method also looks like it would work. So I just have question. What's the difference between a hashtable and a hashmap, and is the difference big enough to warrant using one over the other? – Andrewbandrew05 Sep 10 '20 at 16:27
  • 1
    HashMap is more commonly used when compared to Hashtable. Reason (from Oracle API docs) - `As of the Java 2 platform v1.2, Hashtable was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.` – megatune Sep 11 '20 at 00:21