Uomolepre
Uomolepre

Reputation: 121

How to create a Map with different List as value?

I'm trying to create an algorithm to find the GCD. I know that there is a better way to resolve it, but I'm stuck in this problem: I'm using a map with Key = Divisor and Value = ArrayList of the numbers to divide.

I'd like to create a new ArrayList for each key, but I'm continuing to populate the same ArrayList.

Map<Integer,ArrayList<Integer>> map = new HashMap<Integer,ArrayList<Integer>>();
    int[] arr = {9,27,63}; //input
    ArrayList <Integer> v= new ArrayList<Integer>();



    for (int i = 0; i < arr.length; i++) {
 
        for (int div = 1 ; div < arr[i]; div++) {

            map.put(div, v);
            if ((arr[i] % div) == 0) {
                v.add(arr[i]);
            }
        }
    }

    int result;

    //print
    for (Map.Entry<Integer,ArrayList<Integer>> entry:map.entrySet()) {
        System.out.print("Key: "+(int)entry.getKey());
        System.out.print("");
        for(Integer in: entry.getValue()){
            System.out.print("-> "+in + " ");
        }
        System.out.println();
       //if size of ArrayList == arr input -> is a common divisor. The Greatest is the MCD
       if (entry.getValue().size() == arr.length){
            max = (int)entry.getKey();
        }
    }

    System.out.println("Result: "+ result); //ERROR
}

Output Example:

Key: 57-> 9 -> 9 -> 27 -> 27 -> 27 -> 63 -> 63 -> 63 -> 63 -> 63 
Key: 58-> 9 -> 9 -> 27 -> 27 -> 27 -> 63 -> 63 -> 63 -> 63 -> 63 
Key: 59-> 9 -> 9 -> 27 -> 27 -> 27 -> 63 -> 63 -> 63 -> 63 -> 63 

It's obvious that 57 can't divide 9, so this List should be clear. So, every time I find a divisor, I put it in the same list. Can someone help me?

Upvotes: 0

Views: 99

Answers (2)

abdeali004
abdeali004

Reputation: 473

According to your need, I think this what you want. In looping always loop till the number/2 for its factors because a number doesn't have any factor greater than its half.

    Map<Integer,ArrayList<Integer>> map = new HashMap<Integer,ArrayList<Integer>>();
    int[] arr = {9,27,63}; //input

    for (int i = 0; i < arr.length; i++) {
        int number = arr[i];
        for (int div = 1 ; div <= number/2 ; div++) {
            ArrayList <Integer> v= new ArrayList<>();
            if ((number % div) == 0) {
                if(!map.containsKey(div))
                    map.put(div, v);
                map.get(div).add(number);
            }
        }
    }

Output:

Key: 1-> 9 -> 27 -> 63 
Key: 3-> 9 -> 27 -> 63 
Key: 21-> 63 
Key: 7-> 63 
Key: 9-> 27 -> 63 

Let me know if it is correct as per your need.

Upvotes: 1

fjavierm
fjavierm

Reputation: 186

You should take a look to Java object reference.

I've only skimmed over your code but, I think what you want is something like

for (int i = 0; i < arr.length; i++) {
        for (int div = 1 ; div < arr[i]; div++) {
            if ((arr[i] % div) == 0) {
                if (!map.containsKey(div)) {
                    map.put(div, new ArrayList<>());
                }
                map.get(div).add(arr[i]);
            }
        }
    }

Upvotes: 2

Related Questions