Search code examples
javastorecategorieswords

Best way to create categories with words


I am currently working on a little "Hangman" project. I want to make it possible for the user to choose from different categories, such as "Countries" or "Food" and this got me thinking about what would be the best way to handle and sort those categories.

I saved the words to a little text file, which looks something like this:

Countries: Hungary Austria Argentina Canada;
Food: Donut Bread Hamburger;

For now, I created a multidimensional ArrayList that stores all the words, each category in an individual ArrayList in that ArrayList.

ArrayList< ArrayList<String> > words = new ArrayList< ArrayList<String> >();

// ... read words from .txt file and store it in the words-ArrayList ...

I know, that in each category the first word is the title of a category, so if I wanted to get all the titles of the categories, it would look something like this:

for( ArrayList list : words ) {
    System.out.println( list.get(0) );
}

Now this method I'm using works perfectally fine, it just seems a bit too complex to me and I was wondering, if there are simpler methods to do that. I want to thank in advance for any suggestions you can give me.


Solution

  • Better to use Map<String, List<String>> for my money. The Map can be a HashMap, and the word category would be the key while the List (an ArrayList in the concrete form) would be the related value.

    Then to extract the categories, all you'd need to do would be to extract the key set and iterate through it. e.g.,

      Map<String, List<String>> mapList = new HashMap<String, List<String>>();
    
      // fill map here...
    
      for (String key : mapList.keySet()) {
         List<String> list = mapList.get(key);
    
         System.out.printf("%s: %s%n", key, list);
      }
    

    If you want the keys to be in a certain order, then you'd need to use one of the other concrete implementations of Map such as a TreeMap.

    For a simple example:

    import java.io.InputStream;
    import java.util.*;
    
    public class MapList {
       public static void main(String[] args) {
          Map<String, List<String>> mapList = new HashMap<String, List<String>>();
    
          String sourcePath = "MapListData.txt";
          InputStream source = MapList.class.getResourceAsStream(sourcePath);
          if (source == null) {
             return;
          }
          Scanner scan = new Scanner(source);
          while (scan.hasNextLine()) {
             String line = scan.nextLine().trim();
             if (!line.isEmpty()) {
                line = line.replace(";", "");
                String[] mainTokens = line.split("\\s*:\\s*");
                if (mainTokens.length == 2) {
                   String key = mainTokens[0];
                   List<String> list = new ArrayList<String>();
                   String[] subTokens = mainTokens[1].split("\\s+");
                   for (String subToken : subTokens) {
                      list.add(subToken);
                   }
                   mapList.put(key, list);
                }
             }
          }
    
          if (scan != null) {
             scan.close();
          }
    
          for (String key : mapList.keySet()) {
             List<String> list = mapList.get(key);
    
             System.out.printf("%s: %s%n", key, list);
          }
       }
    }
    

    For me returns:

    Beer: [Pilsner, Weiss, Brown_Ale, IPA]
    Countries: [Hungary, Austria, Argentina, Canada]
    Food: [Donut, Bread, Hamburger]