Q:

TreeMap Force Close

I am new to TreeMap.
I have tried the following code:
    for(Map.Entry<Integer,Integer> entry : MultiSelect_TreeMap.entrySet()) {
        int Books = entry.getValue();

        for(Map.Entry<Integer,Integer> entry2 : MultiSelect_TreeMap2.entrySet()) {
            int CalculatedBooks = entry2.getKey();
            int Books2 = entry2.getValue();
            int Pages = CalculatedBooks - (BooksPos * MaxPos);
            MySQLITE_DATABASE.DeleteIfExist(Books, Pages);
            if(Books == Books2) 
            {
                MultiSelect_LinkedList.add(Pages);
                MultiSelect_TreeMap.remove(CalculatedBooks);
                MultiSelect_TreeMap2.remove(CalculatedBooks);
            }
        }
    }

But I am getting the force close error:
LogCat:
10-20 08:15:54.220: E/AndroidRuntime(764): FATAL EXCEPTION: main
10-20 08:15:54.220: E/AndroidRuntime(764): java.util.ConcurrentModificationException
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.util.TreeMap$MapIterator.stepForward(TreeMap.java:883)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.util.TreeMap$EntrySet$1.next(TreeMap.java:924)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.util.TreeMap$EntrySet$1.next(TreeMap.java:922)
10-20 08:15:54.220: E/AndroidRuntime(764):  at test.project.MainActivity.DeleteAllSelectedNames(MainActivity.java:1012)
10-20 08:15:54.220: E/AndroidRuntime(764):  at test.project.MainActivity$15.onClick(MainActivity.java:1274)
10-20 08:15:54.220: E/AndroidRuntime(764):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-20 08:15:54.220: E/AndroidRuntime(764):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 08:15:54.220: E/AndroidRuntime(764):  at android.os.Looper.loop(Looper.java:137)
10-20 08:15:54.220: E/AndroidRuntime(764):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.lang.reflect.Method.invokeNative(Native Method)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.lang.reflect.Method.invoke(Method.java:525)
10-20 08:15:54.220: E/AndroidRuntime(764):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-20 08:15:54.220: E/AndroidRuntime(764):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-20 08:15:54.220: E/AndroidRuntime(764):  at dalvik.system.NativeStart.main(Native Method)

Can someone please tell me what I did wrong?
Thanks a lot

A:

A for each loop cannot remove items from the underlying collection. You can either count indeces manually
for(int i = 0; i < MultiSelect_TreeMap.size(); i++){
//...
}

or use the iterator
Iterator<Entry<Integer, Integer>> it = MultiSelect_TreeMap.entrySet().iterator();

while(it.hasNext()){
    //...
    if (condition) {
        it.remove();
    }
}

EDIT:
I guessed, what your usecase actually could be. The following code will remove entries from both maps, if there is an Entry in mapA, that is equal to an entry in mapB.
Iterator<Entry<Integer,Integer>> it = mapA.entrySet().iterator();

while(it.hasNext()){
    Entry<Integer,Integer> entry = it.next();

    Integer calcBooks = entry.getKey();
    Integer books1 = entry.getValue();

    if(mapB.containsKey(calcBooks)){

        Integer books2 = mapB.get(calcBooks);

        if(books1.equals(books2)){
            mapB.remove(entry);
            it.remove();
        }
    }               
}

EDIT2: The class cast error isn't directly related to the first problem. The treemap organizes the order of elements by comparison. Therefore, the tree needs a Comparator for its elements.

