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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:31:44+00:00 2026-06-16T05:31:44+00:00

Summary of my question: I need a list that can quickly be iterated and

  • 0

Summary of my question: I need a list that can quickly be iterated and sorted (either by sorting method or adding/removing object).

I’m coding a game in which there are a lot of “collision zones”, that are checked every frame. For optimization, I have a idea of sorting them depends on their X position. The problem is not all collision zones are static, because some of them can move around.

I have managed to handles all the changes, but to maintain the ArrayList (or ConcurrentLinkedQueue) sorted using Collections.sort() is too slow.

So I got a new idea: I may use a Tree, and whenever a zone’s X is changed, instead of sorting all elements again, I can just remove then re-add it from the tree. However, I think that adding and removing operator in TreeList are expensive too. Moreover, iterating through Tree is not as effective as ConcurrentLinkedQueue, LinkedList or ArrayList.

Please tell me if there is any built-in data structure that satisfy my need. If there is no such data structure, I intend to extend ArrayList class, override the add method to ensure the order (by using overload add(index, item). If you think this is the best way, please give me the best way to find the index. I already use BinarySearch but I think there is a bug:

@Override
public boolean add(T e) {
    // Find the position
    int left = 0;
    int right = this.size() - 1;
    int pos = right / 2;
    if (e.compareTo(this.get(0)) <= 0) {
        pos = 0;
    } else if (e.compareTo(this.get(this.size() - 1)) >= 0) {
        pos = this.size();
    } else {
        // Need: e[pos - 1] <= e[pos] <= e[pos + 1] 
        boolean firstCondition = false;
        boolean secondCondition = false;

        do {
            firstCondition = this.get(pos - 1).compareTo(this.get(pos)) <= 0;
            secondCondition = this.get(pos).compareTo(this.get(pos + 1)) >= 0;

            if (!firstCondition) {
                right = pos - 1;
                pos = (left + right) / 2;
            } else if (!secondCondition) {
                left = pos + 1;
                pos = (left + right) / 2;
            }
        } while (!(firstCondition && secondCondition));
    }

    this.add(pos, e);

    return true;
}
  • 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-16T05:31:45+00:00Added an answer on June 16, 2026 at 5:31 am

    I would use a tree set. if you need to allow duplicates you can use a custom comparator. while iterating a tree set is slightly slower than an array, adding and removing is much faster.

    It appears you are doing an insertion sort which is O (n). an insert on a tree set is O (ln n)

    IMHO The best way to store duplicates by using a TreeMap<MyKey, List<MyType>> like this

    Map<MyKey, List<MyType>> map = new TreeMap<>();
    // to add
    MyType type = ...
    MyKey key = ...
    List<MyType> myTypes = map.get(key);
    if (myTypes == null)
        map.put(key, myTypes = new ArrayList<>());
    myTypes.add(type);
    
    // to remove
    MyType type = ...
    MyKey key = ...
    List<MyType> myTypes = map.get(key);
    if (myTypes != null) {
        myTypes.remove(myType);
        if (myTypes.isEmpty())
            map.remove(key);
    }
    

    In this case, addition and removal is O(ln N);

    You can allow “duplicates” is a TreeSet by defining all objects as different e.g.

    Set<MyType> set = new TreeSet<>(new Comparator<MyType>() {
       public int compare(MyType o1, MyType o2) {
          int cmp = /* compare the normal way */
          if (cmp == 0) {
              // or use System.identityHashCode()
             cmp = Integer.compare(o1.hashCode(), o2.hashCode());
             return cmp == 0 ? 1 : cmp; // returning 0 is a bad idea.
          }
       }
    }
    

    As you can see this approach is ugly unless you have some way of making every object unique.

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

Sidebar

Related Questions

Clarification/summary for the question -- we're looking for: a hosted bug tracking system, that
Title edits that reflect a better summary of the question are welcome. I'd like
EDIT: In case the question below looks a bit 'extensive', the summary is that
Question Summary: how can I get the path to the currently active application under
My question is surely banal but i can't set up an sql query that
Question Summary: Is there a better method than the one posted below to implement
Summary: I need a simple self-contained way to seed my RNG so that the
Question summary: How do I modify the code below so that untrusted, dynamically-loaded code
Summary of my question: Does NSURLConnection retain its delegate? Detailed question and scenario: I
A short summary of the question: I have a parent class which is extended

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.