Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:10:39+00:00 2026-06-05T05:10:39+00:00

i have Two list ,List<String> and List<Item>. public class Item { private String name;

  • 0

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>?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:10:41+00:00Added an answer on June 5, 2026 at 5:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two extension methods: public static string ToString(this List<object> list, char delimiter) {
I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
Say I have two lists: List<CanidateSkill> canidateSkills; List<JobDesiredSkill> desiredSkills; class CanidateSkill { public Guid
I have two classes: public class Person { public string FirstName { get {
I have a two objects as follows: public class Item { public int ItemId
I have two List<string> ( listOfFullPaths containg full database paths for e.g. C:\MyDir\SomeDir\SomeDatabase.mdf) and
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew
Ok so I have two lists in C# List<Attribute> attributes = new List<Attribute>(); List<string>
I have a list of custom objects called EntertainmentEvent: public class EntertainmentEvent { public
I have my class structure as follows public class Email { public string Subject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.