简体   繁体   中英

Java Two List<?> Using Iterator remove element from one?

i have Two list ,List<String> and List<Item>.

public class Item {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

My Code::

public class Twolist {

    List<Item> list1 = new ArrayList<>();
    List<String> list2 = new ArrayList<>();


    public Twolist() {

        Item item = new Item();
        item.setName("itemname1");
        list1.add(item);

        item = new Item();
        item.setName("itemname2");
        list1.add(item);

        item = new Item();
        item.setName("itemname3");
        list1.add(item);

        item = new Item();
        item.setName("itemname4");
        list1.add(item);

        item = new Item();
        item.setName("itemname5");
        list1.add(item);

        item = new Item();
        item.setName("itemname6");
        list1.add(item);

        item = new Item();
        item.setName("itemname7");
        list1.add(item);

        item = new Item();
        item.setName("itemname8");
        list1.add(item);

        // list 2 start here

        list2.add("itemname2");
        list2.add("itemname4");
        list2.add("itemname6");
        list2.add("itemname8");

        Iterator<Item> it1 = list1.iterator();

        while (it1.hasNext()) {
            Item item2 = (Item) it1.next();
            System.out.println(item2.getName());


            for (int i = 0; i < list2.size(); i++) {
                String str = list2.get(i);

                if (item2.equals(str)) {

                } else {
                    it1.remove();
                }
            }

        }

        System.out.println(list1.size());
    }

    public static void main(String[] args) {

        new Twolist();
    }

Error::

itemname1
Exception in thread "main" java.lang.IllegalStateException
    at java.util.ArrayList$Itr.remove(ArrayList.java:804)
    at com.samir.CollectionP.Twolist.<init>(Twolist.java:67)
    at com.samir.CollectionP.Twolist.main(Twolist.java:78)

if name in List<Item> is equals List<String> is same then remove element from List<Item> . how can i remove element from List<Item> ?

Well, you have multiple problems, so here is a possible solution (if you want to remove the elements of list2 from list1):

for (int i = 0; i < list1.size(); i++) {
  String s1 = list1.get(i).getName();
  System.out.println(s1);
  for (int j = 0; j < list2.size(); j++) {
    String s2 = list2.get(j);
    if(s1.equals(s2)){
      list1.remove(i);
      i--;
      break;
    }
  }
}

If you need to remove data not in common in the 2 lists, try:

for (int i = 0; i < list1.size(); i++) {
  String s1 = list1.get(i).getName();
  System.out.println(s1);
  boolean found = false;
  for (int j = 0; j < list2.size(); j++) {
    String s2 = list2.get(j);
    if(s1.equals(s2)){
      found = true;
      break;
    }
  }
  if(!found){
    list1.remove(i);
    i--;
  }
}

you need to use the boolean found in order not to delete all the elements.

In your internal for loop you are calling it1.remove(); (eventually) multiple times, for a single call to it1.next() right at the beginning on while loop.

You can not call it1.remove(); multiple times for a single it1.next()

the javadocs for Iterator.remove() expalins the details

class Item { String name; }

class SOF {

    static List<Item> list1 = new ArrayList();
    static List<String> list2 = new ArrayList();

    static {
        Iterator<Item> i = list1.iterator();
        while (i.hasNext()) {
            if (!exists(i.next().name)) {
                i.remove();
            }
        }
    }

    static boolean exists(String s) {
        for (String s2: list2) {
            if (s2.equals(s2))
                return true;
        }
        return false;
    }
}

The provided solution will work, BUT:

I think a HashMap<String, Item> list1 will be a better datatype. It is definitive faster than a List .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM