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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:39:56+00:00 2026-05-13T23:39:56+00:00

I’ve been working all day and I somehow can’t get this probably easy task

  • 0

I’ve been working all day and I somehow can’t get this probably easy task figured out – probably a lack of coffee…

I have a synchronizedList where some Objects are being stored. Those objects have a field which is something like an ID. These objects carry information about a user and his current state (simplified).

The point is, that I only want one object for each user. So when the state of this user changes, I’d like to remove the “old” entry and store a new one in the List.

protected static class Objects{
    ...
    long time;
    Object ID;
    ... 
    }

…

if (Objects.contains(ID)) {
            Objects.remove(ID);
            Objects.add(newObject);
        } else {
            Objects.add(newObject);
        }

Obviously this is not the way to go but should illustrate what I’m looking for…
Maybe the data structure is not the best for this purpose but any help is welcome!


EDIT:
Added some information…
A Set does not really seem to fit my purpose. The Objects store some other fields besides the ID which change all the time. The purpose is, that the list will somehow represent the latest activities of a user. I only need to track the last state and only keep that object which describes this situation.
I think I will try out re-arranging my code with a Map and see if that works…

  • 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-05-13T23:39:57+00:00Added an answer on May 13, 2026 at 11:39 pm

    A Map is easiest, but a Set reflects your logic better. In that case I’d advice a Set.

    There are 2 ways to use a set, depending on the equals and hashCode of your data object.

    If YourObject already uses the ID object to determine equals (and hashCode obeys the contract) you can use any Set you want, a HashSet is probably best then.

    If YourObjects business logic requires a different equals, taking into account multiple fields beside the ID field, then a custom comparator should be used. A TreeSet is a Set which can use such a Comparator.

    An example:

    Comparator<MyObject> comp = new Comparator<MyObject>{
      public int compare(MyObject o1, MyObject o2) {
        // NOTE this compare is not very good as it obeys the contract but
        // is not consistent with equals. compare() == 0 -> equals() != true here
        // Better to use some more fields
        return o1.getId().hashCode() < o2.getId().hashCode();
      }
      public boolean equals(Object other) {
        return 01.getId().equals(o2.getId());
      }
    }
    
    Set<MyObject> myObjects = new TreeSet(comp);
    

    EDIT
    I have updated the code above to reflect that id is not an int, as suggested by the question.

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

Sidebar

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.