1

2nd question, which is continue of first. I have got two Lists of strings. There is an List of strings (asu) - M1, M2, M3 ... As well as an List of string (rzs) - M1, M2, M3 and all possible combinations thereof. The need for each element (asu) (for example M1) to find an element in (rzs) (M1, M1M2, ..), which contains (e.g. M1). Example: took M1 from (asu) and will start search for duplicate(contain) in (rzs). We found M1M2 in (rzs), it contains M1. After that we should delete both elements from lists. Great thanks to No Idea For Name helped for modification this code. But the program always fails because AbstractList.remove error. Please help to implementation logic and tuning code!

Imports..........
        public class work{
        List<string> asu = Arrays.asList("M1","M1","M1","M3","M4","M5","M1","M1","M1","M4","M5","M5");
        List<string> rzs = Arrays.asList("M1","M2","M3","M4","M5",
        "M1M2","M1M3","M1M4","M1M5","M2M3","M2M4","M2M5","M3M4","M3M5","M4M5"
        ,"M1M2M3","M1M2M4","M1M2M5","M1M3M4","M1M3M4","M1M4M5","M2M4","M2M5");

        public static void main(String[] args) {
        work bebebe = new work();
        bebebe.mywork();
        }

        List<string> tmp1 = new ArrayList<string>();  
        List<string> tmp2 = new ArrayList<string>();  
           System.out.println(Arrays.deepToString(rzs));
           System.out.println(Arrays.deepToString(asu));
           for (string curr : asu){
             for (string currRzs : rzs){
               System.out.println("New iteration ");
               if (currRzs.contains(curr)) {
                  System.out.println("Element ("+curr+") in ASU =
                                    element ("+currRzs+") in RZS");
                  if(tmp1.contains(curr) == false)
                     tmp1.add(curr);

                  if(tmp2.contains(currRzs) == false)
                     tmp2.add(currRzs);
               }
              } 
            }

           for (string curr : tmp1){
              asu.remove(curr);
           }

           for (string currRzs : tmp2){
              rzs.remove(currRzs);
           }
3
  • 2nd question, which is continue of first - it is good practice to provide links Commented Aug 5, 2013 at 10:47
  • do you get UnsupportedOperationException? Commented Aug 5, 2013 at 10:50
  • yes, i have got UnsupportedOperationException Commented Aug 5, 2013 at 10:57

5 Answers 5

2

You should try to make use of removeAll() or retainAll() methods of Collection.

For example:

List<String> aList = new ArrayList<String>();
aList.add("a");
aList.add("b");
aList.add("c");
aList.add("d");
aList.add("e");

List<String> bList = new ArrayList<String>();
bList.add("b");
bList.add("e");
bList.add("d");

aList.removeAll(bList);

will give you the "a" and "c" elements left in aList

While if you try to make use of retainAll() method:

aList.retainAll(bList);

will give you "b", "d" and "e" elements left in aList;

retainAll() is used to remove all the elements of the invoking collection which are not part of the given collection.

removeAll() is used to remove all the elements of a collection from another collection.

So, it all depends on your use-case.

EDIT

If in any case you want to remove some elements from these collections while iterating conditionally then you should first obtain the Iterator<Type> then call the remove() method over it.

Like:

while(iterator.hasNext()){
    String str = iterator.next();

    if(str.equals('test')){
        iterator.remove();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Don't remove items from list using foreach loop. Use classic for and iterate over elements, and when removing item, decrease iterator.

Comments

2

To safely remove elements while iterating use Iterator.remove method:

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

Iterator<String> i = tmp1.iterator();
while (i.hasNext()) {
   i.next(); // must be called before remove
   i.remove();
}

Also it is easier to remove all collection from another by simply calling:

asu.removeAll(tmp1);

Comments

2

instead of List you can use Set, which will remove automatically the duplicate elements...

2 Comments

i can't use Set for my situation. Actually the main problem of this topic lies in the content of one object to another.
I see...well I can only say what others already suggested... use the Iterator's remove method to delete the duplicates...:D
2

You can use removeAll() method to remove collection of elements from the list instead of removing one by one.
use

asu.removeAll(tmp1);

instead of

for (string curr : tmp1)
{
    asu.remove(curr);
}

and use

rzs.removeAll(tmp2);  

instead of

for (string currRzs : tmp2)
{
     rzs.remove(currRzs);
}

update
I trace out your problem.The problem lies in Arrays.asList() method.
According to Arrays#asList

asList() returns "a fixed-size list backed by the specified array". If you want to resize the array, you have to create a new one and copy the old data. Then the list won't be backed by the same array instance.

So create a duplicate ArrayList for the lists.Like this

 List<string> asuDuplicat = new ArrayList<string>(asu);  
 List<string> rzsDuplicat = new ArrayList<string>(rzs);  

use asuDuplicat,rzsDuplicat.

 asuDuplicat.removeAll(tmp1);
 rzsDuplicat.removeAll(tmp2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.