简体   繁体   English

Java两个清单 <?> 使用迭代器从一个元素中删除元素?

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

i have Two list ,List<String> and List<Item>. 我有两个列表,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> . 如果List<Item>名称等于List<String>相同,则从List<Item>删除元素。 how can i remove element from List<Item> ? 如何从List<Item>删除元素?

Well, you have multiple problems, so here is a possible solution (if you want to remove the elements of list2 from list1): 好吧,您有多个问题,因此这是一个可能的解决方案(如果您想从list1中删除list2的元素):

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. 您需要使用boolean foundboolean found ,以便不删除所有元素。

In your internal for loop you are calling it1.remove(); 在内部for循环中,您正在调用it1.remove(); (eventually) multiple times, for a single call to it1.next() right at the beginning on while loop. (最终)多次,以便在while循环开始时对it1.next()进行一次调用。

You can not call it1.remove(); 您不能调用它it1.remove(); multiple times for a single it1.next() 一个it1.next()多次

the javadocs for Iterator.remove() expalins the details Iterator.remove()javadocs详细说明

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. 我认为HashMap<String, Item> list1将是更好的数据类型。 It is definitive faster than a List . 它比List快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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