Reputation: 346
I am developing a word game with libgdx and I have a word list to use in my game. I need to keep this words in app. I can't not use SQLite because I also want my game work on IOS. So I decided to use Json and I used code below. In my json file there are one hundred thousand words.
FileHandle file = Gdx.files.internal("data/word.json");
Json json = new Json();
ObjectMap<String, Data> dataMap = json.fromJson(ObjectMap.class, file);
But it takes too long to load, and consumes too much memory naturally. I only want to fetch a random word from Json file, and search for a word is exist in Json file. It doesn't seem correct to load all file for only these two simple operation.
What is the best implementation for these? Is Json only option in my situation?
Thanks in advance.
Upvotes: 0
Views: 174
Reputation: 521
You could just make a text file with the words, e.g. one per line, in alphabetic order. Upon starting your application you could read the entire file, making a simple index, e.g. noting the start position of words starting with the letter 'b'. You can then use the index to jump closer to the word you are looking for in the file. You can improve the index to include more letters, but each comes with a memory cost.
Upvotes: 1