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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:35:40+00:00 2026-06-02T23:35:40+00:00

I have an interesting problem I would like some help with. I have implemented

  • 0

I have an interesting problem I would like some help with. I have implemented a couple of queues for two separate conditions, one based on FIFO and the other natural order of a key (ConcurrentMap). That is you can image both queues have the same data just ordered differently. The question I have (and I am looking for an efficient way of doing this) if I find the key in the ConcurrentMap based on some criteria, what is the best way of finding the “position” of the key in the FIFO map. Essentially I would like to know whether it is the firstkey (which is easy), or say it is the 10th key.

Any help would be greatly appreciated.

  • 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-02T23:35:41+00:00Added an answer on June 2, 2026 at 11:35 pm

    I believe something like the code below will do the job. I’ve left the implementation of element –> key as an abstract method. Note the counter being used to assign increasing numbers to elements. Also note that if add(...) is being called by multiple threads, the elements in the FIFO are only loosely ordered. That forces the fancy max(...) and min(...) logic. Its also why the position is approximate. First and last are special cases. First can be indicated clearly. Last is tricky because the current implementation returns a real index.

    Since this is an approximate location, I would suggest you consider making the API return a float between 0.0 and 1.0 to indicate relative position in the queue.

    If your code needs to support removal using some means other than pop(...), you will need to use approximate size, and change the return to ((id - min) / (max - min)) * size, with all the appropriate int / float casting & rounding.

    public abstract class ApproximateLocation<K extends Comparable<K>, T> {
    
        protected abstract K orderingKey(T element);
    
        private final ConcurrentMap<K, Wrapper<T>> _map = new ConcurrentSkipListMap<K, Wrapper<T>>();
        private final Deque<Wrapper<T>> _fifo = new LinkedBlockingDeque<Wrapper<T>>();
        private final AtomicInteger _counter = new AtomicInteger();
    
        public void add(T element) {
            K key = orderingKey(element);
            Wrapper<T> wrapper = new Wrapper<T>(_counter.getAndIncrement(), element);
            _fifo.add(wrapper);
            _map.put(key, wrapper);
        }
    
        public T pop() {
            Wrapper<T> wrapper = _fifo.pop();
            _map.remove(orderingKey(wrapper.value));
            return wrapper.value;
        }
    
        public int approximateLocation(T element) {
            Wrapper<T> wrapper = _map.get(orderingKey(element));
            Wrapper<T> first = _fifo.peekFirst();
            Wrapper<T> last = _fifo.peekLast();
            if (wrapper == null || first == null || last == null) {
                // element is not in composite structure; fifo has not been written to yet because of concurrency
                return -1;
            }
            int min = Math.min(wrapper.id, Math.min(first.id, last.id));
            int max = Math.max(wrapper.id, Math.max(first.id, last.id));
            if (wrapper == first || max == min) {
                return 0;
            }
            if (wrapper == last) {
                return max - min;
            }
            return wrapper.id - min;
        }
    
        private static class Wrapper<T> {
            final int id;
            final T value;
    
            Wrapper(int id, T value) {
                this.id = id;
                this.value = value;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting genetics problem that I would like to solve in native
I have one interesting problem. I must parse mail body (regular expression), get some
I have an interesting problem which I've been looking into and would appreciate some
I have an interesting problem and would appreciate your thoughts for the best solution.
I have some interesting problem for an hour.. In my flex project, all width
I'm trying to cope with some problem in finding good idea. I would like
I have an interesting problem to solve here that may require some creative direction.
I'm haiving an interesting discussion with an esteemed colleague and would like some additional
I have an interesting thought-problem right now, so maybe someone of you could help.
I've got an interesting problem that I'd like some advice and opinions on if

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.