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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:26:28+00:00 2026-06-09T23:26:28+00:00

What method should I use to keep items in a list if : I

  • 0

What method should I use to keep items in a list if :

  1. I have a an object which contains a list of items and can do action on those items
  2. Those items are ordered inside a list
  3. I need to be able to change the position of items inside the list and add or remove an item based on its id.
  4. The list will be saved into a database eventually (and items will need to keep their order inside this list)
  5. It doesn’t make sense to have a property “order” on the orderd items (these objects can be in multiple kind of lists).

The choices that I have (is there any other choices?) :

  1. Use a List object so it will automatically keep the order. But I’ll have to loop through all objects (at worse) to find an object by Id.
  2. Use a map with a Key / Value (object id / object), so it’ll be easy to retrieve the object (and fast) and keep another map that just keeps the order of the object (object Id / object order)

Solution #1) This seems like the easiest solution with a performance drawback.

Solution #2) This seems like the fastest solution but with a lot more code when reordering list.

Is there an object that I don’t know about that would be ordered, have Key / Value pairs and have methods like object.add(index, key, value) or object.getByIndex(index) or object.getByKey(key)?

  • 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-09T23:26:29+00:00Added an answer on June 9, 2026 at 11:26 pm

    As Roni suggested you need to use your own data structure. I believe that is the only option viable to fill in your requirement.

    I have done a sample to fill in your requirement as a starting point and you can build on it.

    public class MapAndList {
    
        private List<String> keys = new ArrayList<String>();
        private Map<String, Object> keyValuePair = new HashMap<String, Object>();
    
        public void addItem(String key, Object item) {
            // TODO if the key is already present in the list throw exception
            keys.add(key);
            keyValuePair.put(key, item);
        }
    
        public void removeItem(String key) {
            keys.remove(key);
            keyValuePair.remove(key);
        }
    
        public void removteItem(int index) {
            removeItem(keys.get(index));
        }
    
        public void addItem(String key, Object item, int index) {
            keys.add(index, key);
            keyValuePair.put(key, item);
        }
    
    }
    

    Hope this helps you….

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

Sidebar

Related Questions

I want to create a plugin called 'myPlugin'. Which method should I use and
I have a method that returns lot of data, should I use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for
In c# , when sending a parameter to a method, when should we use
How often should I use static methods generally? If I have like: Class1 _class34
Which method should I override to be guaranteed to always get the correct size
How can I specify that a method should take as parameter a pointer to
I have a sorting comparator which I need to use in a few different
I have few stacks in my code that I use to keep track of
I heard that static methods should use only static variables in java. But, main
What method should I follow in java to produce WordWord from Word#$#$% Word 1234

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.