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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:02:02+00:00 2026-05-13T11:02:02+00:00

I would like to store tuples objects in a concurent java collection and then

  • 0

I would like to store tuples objects in a concurent java collection and then have an efficient, blocking query method that returns the first element matching a pattern. If no such element is available, it would block until such element is present.

For instance if I have a class:

public class Pair {
  public final String first;
  public final String Second;
  public Pair( String first, String second ) {
    this.first = first;
    this.second = second;
  }
}

And a collection like:

public class FunkyCollection {
  public void add( Pair p ) { /* ... */ }
  public Pair get( Pair p ) { /* ... */ }
}

I would like to query it like:

myFunkyCollection.get( new Pair( null, "foo" ) );

which returns the first available pair with the second field equalling “foo” or blocks until such element is added. Another query example:

myFunkyCollection.get( new Pair( null, null ) );

should return the first available pair whatever its values.

Does a solution already exists ? If it is not the case, what do you suggest to implement the get( Pair p ) method ?

Clarification: The method get( Pair p) must also remove the element. The name choice was not very smart. A better name would be take( ... ).

  • 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-13T11:02:02+00:00Added an answer on May 13, 2026 at 11:02 am

    Here’s some source code. It basically the same as what cb160 said, but having the source code might help to clear up any questions you may still have. In particular the methods on the FunkyCollection must be synchronized.

    As meriton pointed out, the get method performs an O(n) scan for every blocked get every time a new object is added. It also performs an O(n) operation to remove objects. This could be improved by using a data structure similar to a linked list where you can keep an iterator to the last item checked. I haven’t provided source code for this optimization, but it shouldn’t be too difficult to implement if you need the extra performance.

    import java.util.*;
    
    public class BlockingQueries
    {
        public class Pair
        {
            public final String first;
            public final String second;
            public Pair(String first, String second)
            {
                this.first = first;
                this.second = second;
            }
        }
    
        public class FunkyCollection
        {
            final ArrayList<Pair> pairs = new ArrayList<Pair>();
    
            public synchronized void add( Pair p )
            {
                pairs.add(p);
                notifyAll();
            }
    
            public synchronized Pair get( Pair p ) throws InterruptedException
            {
                while (true)
                {
                    for (Iterator<Pair> i = pairs.iterator(); i.hasNext(); )
                    {
                        Pair pair = i.next();
                        boolean firstOk = p.first == null || p.first.equals(pair.first);
                        boolean secondOk = p.second == null || p.second.equals(pair.second);
                        if (firstOk && secondOk)
                        {
                            i.remove();
                            return pair;                
                        }
                    }
                    wait();
                }
            }   
        }
    
        class Producer implements Runnable
        {
            private FunkyCollection funkyCollection;
    
            public Producer(FunkyCollection funkyCollection)
            {
                this.funkyCollection = funkyCollection;
            }
    
            public void run()
            {
                try
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        System.out.println("Adding item " + i);
                        funkyCollection.add(new Pair("foo" + i, "bar" + i));
                        Thread.sleep(1000);
                    }
                }
                catch (InterruptedException e)
                {
                    Thread.currentThread().interrupt();
                }
            }
        }
    
        public void go() throws InterruptedException
        {
            FunkyCollection funkyCollection = new FunkyCollection();
            new Thread(new Producer(funkyCollection)).start();
            System.out.println("Fetching bar5.");
            funkyCollection.get(new Pair(null, "bar5"));
            System.out.println("Fetching foo2.");
            funkyCollection.get(new Pair("foo2", null));
            System.out.println("Fetching foo8, bar8");
            funkyCollection.get(new Pair("foo8", "bar8"));
            System.out.println("Finished.");
        }
    
        public static void main(String[] args) throws InterruptedException
        {
            new BlockingQueries().go();
        }
    }
    

    Output:

    Fetching bar5.
    Adding item 0
    Adding item 1
    Adding item 2
    Adding item 3
    Adding item 4
    Adding item 5
    Fetching foo2.
    Fetching foo8, bar8
    Adding item 6
    Adding item 7
    Adding item 8
    Finished.
    Adding item 9
    

    Note that I put everything into one source file to make it easier to run.

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

Sidebar

Ask A Question

Stats

  • Questions 419k
  • Answers 419k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The mail() function will use whatever php.ini tells it to… May 15, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer return input-'0'; This, of course, given that input is a… May 15, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer I think maybe a little more detail about the exact… May 15, 2026 at 10:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.