How to organize multiple HashMaps?

Viewed 79

Hello! I'm trying to write as my first project a simple program, which would be able to test my knowledge of English words. The program is going to be similar to a paper version of dictionary. Right now managing program only with my testing unit of words (object map) is very simple and clear.


//Creating HashMap, which consists of words and their translated equivalents

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("0", "zero");
    map.put("1", "one");
    map.put("2", "two");
    map.put("3", "three");
    map.put("4", "four");
    map.put("5", "five");
    map.put("6", "six");
    map.put("7", "seven");
    map.put("8", "eight");
    map.put("9", "nine");
    map.put("10", "ten");

//User inputs integer from 1 to 100[%], which will determine number of words from base (HashMap) in the test

    Scanner scanner = new Scanner(System.in);
    double goodAnswers = 0;
    int numberOfQuestions = scanner.nextInt();
    numberOfQuestions *= (double) map.size() / 100;

//Creating two arrays, first one which consists all keys from HashMap

    ArrayList<String> arrayInOrder = new ArrayList<String>(map.keySet());
    ArrayList<String> arrayInDisorder = new ArrayList<String>();

//Filling a second array in a random manner

    int initialArraySize = arrayInOrder.size();
    for (int i = 0; i < initialArraySize; i++) {
        int j = new Random().nextInt(arrayInOrder.size());
        arrayInDisorder.add(arrayInOrder.get(j));
        arrayInOrder.remove(j);
    }

//Outputs a word (declared number of times) and waits for a user to input a translated word, then the program compares it with the value from the hashmap

    for (int i = 0; i < numberOfQuestions; i++) {
        System.out.println(arrayInDisorder.get(i));
        String input = scanner.next();
        if (input.equals(map.get(arrayInDisorder.get(i)))) {
            goodAnswers++;
        }
    }

//Outputs final result in %

    double result = 100 * goodAnswers / numberOfQuestions;
    System.out.println("Wynik: " + String.format("%.2f", result) + "%");

The difficulties start, however, when I want to give a user an option to choose from which units he wants to be tested. Let's say, now the program consists 3 HashMaps.


    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("0", "zero");
    map1.put("1", "one");
    map1.put("2", "two");
    map1.put("3", "three");
    map1.put("4", "four");

    HashMap<String, String> map2 = new HashMap<String, String>();
    map2.put("5", "five");
    map2.put("6", "six");
    map2.put("7", "seven");
    map2.put("8", "eight");

    HashMap<String, String> map3 = new HashMap<String, String>();
    map3.put("9", "nine");
    map3.put("10", "ten");

    map2.putAll(map3);
    map1.putAll(map2);

There would be only 3 HashMaps now, and I could use method putAll(), but the problem is, that in the example above I merged all HashMaps and the functionality I want to add to my program is that user can choose from which units (maps) he/she wants to be tested.

For example if the program would include 100 Hashmaps and a user just types 1, 7, 65 into the console, the program knows, that it have to merge 1st, 7th and 65th HashMap into a bigger one, which it is going to use as its 'main' map to test user's knowledge.

To sum up I would like to know how to merge these HashMaps which were chosen by a user into the single one (so my program will be able to use it), when he/she types them (HashMaps' names) into the console.

2 Answers

Thanks to you I recalled, that Arrays can contain other objects, for example HashMaps, so I came up with this:


    ArrayList<HashMap> motherArray = new ArrayList<HashMap>();

    motherArray.add(map1);
    motherArray.add(map2);
    motherArray.add(map3);

//Asking a user to choose number of units

    Scanner scanner = new Scanner(System.in);
    System.out.println("How many maps do you want to include in your test?");
    int numberOfMaps = scanner.nextInt();

//Creating the HashMap, which will contain all HashMaps chosen by a user from Array created earlier

    HashMap<String, String> motherMap = new HashMap<String, String>();

    for (int i = 0; i < numberOfMaps; i++) {
        System.out.println("What are their numbers (indexes)?");
        int indexOfMap = scanner.nextInt();
        motherMap.putAll(motherArray.get(i));
    }

I'm not sure if it is precisely what you wanted to point out, but this solution seems to be working pretty well for me even if I decide to expand or increase the number of these maps.

Thank you very much for your hint!

The problem manifests here: map1

You have more than of some object. When you start giving them names like map1, map2, ... you are tying yourself to exactly those variables.

The concept to use instead: arrays, respectively List/ArrayList.

That is all there is to this.

Related