【问题标题】:Comparing string between 2 ArrayList<HashMap<String, String>>比较 2 个 ArrayList<HashMap<String, String>> 之间的字符串
【发布时间】:2015-03-14 08:55:20
【问题描述】:

我有 2 个ArrayList&lt;HashMap&lt;String,String&gt;&gt;

  1. hashMapFinishedList
  2. hashMapUnfinishedList

两个hashMap的属性相同,取值不同,

我想对这两个 Arraylist 进行比较,如果它具有相同的值,则必须从第一个 hashmap 中删除它,

我试过这段代码:

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

但它只比较第一个hashmap,然后循环不会进行下一个hashmap,

更让我担心的是,在我的 logcat 中,我的程序可能没有正确循环(虽然不太确定),我的 hashmap 大小为 2,它只循环一次。

这是我的日志,

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)

你能帮我找到答案,甚至是正确的代码吗?

第 180 行是hashMapFixFinishedList.remove(i);

提前谢谢你..

【问题讨论】:

    标签: java android string arraylist


    【解决方案1】:

    当你删除 1 个值时,你的 hashMapFinishedList 大小为 2,然后它从 for 循环中出来,因为大小变为 1

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

    这就是为什么它从循环中出来

    试试这个方法..

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

    希望它有效..!

    如果您希望单个 hashMap 中的所有数据没有重复的 vlues,那么只需创建一个新的 Hashmap 并

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

    它会自动删除重复值。

    【讨论】:

    • 你是对的,现在至少我知道问题的原因了,但我仍然得到一个错误,IndexOutOfBound Exception,因为 String A,你能帮我一点吗?谢谢
    • 做一件事,为你的 hashMapFinishedList 创建一个新的 hashmap 副本。,,,, 现在使用 newhashmap.remove(i);在这个新的 hashmap 上,如果你仍然遇到这个问题,那么把这个 newhashmap.remove(i);在 IF 条件中 if(!(i>newhashmap.size()))
    • 我试了你的代码,还是不行,我在上面编辑了我的代码来给出更好的解释,它仍然说 indexOutOfBondException,
    • 尝试在你的第一个代码中使用 for (int i = 0; i
    • 然后检查哈希映射的大小,在比较任何对象之前请检查它是否为空。或者在那个特定的索引对象是否存在
    【解决方案2】:
    • 您可以使用 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();
    } }}

    【讨论】:

    • 感谢您的回复,但我是android编程的新手,迭代像这种情况stackoverflow.com/a/46908/4670114
    • 是的,您也可以这样做或 [link] (stackoverflow.com/questions/7683960/… this)。让我知道你在哪里卡住了
    • 请见谅,如果我要求太多,但它应该是这样的?第一个循环:for (HashMap map : hashMapFinishedList) 第二个循环:for (HashMap map2 : hashMapUnfinishedList) { },那么我如何比较并从每个 hashmap 中获取字符串?
    • 没问题,你可以问,是的,你走对了
    • 你能给我一个关于如何使用迭代器比较所有两个哈希图值的代码示例吗?
    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多