got to delete an element from a collection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiniskumar
    New Member
    • Feb 2007
    • 58

    got to delete an element from a collection

    Ive got a collection with values
    Object[0]= hashmap(k,v)
    Object[1]= hashmap(k,v)
    Object[2]= hashmap(k,v)
    Object[3]= hashmap(k,v)
    Object[4]= hashmap(k,v)

    i terated this collection and deleted object[1]. then when i iterate again i get the following exception.
    java.util.Concu rrentModificati onException

    i want to delete elements till Object[3].
    how do i do this
    please help
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shiniskumar
    Ive got a collection with values
    Object[0]= hashmap(k,v)
    Object[1]= hashmap(k,v)
    Object[2]= hashmap(k,v)
    Object[3]= hashmap(k,v)
    Object[4]= hashmap(k,v)

    i terated this collection and deleted object[1]. then when i iterate again i get the following exception.
    java.util.Concu rrentModificati onException

    i want to delete elements till Object[3].
    how do i do this
    please help
    How did you delete? Post your code for the deleting part

    Comment

    • shiniskumar
      New Member
      • Feb 2007
      • 58

      #3
      Originally posted by r035198x
      How did you delete? Post your code for the deleting part

      for (Iterator iter = voucherTransact ionCols.iterato r(); iter.hasNext(); ) {
      Object object = (Object) iter.next();
      HashMap campusModel = (HashMap) object;

      voucherTransact ionCols.remove( campusModel);
      }

      Now i can delete the elements but when i delete Object[3] , the element Object[4] moves to the third position . so i cant delete the last elenment in the collection.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        see this code carefully .....

        Collection c = some code here;

        Label:
        for(int i=0;i<c.size(); i++)
        if(condition)
        {
        c.remove(c.get( i));
        continue Label;
        }

        try this code ..........
        all the best

        Comment

        • shiniskumar
          New Member
          • Feb 2007
          • 58

          #5
          Originally posted by dmjpro
          see this code carefully .....

          Collection c = some code here;

          Label:
          for(int i=0;i<c.size(); i++)
          if(condition)
          {
          c.remove(c.get( i));
          continue Label;
          }

          try this code ..........
          all the best

          i cannot get the method c.get(i);

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            sorry ..... i just use general convention .....
            now i use according to ur code ......

            label:
            for (Iterator iter = voucherTransact ionCols.iterato r(); iter.hasNext() {
            Object object = (Object) iter.next();
            HashMap campusModel = (HashMap) object;

            voucherTransact ionCols.remove( campusModel);
            continue label;
            }

            try this code ...... all the best ....
            i think u understood my logic

            Comment

            • shiniskumar
              New Member
              • Feb 2007
              • 58

              #7
              Originally posted by dmjpro
              sorry ..... i just use general convention .....
              now i use according to ur code ......

              label:
              for (Iterator iter = voucherTransact ionCols.iterato r(); iter.hasNext() {
              Object object = (Object) iter.next();
              HashMap campusModel = (HashMap) object;

              voucherTransact ionCols.remove( campusModel);
              continue label;
              }

              try this code ...... all the best ....
              i think u understood my logic
              now im getting java.util.Concu rrentModificati onException after deleting the first element and going for the next iteration.
              wat to do

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                wait plzzzz ... i m trying to solve it ......

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  it is because of removing and iterating concurrently...

                  so if u post ur code ...... then i think i will give u some hope....


                  thanxxxxx

                  Comment

                  • shiniskumar
                    New Member
                    • Feb 2007
                    • 58

                    #10
                    Originally posted by dmjpro
                    it is because of removing and iterating concurrently...

                    so if u post ur code ...... then i think i will give u some hope....


                    thanxxxxx

                    hey i got it
                    this is my code
                    label:
                    for (Iterator iter = voucherTransact ionCols.iterato r(); iter.hasNext(); ) {
                    Object object = (Object) iter.next();
                    HashMap campusModel = (HashMap) object;
                    iter.remove();
                    continue label;
                    }

                    }

                    }

                    Use the iterator to remove....... instead of coll.remove()

                    Thanks a lot

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Here is a hint from the docs


                      "The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModif icationExceptio n. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future"

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by shiniskumar
                        hey i got it
                        this is my code
                        label:
                        for (Iterator iter = voucherTransact ionCols.iterato r(); iter.hasNext(); ) {
                        Object object = (Object) iter.next();
                        HashMap campusModel = (HashMap) object;
                        iter.remove();
                        continue label;
                        }

                        }

                        }

                        Use the iterator to remove....... instead of coll.remove()

                        Thanks a lot
                        And it doesn't look like you'd seen my hint too. Very good work.

                        Comment

                        Working...