-2

i am trying to to calculating sum of mod of all values of a hashmap in java 8. but it is not providing as supposed to. below is the code i have written.

private static int FindPairs(int n,int[] ar) {
    Map<Integer, Integer> countermap=new HashMap<>();
    Iterator<Integer> itr=countermap.keySet().iterator();
    int result =0;
    for(int i=0;i<ar.length;i++) {
        if(countermap.containsKey(ar[i])) {
            countermap.put(ar[i], countermap.get(ar[i])+1);
        }
        else {
            countermap.put(ar[i], 1);
        }
    }
    int mod=0;

    while(itr.hasNext()) {
        System.out.println(itr.next());
        mod=itr.next()%2;
        result=result + mod;
    }
    System.out.println(Arrays.asList(countermap));
    return result;
}

input:

ar= {1,2,3,4,5,6,1,3,4,5}

n=7

output:

[{1=2, 2=1, 3=2, 4=2, 5=2, 6=1}]

0

5
  • 1
    Why is n never used anywhere? Also can you clarify what you're expecting please? Mar 6, 2019 at 16:29
  • Isn't the n unused in the method!?
    – Naman
    Mar 6, 2019 at 16:29
  • Yes I have used array.length while troubleshooting. I am trying to counter the pairs possible in a given array for repeated values. Mar 6, 2019 at 16:31
  • The first loop can be written as simple as for(int value: ar) countermap.marge(value, 1, Integer::sum); then the second for(int next: countermap.keySet()) { System.out.println(next); result += next % 2; } and for the final print statement, there is no need to wrap the map in a list, System.out.println(countermap); will do, unless the [ and ] around the output are crucial.
    – Holger
    Mar 6, 2019 at 18:18
  • sysout is just for the troubleshooting purpose only. plus i want to work on the values not keys keyset() will return a set of keys from map. basically i want to traverse the map looking for the mod of (value%2) and adding that up in result variable Mar 7, 2019 at 7:42

1 Answer 1

2

I have found two problems in the code you posted

  1. Your iterator has to be created after your HashMap has been filled. Otherwise, it will always be empty

    Iterator<Integer> itr = countermap.keySet().iterator(); <---- HERE
    while (itr.hasNext()) {
        System.out.println(itr.next());
        mod = itr.next()% 2;
        result = result + mod;
    }
    
  2. Also, calling itr.next() twice in a row will just skip one all the time. You should call it once, store it in a variable, and then use it twice if you want

    Iterator<Integer> itr = countermap.keySet().iterator();
    while (itr.hasNext()) {               _
        Integer next = itr.next();         |
        System.out.println(next);           }<---- HERE
        mod = next % 2;                   _|
        result = result + mod;
    }
    

With those changes, the output becomes the following

1
2
3
4
5
6
[{1=2, 2=1, 3=2, 4=2, 5=2, 6=1}]
3
2
  • I think the problem was having iterator declaration before hashmap thanks Mar 6, 2019 at 16:40
  • 1
    Or just use the standard loop idiom, for(Integer next: countermap.keySet()) { System.out.println(next); mod = next % 2; result = result + mod; } which guides to the correct use of a local variable automatically.
    – Holger
    Mar 6, 2019 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.