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 8967487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:15:16+00:00 2026-06-15T17:15:16+00:00

I have a list of objects in this format : class myObj { private

  • 0

I have a list of objects in this format :

class myObj {

    private String identifier;

    public myObj(String identifier){
        this.identifier = identifier;
    }

}

List<myObj> allobjects = new ArrayList<myObj>();
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("1234"));
allobjects.add(new myObj("12345"));
allobjects.add(new myObj("12"));
allobjects.add(new myObj("12"));

What is an elegant method of extracting the duplicate objects into seperate Lists ?
So in above example a new List is returned containing two lists. The first lists
contains :

new myObj("123");
new myObj("123");
new myObj("123");
new myObj("123"); 

The second list contains :

new myObj("12");
new myObj("12");

A possible solution is to create a new object :

List<List<myObj>> newList = new ArrayList<List<myObj>>

And then for each element in the list ‘allobjects’ iterate over each element and for each element that is contained more than once add it to the list. Then at the
end of the iteration for the current element add the newly created list to ‘newList’

Is this acceptable or is there another solution ?

  • 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-15T17:15:17+00:00Added an answer on June 15, 2026 at 5:15 pm

    Add equals and hashCode methods to the myObj class, so that you can use them as Map keys:

    class myObj {
    
        private String identifier;
    
        public myObj(String identifier){
            this.identifier = identifier;
        }
    
        public int hashCode(){
            return identifier.hashCode();
        }
    
        public boolean equals(Object o){
            return identifier.equals(((myObj)o).identifier);
        }
    }
    

    Then declare a Map:

    Map<myObj, List<myObj>> map = new HashMap<myObj, List<MyObj>>()
    

    and iterate through the original list. Use the myObjs as a map key, retrieving each time a list that corresponds to that myObj. If you encounter a certain myObj for the first time, don’t forget to create the list:

    for(myObj obj : allobjects){
        List<myObj> list = map.get(obj);
        if(list == null){
            list = new ArrayList<myObj>();
            map.put(obj, list);
        }
        list.add(obj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of objects with this implementation: class Locations { public string
I have a List of String objects that are pipe delimited. something like this:
I have a list of custom objects called EntertainmentEvent: public class EntertainmentEvent { public
I have a list of this object: public class Customer { public int Id
I have this class: public class User { public User(String nickname, String ipAddress) {
i have a list of lat/long objects on my server. eg. public class LatitudeLongitude
I have a simple object in the format of class MyObject { public string
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a list of objects which I store in the session. This list
I have an array list of objects and I am using this example to

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.