简体   繁体   English

比较 2 个 ArrayList 之间的字符串<HashMap<String, String> &gt;

[英]Comparing string between 2 ArrayList<HashMap<String, String>>

I have 2 ArrayList<HashMap<String,String>>我有 2 个ArrayList<HashMap<String,String>>

  1. hashMapFinishedList hashMapFinishedList
  2. hashMapUnfinishedList hashMapUnfinishedList

Both hashMap have same attributes with different values,两个 hashMap 具有相同的属性和不同的值,

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,我想在这两个 Arraylist 之间进行比较,如果它具有相同的值,则必须从第一个哈希图中删除它,

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.更让我担心的是,在我的 logcat 中,我的程序可能没有正确循环(虽然不太确定),我的哈希图大小为 2,并且它只循环了一次。

Here is my logcat,这是我的 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);第 180 行是hashMapFixFinishedList.remove(i);

Thank you in advance..先感谢您..

  • You can iterate through hash map Using Iterator and using For each loop -Hope it will helpful to you Thanks您可以使用IteratorFor each循环遍历哈希映射 - 希望对您有所帮助 谢谢

 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(); } }}

Your hashMapFinishedList size is 2 when you remove 1 value ,then it comes out from the for loop,as the size becomes 1当您删除 1 个值时,您的 hashMapFinishedList 大小为 2,然后它从 for 循环中出来,因为大小变为 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如果您希望所有数据都在没有重复 vlues 的单个 hashMap 中,那么只需创建一个新的 Hashmap 和

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

It will automatically deletes duplicate values.它会自动删除重复值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM