How do I insert "pairs" or n-size into a collection of lists instead of composing HashMaps?

So I have a situation where I need to pass three values โ€‹โ€‹to a sequential BlockingQueue:

(SelectableChannel, ComponentSocketBasis, Integer).

      

In fact, they don't need to display a hash at all, and using a HashMap is ridiculous since there will always be only one key for each record; it would be nice if they were in some kind of ordered set. However, due to the lack of a known alternative, I used a HashMap in my implementation and created this generics obfuscation:

private LinkedBlockingQueue<HashMap<HashMap<SelectableChannel, ComponentSocketBasis>, Integer>> deferredPollQueue = new LinkedBlockingQueue<HashMap<HashMap<SelectableChannel, ComponentSocketBasis>, Integer>>();

      

It seems really funny. I must be a terrible n00b. Of course, there is a better way to do this that does not require me to decompose the key when fetching values โ€‹โ€‹or backtracking (theoretical - in practice, Java is always bloated :) algorithmic complexity on a useless hash computation I don't need because I have a key space of 1 and not even want to relationally match three links but just group them? With this implementation, I have to output the values โ€‹โ€‹like this:

while(deferredPollQueue.size() > 0) {
    System.out.println("*** Draining new socket channel from queue");
    HashMap<HashMap<SelectableChannel, ComponentSocketBasis>, Integer> p = deferredPollQueue.take();

    SelectableChannel chan = null;
    ComponentSocketBasis sock = null;
    int ops = 0;

    HashMap<SelectableChannel, ComponentSocketBasis> q = p.keySet().iterator().next();

    chan = q.keySet().iterator().next();
    sock = q.get(chan);

    ops = p.get(q).intValue();

    SelectionKey k = chan.register(selector, ops);  

    if(!channelSupervisorMap.containsKey(k))
        channelSupervisorMap.put(k, sock);
}

      

I'm sure anyone capable of reasoning here probably thinks this is a funny way to do it, so the question is, which is correct? :) I can't find evidence of java.util.Pair or java.util.Triplet in either location.

I suppose Orthodox Way (TM) would have to execute a custom class or interface just for the purpose of accommodating that triplet, but for such a small task on such a large system, it seems ridiculous verbose and unnecessary - albeit, then again, Java itself.

Also, maybe the values โ€‹โ€‹can be put in an ArrayList or Vector or derivatives, but in Java this doesn't provide a shorter way of accessing them than I get out of this HashMap here, although it might solve the problem of algorithmic complexity.

Back in Perl, we simply do this using the array reference as the value within the array:

push(@$big_queue_array, [$elem1, \%elem2, \@elem3]);

      

What's the best equivalent in Java?

0


a source to share


4 answers


Why not just create your own generic pair or triple class? Pretty much every Java 5+ project gets them in their own use classes!



+5


a source


You say that a custom class for storing the triple would be bloated and unnecessary, but this is really the way to do it, how object oriented modeling works. The custom class is explicit and readable and does not require more runtime resources than the generic holder class.



+4


a source


Functional Java has pairs <a href = "http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/P3.html" rel = "NOFOLLOW noreferrer"> triplets and tuples up to arity 8. There's also a type called HList

for the harsh level. So your type would be:

LinkedBlockingQueue<P3<SelectableChannel, ComponentSocketBasis, Integer>>

      

It's just a library, so drop the jar in your classpath and you're good to go.

+2


a source


You can just use ArrayList to store objects, since you know which object will be at that location. Creating a new class with SelectableChannel and ComponentSocketBasis elements for them would probably be better.

If you are going to do a lot of things, creating a common pair or tuple will now save you a lot of time, but if this is the only place you are going to use, then creating a new class will make your code much easier to read.

Whenever you see your class name in code, you know exactly what it is for, whereas if you just see your generic union it can be more difficult for you to figure out what it is used for.

This is a trade-off between programming time and readability.

+2


a source







All Articles