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

  • Home
  • SEARCH
  • 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 8335999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:50:55+00:00 2026-06-09T03:50:55+00:00

I have a Java class as class Students{ private String fName; private String lName;

  • 0

I have a Java class as

class Students{
    private String fName;   
    private String lName;
    private String uName;
    
    
    public Students(String fName, String lName, String uName) {
            this.fName = fName;
            this.lName = lName;
            this.uName = uName;
     }      
    
    public String getuName() {
        return uName;
    }
    public void setuName(String uName) {
        this.uName = uName;
    }
    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }
    
}

Also I call this using

public class TestClass {
public static void main(String[] args) {

    Students students1 = new Students("xyz","abc","xyzAbc");
    Students students2 = new Students("poi","son","poison");
    Students students3 = new Students("yog","jos","yogjos");
    
    Students students4 = new Students("xyz","abc","xyzAbc");
    Students students5 = new Students("pon","son","xyzAbc");
    Students students6 = new Students("yog","jos","someotherUName");
    Students students7 = new Students("yog","jos","someotherUName2");
    
    List studentList1 = new ArrayList();
    List studentList2 = new ArrayList();
    
    studentList1.add(students1);
    studentList1.add(students2);
    studentList1.add(students3);
    
    studentList2.add(students4);
    studentList2.add(students5);
    studentList2.add(students6);
}
}

Now I want a filtered list which would contain only unique "uName" values. Thus I want the comparison between "uName" field of each list and remove common ones.

At the end I would want 2 filtered list for studentList1 and studentList2.

I read about the removeAll method, but it seems to work with List of Integer/String data and not with List of Objects (as in my case).

  • 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-09T03:50:59+00:00Added an answer on June 9, 2026 at 3:50 am

    You can still use removeAll if the Objects in the List implement equals() properly.

    AbstractCollection, which is the base for most kind of List implementations (including ArrayList) uses contains() in its implementation of removeAll. ArrayList‘s implementation of contains relies on indexOf(), which lastly uses equals().

    You could implement equals() in your Student class to specify that an Student is equal to another if and only their uName fields are equal.

    Please note that equals has associated semantics (see its javadoc), and you should be careful when choosing how to implement it. Consider if two student instances really represent the same student when their uNames are equal. In my opinion, this sounds like a very specific requirement of how to sort these things out and should not impact the semantics of the class.

    You’ll be much better off with @AlexR or @KumarVivekMitra‘s approach.

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

Sidebar

Related Questions

I have a Java class as class Students{ private String fName; private String lName;
If I have a java class which is package-private (declared with class, not public
public class Student implements java.io.Serializable { private long studentId; private String studentName; private Set<Course>
I have a Java class, Node as follows : class Node { public ArrayList<Node>
I have a Java class class Student { String name; int age; } Also,
I have a generic java class that stores comparables: public class MyGenericStorage<T extends Comparable<T>>
This is my Student class that used to test log4j. public class Student{ private
I have a java class and I need to debug it (put breakpoints and
I have a java class containing all the columns of a database table as
I have a Java class MyPojo that I am interested in deserializing from JSON.

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.