1

I have 2 ArrayList<HashMap<String,String>>

  1. hashMapFinishedList
  2. hashMapUnfinishedList

Both hashMap have same attributes with different values,

I want to make a comparison between these 2 Arraylist, and if it has the same value, than it has to be deleted from the 1st hashmap,

I tried this code :

public void compareData() {
    // TODO Auto-generated method stub
    Log.d("Location", "Arrive at compareData");
    String a, b;
    Log.d("Status",
            "Total Finished List "
                    + String.valueOf(hashMapFinishedList.size()));
    Log.d("Status",
            "Total Unfinished List "
                    + String.valueOf(hashMapUnfinishedList.size()));
    hashMapFixFinishedList.addAll(hashMapFinishedList);

    int count = 0;
    int count2 = 0;
    int size = hashMapFinishedList.size();
    for (int i = 0; i < size; i++) {
        count++;
        Log.d("Status",
                "Inside Looping FinishedList, Looping "
                        + String.valueOf(count));

        for (int j = 0; j < hashMapUnfinishedList.size(); j++) {
            count2++;
            Log.d("Status", "Inside Looping UnfinishedList, Looping ke "
                    + String.valueOf(count2));
            a = hashMapFinishedList.get(i).get("finishedListId");
            b = hashMapUnfinishedList.get(j).get("unfinishedListId");
            if (a.equals(b)) {
                Log.d("Status", a + " is equal " + b);
                Log.d("Location",
                        "Attempt to remove "
                                + hashMapFinishedList.get(i).get(
                                        "finishedListTitle"));
                if (!(i > hashMapFixFinishedList.size()))
                    hashMapFixFinishedList.remove(i);

            } else
                Log.d("Status", a + " is not equal " + b);
        }
    }
}

but it only compares the first hashmap, then the loop doesn't proceed to the next hashmap,

and what concerns me even more is the fact that in my logcat, my program perhaps doesn't loop properly (not really sure though), my hashmap size is 2, and it loops only once.

Here is my logcat,

03-14 17:35:36.195: D/Location(32341): Arrive at compareData
03-14 17:35:36.195: D/Status(32341): Total Finished List 2
03-14 17:35:36.195: D/Status(32341): Total Unfinished List 4
03-14 17:35:36.195: D/Status(32341): Inside Looping FinishedList, Looping 1
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 1
03-14 17:35:36.195: D/Status(32341): 21 is equal 21
03-14 17:35:36.195: D/Location(32341): Attempt to remove Fffgggh
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 2
03-14 17:35:36.195: D/Status(32341): 21 is not equal 44
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 3
03-14 17:35:36.195: D/Status(32341): 21 is not equal 46
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 4
03-14 17:35:36.195: D/Status(32341): 21 is not equal 48
03-14 17:35:36.195: D/Status(32341): Inside Looping FinishedList, Looping 2
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 5
03-14 17:35:36.195: D/Status(32341): 46 is not equal 21
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 6
03-14 17:35:36.195: D/Status(32341): 46 is not equal 44
03-14 17:35:36.195: D/Status(32341): Inside Looping UnfinishedList, Looping ke 7
03-14 17:35:36.195: D/Status(32341): 46 is equal 46
03-14 17:35:36.195: D/Location(32341): Attempt to remove Mau Coba Ah
03-14 17:35:36.195: D/AndroidRuntime(32341): Shutting down VM
03-14 17:35:36.195: W/dalvikvm(32341): threadid=1: thread exiting with uncaught exception (group=0x41a60c08)
03-14 17:35:36.200: E/AndroidRuntime(32341): FATAL EXCEPTION: main
03-14 17:35:36.200: E/AndroidRuntime(32341): Process: com.thesis.teamizer, PID: 32341
03-14 17:35:36.200: E/AndroidRuntime(32341): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
03-14 17:35:36.200: E/AndroidRuntime(32341):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at java.util.ArrayList.remove(ArrayList.java:403)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at com.thesis.teamizer.ViewTaskToDoList.compareData(ViewTaskToDoList.java:180)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at com.thesis.teamizer.ViewTaskToDoList$AttemptGetTodolist.onPostExecute(ViewTaskToDoList.java:708)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at com.thesis.teamizer.ViewTaskToDoList$AttemptGetTodolist.onPostExecute(ViewTaskToDoList.java:1)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at android.os.AsyncTask.finish(AsyncTask.java:632)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at android.os.Looper.loop(Looper.java:146)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at android.app.ActivityThread.main(ActivityThread.java:5602)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at java.lang.reflect.Method.invokeNative(Native Method)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at java.lang.reflect.Method.invoke(Method.java:515)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-14 17:35:36.200: E/AndroidRuntime(32341):    at dalvik.system.NativeStart.main(Native Method)

could you please help me to find the answer, or even the right code?

And Line 180 is hashMapFixFinishedList.remove(i);

Thank you in advance..

Julius Leo
  • 41
  • 4

2 Answers2

0
  • You can iterate through hash map Using Iterator and using For each loop -Hope it will helpful to you Thanks

Iterator<Map.Entry<String, String>> iterator = hashMapUnfinishedList.entrySet().iterator() ;
Iterator<Map.Entry<String, String>> iterator2 = hashMapfinishedList.entrySet().iterator() ;
        while(iterator.hasNext()){
            Map.Entry<String, String> obj = iterator.next();
            String comparevalue= obj.getValue();
            //You can remove elements while iterating.
 

  while(iterator2.hasNext()){
            Map.Entry<String, String> obj2 = iterator.next();
            System.out.println(ob2j.getKey() +" :: "+ ob2j.getValue());
            //You can remove elements while iterating.
if(objecomparevalue.equals(ob2j.getValue()))
{
iterator1.remove();
} }}
Hardy
  • 2,576
  • 1
  • 23
  • 45
  • Thank you for your response, but I'm kinda new in android programming, iteration like this case http://stackoverflow.com/a/46908/4670114 ? – Julius Leo Mar 14 '15 at 09:02
  • yes you can do that or [link] (http://stackoverflow.com/questions/7683960/how-to-iterate-arraylisthashmapstring-string this) also. let me know where you get stuck – Hardy Mar 14 '15 at 09:06
  • Forgive me if I'm asking too much, but it supposed to be like this? 1st loop : for (HashMap map : hashMapFinishedList) 2nd loop : for (HashMap map2 : hashMapUnfinishedList) { }, then how could I compare and get string from each hashmap? – Julius Leo Mar 14 '15 at 09:23
  • No problem you can ask and yes exactly you are on right way – Hardy Mar 14 '15 at 09:38
  • Can you please give me a code example on how to compare all of two hashmaps value using iterator? – Julius Leo Mar 17 '15 at 02:49
  • Check my new answer in post – Hardy Mar 17 '15 at 05:31
  • @JuliusLeo please tellme where you get stuck and can post your code of line with errors thanks – Hardy Mar 17 '15 at 05:38
0

Your hashMapFinishedList size is 2 when you remove 1 value ,then it comes out from the for loop,as the size becomes 1

 i < hashMapFinishedList.size(); //and this condition becomes false.

thats why it comes out from the loop

Try this way..

int size =hashMapFinishedList.size();


 for (int i = 0; i < size; i++) //now use this size in your main for loop

Hope it works..!

And in case if you want all data in single hashMap with no duplicate vlues then just create a new Hashmap and

 hashmap.addAll(hashmap1.values());
 hashmap.addAll(hashmap2.values());

It will automatically deletes duplicate values.

Gagan
  • 745
  • 10
  • 31
  • You are right, now at least I know what cause the problem, but I still get an error, IndexOutOfBound Exception, because of String A, can you please help me a lil bit? thank you – Julius Leo Mar 14 '15 at 09:52
  • do one thing ,create a new hashmap copy of your hashMapFinishedList.,,,,now use newhashmap.remove(i); on this new hashmap, and if you still get this problem ,then put this newhashmap.remove(i); inside IF condition if(!(i>newhashmap.size())) – Gagan Mar 14 '15 at 10:10
  • I tried your code, and it still doesn't work, I've edited my code above to give a better explanation, it still said about indexOutOfBondException, – Julius Leo Mar 14 '15 at 10:39
  • try using for (int i = 0; i <= hashMapFinishedList; i++) in your very first code – Gagan Mar 14 '15 at 11:45
  • Then check the size of hash map and before comparing any object please check whether it is null or not.or at that particular index object exist or not – Hardy Mar 17 '15 at 05:19