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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:00:14+00:00 2026-05-24T18:00:14+00:00

I want to have an object that allows other objects of a specific type

  • 0

I want to have an object that allows other objects of a specific type to register themselves with it. Ideally it would store the references to them in some sort of set collection and have .equals() compare by reference rather than value. It shouldn’t have to maintain a sort at all times, but it should be able to be sorted before the collection is iterated over.

Looking through the Java Collection Library, I’ve seen the various features I’m looking for on different collection types, but I am not sure about how I should go about using them to build the kind of collection I’m looking for.

This is Java in the context of Android if that is significant.

  • 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-24T18:00:15+00:00Added an answer on May 24, 2026 at 6:00 pm

    Java’s built-in tree-based collections won’t work.

    To illustrate, consider a tree containing weak references to nodes ‘B’, ‘C’, and ‘D’:

       C
    B     D
    

    Now let the weak reference ‘C’ get collected, leaving null behind:

       -
    B     D
    

    Now insert an element into the tree. The TreeMap/TreeSet doesn’t have sufficient information to select the left or right subtree. If your comparator says null is a small value, then it will be incorrect when inserting ‘A’. If it says null is a large value, it will be incorrect when inserting ‘E’.

    Sort on demand is a good choice.

    A more robust solution is to use an ArrayList<WeakReference<T>> and to implement a Comparator<WeakReference<T>> that delegates to a Comparator<T>. Then call Collections.sort() prior to iteration.

    Android’s Collections.sort uses TimSort behind-the-scenes and so it runs quite efficiently if the input is already partially sorted.

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

Sidebar

Related Questions

I want a Python object that will monitor whether other objects have changed since
Hmm, I have a money object that allows me to add other money objects
I have an object of type Hash that I want to loop over via
I want to have a generic object that implements an interface. I mean if
I have an object that implements an interface. I want to call on the
I have an datetime object that I want to remove one hour to display
I have an XML object that I want to send to my player via
I have a traceback object that I want to show in the nice format
I have one very general object that I want to map to a destination
I have an object with a method that returns a filehandle, and I want

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.