Relate 2 objects to a single reference/key

Asked

Viewed 366 times

4

I have a while(true) which accepts multiple connections (Socket and Serversocket). Each connection is passed to a Processing object that handles your reading/writing of data. This Processing is passed to a Thread so you can accept connections and read/write simultaneously.

I was wondering if there’s any way to save these two objects (Treatment and Thread) so that they have a key of identification, for when I need to close them, call this key, receive the 2 objects and close them individually.

  • Hashmap does not solve your problem?

  • I tried to use, but I have 2 objects from different classes. I don’t know if Hashmap supports something like.

  • In the initial answer I had written for you to use two Haspmaps, one for Treatment and one for Thread, however, both indexed with the same key. This is another way to do it, if you don’t want to use the suggestion of the Tuple class.

  • I liked the previous reply. I will try to use it in my code, thanks for sharing.

1 answer

3


You can make a tuple class (for 2 elements). As described here: https://stackoverflow.com/a/12328838/2236741

With this class you even have the hash calculation taking into account the two objects. Modify it to your reality if necessary.

In this case it would look like this:

Map<Integer, Tuple<Tratamento,Thread>> objs = new HashMap<Integer, Tuple<Tratamento,Thread>>();

You will probably have to have some kind of object that stores keys that have not yet been closed.

If more than one thread goes to Hashmap, then you need to use a thread-safe version called Concurrenthashmap.

Update - Example of use

Below is an example of how to use it. Note that I used Integer, String instead of Thread and Treatment.

    HashMap<Integer, Tuple<Integer,String>> objs = new HashMap<Integer, Tuple<Integer, String>>();
    objs.put(10,new Tuple<Integer, String>(150,"ABC"));
    objs.put(11,new Tuple<Integer, String>(300,"DEF"));

    Tuple<Integer,String> tuple = objs.get(10);

    System.out.println(tuple.x);
    System.out.println(tuple.y);


    tuple = objs.get(11);

    System.out.println(tuple.x);
    System.out.println(tuple.y);

If you want, just change the attributes of the Tuple class from x and y to Treatment and Thread. However, your class will no longer be generic. If you only need it for this part of your code, switch to make it clear.

  • I didn’t know you had Tuple in JAVA, I met recently in Swift and is very useful, well remembered.

  • There are even some libraries: http://www.javatuples.org/ or (http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/tuple/package-summary.html)

  • How can I add values to Map? I’m trying with objectName.put(s, Tuple<threadNome, tratamentoNome>()); s is a string I’m using to identify and object the name of the Map;

  • I updated the answer with an example..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.