-3

I want to realize a program that contains a hashmap<integer,ArrayList<integer>>and Arraylist<integer>

I want to test every value in arraylist if it is equal to the arraylist hashmap value

For example the first arraylist element with the first element in hashmap, the second arraylist element with a second element hashmap etc.

If are equal I incremete the counter and store it in a new hashmap that contains the keys and the counter

this is what i do

HashMap<Integer, Integer> sim = new HashMap<Integer, Integer>();
List<Integer> listOperateur = new ArrayList<Integer>();
    for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) {
       //hmm and listOperateur Already established and filled

                    if (e.getValue().equals(listOperateur)) {
                        count++;

            }
            sim.put(e.getKey(), count);
    }


    }
fatoma
  • 32
  • 8
  • 1. You didn't ask a question, so it isn't clear what you want to know. 2. Your code compares an empty array list with the values contained in the HashMap. This doesn't sound like what you wanted to achieve. 3. There's no real meaning in the order of HashMap entries. So comparing "the first element" with anything is meaningless. – Malt Apr 06 '17 at 17:44
  • I do not know how to do it, How to test the first arraylist element with the first hashmap element, the second element with the second element etc. And if the elements compared are identical I will add 1 to the counter and store the counter in a new hashmap – fatoma Apr 06 '17 at 17:55
  • Entries in HashMaps are unordered. There's no meaning for the term "first element". – Malt Apr 06 '17 at 17:56
  • The first value in arraylist from hashmap and the first value in arraylist the second with second etc. – fatoma Apr 06 '17 at 18:00

2 Answers2

0

Hashmap doesn't maintain order, you should prefer using LinkedHashMap for maintaining the order and so that you can compare as needed.

vijayraj34
  • 2,135
  • 26
  • 27
  • how to use LinkedHashMap ?? – fatoma Apr 06 '17 at 17:57
  • @fatoma Please refer this link http://stackoverflow.com/questions/6140856/what-is-linkedhashmapk-v – vijayraj34 Apr 06 '17 at 18:07
  • @fatoma Can you please tell me what you gonna compare? But one thing i realize is you are trying to iterate through in order for comparing. – vijayraj34 Apr 06 '17 at 18:12
  • fr exemple hasmap H : 1 [0, 1, 1, 0] 2 [0, 1, 1, 1] 3 [1, 0, 1, 0] arraylist 1 1 1 1 hashmap result ( similair element sum) 1 [2] 2 [3] 3 [2] – fatoma Apr 06 '17 at 18:13
  • what is this ? _(similair element sum) 1 [2] 2 [3] 3 [2]_ – vijayraj34 Apr 06 '17 at 18:23
  • that is to say 1 is the key [2] is: {1 [0, 1, 1, 0]} in hashmap and {1,1,1,1} first element hashmap=0 first element arraylist=1 --> not similair =0| second element hashmap=1 second element arraylist=1 --> similair= 1| third element hashmap=1 thirdelement arraylist=1 --> similair=1| etc. then count =2 – fatoma Apr 06 '17 at 18:29
0

I did this program that does what I need But it does not give the correct result

    Iterator i1 = listOperateur.iterator();
    for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) {

            count = 0;

            for (Integer mapValue : e.getValue()) {

                if (mapValue.equals(listOperateur.get(k))) {
                    count++;

                }
            }
            sim.put(e.getKey(), count);
            k++;

    }
fatoma
  • 32
  • 8