Doubt interpretation function

Asked

Viewed 84 times

6

I have a Hashmap users:

private Map<String, ObjectOutputStream> utilizadores = new HashMap<String, ObjectOutputStream>();  

Can anyone tell me what this function does exactly? Here under no circumstances is Hashmap changed because it does not?

private synchronized void enviar_para_um(Mensagem mensagem){
        for(Map.Entry<String, ObjectOutputStream> mapa : utilizadores.entrySet()){
            if(mapa.getKey().equals(mensagem.getNomeClienteReceptorMensagem())) {       

                try {
                    mapa.getValue().writeObject(mensagem);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

2 answers

5

for below will go through all the Hashmap users. To go through it calls the method entrySet() that returns a Set<Map.Entry<K,V>>.

for(Map.Entry<String, ObjectOutputStream> mapa : utilizadores.entrySet())

if below checks that the Hashmap item key is equal to mensagem.getNomeClienteReceptorMensagem().

if(mapa.getKey().equals(mensagem.getNomeClienteReceptorMensagem())) {

If so, then the value map is accessed by calling the method writeObject and passing to mensagem as a parameter.

mapa.getValue().writeObject(mensagem);

I understand, therefore, that this method does the proposed, ie sends the message to a user only. The user in which the attribute nomeClienteReceptorMensagem of mensagem passed as parameter matches with one of the Hashmap keys utilizadores.

  • 2

    I just don’t understand why the developer iterates the HashMap instead of doing something like ObjectOutputStream oos = utilizadores.get(mensagem.getNomeClienteReceptorMensagem()); if (oos != null) { oos.writeObject(mensagem) }. It seems that this bond is unnecessary.

  • it’s basically going to be the same!

  • 1

    Well observed @Anthonyaccioly. The loop is unnecessary in this case. The access is O(1), but in this case is O(n). Anyway, I’ve been following the java, I’ve even talked to him in chat. He’s in the learning process and it looks like this is a graduation project.

  • 1

    @java, really the effect is the same, however, the chunk of code proposed by Anthonyaccioly eliminates the need for the loop. If there were many users, then the loop would be much slower than the chunk of code proposed by it. Note that a hashmap is a data structure built so that you can access the data in time O(1).

  • @Cantoni the way that Anthony said is most noticeable! however it is all a matter of speed in data processing.

  • @Anthonyaccioly, worth an answer. Your comment enriches the discussion.

  • 1

    Exact @java. In this case, it’s more a question of performance added to a clarity of code. This. :-)

  • 1

    @Anthonyaccioly put your answer here

  • @Cantoni help me on the other issue I put pff

  • Done. And a long sentence for the comment pass.

Show 5 more comments

4


Turning my comments into a response.

The code in question is some kind of Dispatcher Pattern. The idea is to send the message by typing it in ObjectOutputStream of a particular customer (according to the value of mensagem.getNomeClienteReceptorMensagem()).

To improve performance I would rewrite this method as follows:

private synchronized void enviaParaIm(Mensagem mensagem){
    ObjectOutputStream oos = utilizadores.get(mensagem.getNomeClienteReceptorMensagem());
    if (oos != null) {
        try {
            oos.writeObject(mensagem);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

See that this way the code gets cleaner. In addition there is a performance improvement. The initial method iterated all map values (linear time O(n)), while the second makes a lookup direct, what, for a HashMap wheel in constant amortised time (O(1)).

Browser other questions tagged

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