我有2 ArrayList<HashMap<String,String>>
两个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,并且它只循环一次。
这是我的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)
你可以帮我找到答案,甚至是正确的代码吗?
第180行是hashMapFixFinishedList.remove(i);
提前谢谢..
答案 0 :(得分:0)
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();
} }}
&#13;
答案 1 :(得分:0)
当你删除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中的所有数据都没有重复的线索,那么只需创建一个新的Hashmap并
hashmap.addAll(hashmap1.values());
hashmap.addAll(hashmap2.values());
它会自动删除重复值。