How to access data from a Hashmap in another class

1

How can I access data from a Hashmap in another class?

Example:

Class1:

Hashmap<String,String> teste = new Hashmap<>();
teste.put("TESTE", "exemplo");

Class2:

How can I get access to Hashmap test?

When instantiating Classe1 does%% do not stay with the data or is it?

    
asked by anonymous 13.10.2014 / 13:59

1 answer

4

Making the map available

First, you need to define some means of retrieving the map that is in Classe1 . There are several ways to do this ...

1. Instance Attribute

The map could be placed in an instance attribute, with the values initialized in the constructor:

public class Classe1 {
    public Map<String, String> teste = new Hashmap<>();
    public Classe1() {
        teste.put("TESTE", "exemplo");
    }
}

2. Method that returns the instance map

However, public attributes are not recommended. This breaks the encapsulation of the class. So we could have a getter method:

public class Classe1 {
    private Map<String, String> teste = new Hashmap<>();
    public Classe1() {
        teste.put("TESTE", "exemplo");
    }
    public Map<String, String> getMapa() {
        return teste;
    }
}

3. Method that returns a new map

If you need to create several different maps, depending on some situation, create the map in a method and return it:

public class Classe1 {
    public Map<String, String> getNovoMapa()
        Map<String, String> teste = new Hashmap<>();
        teste.put("TESTE", "exemplo");
        return teste;
    }
}

Making the map statically

If the content of the map is not dependent on an instance of Classe1 , for example if the values are fixed, then it does not need to depend on an instance of Classe1 .

There are also several ways to do this:

1. Static attribute

The map could be placed in a static attribute, with values initialized in a static boot block:

public class Classe1 {
    public static Map<String, String> testeEstatico = new Hashmap<>();
    static {
        testeEstatico.put("TESTE", "exemplo");
    }
}

2. Static method that returns the class map

However, we have in the previous example the problem with class encapsulation. So we could have a static getter method:

public class Classe1 {
    private Map<String, String> testeEstatico = new Hashmap<>();
    static {
        testeEstatico.put("TESTE", "exemplo");
    }
    public static Map<String, String> getMapaEstatico() {
        return testeEstatico;
    }
}   

3. Static method that returns a new map

To return a new map statically:

public class Classe1 {
    public static Map<String, String> getNovoMapaEstatico()
        Map<String, String> testeEstatico = new Hashmap<>();
        testeEstatico.put("TESTE", "exemplo");
        return testeEstatico;
    }
} 

Access map

If the map is not static, Classe2 must have a reference to an instance of Classe1 .

This can be done in several ways ...

1. Instantiating Classe1 directly

public class Classe2 {
    public void metodo() {
        Classe1 classe1 = new Classe1();

        //recupera novo mapa
        Map<String, String> testeNovo = classe1.getNovoMapa();

        //recupera mapa da instância através do getter
        Map<String, String> teste = classe1.getMapa();

        //recupera mapa diretamente do atributo da instância (não recomendado)
        Map<String, String> teste2 = classe1.teste;
    }
}

2. Receiving an instance of Classe1 per parameter in method

public class Classe2 {
    public void metodo(Classe1 classe1) {
        //recupera novo mapa
        Map<String, String> testeNovo = classe1.getNovoMapa();

        //recupera mapa da instância através do getter
        Map<String, String> teste = classe1.getMapa();

        //recupera mapa diretamente do atributo da instância (não recomendado)
        Map<String, String> teste2 = classe1.teste;
    }
}

3. Having an instance of Classe1 within Classe2

In this case, the classe1 attribute must be initialized in some way.

public class Classe2 {

    Classe1 classe1;

    //inicialização via construtor
    public Classe2(Classe1 classe1) {
        this.classe1 = classe1;
    }

    //inicialização via setter
    public void setClasse1(Classe1 classe1) {
        this.classe1 = classe1;
    }

    public void metodo() {
        //recupera novo mapa
        Map<String, String> testeNovo = classe1.getNovoMapa();

        //recupera mapa da instância através do getter
        Map<String, String> teste = classe1.getMapa();

        //recupera mapa diretamente do atributo da instância (não recomendado)
        Map<String, String> teste2 = classe1.teste;
    }
}

Accessing the map statically

If the map is static, you can access it directly from any point in the code.

public class Classe2 {
    public void metodo() {
        //recupera novo mapa
        Map<String, String> testeNovo = Classe1.getNovoMapaEstatico();

        //recupera mapa da classe através do getter
        Map<String, String> teste = Classe1.getMapaEstatico();

        //recupera mapa diretamente do atributo da classe (não recomendado)
        Map<String, String> teste2 = Classe1.testeEstatico;
    }
}

Considerations

There are advantages and disadvantages in the various approaches. In summary:

  • Avoid accessing attributes directly
  • For "constant" maps (which do not change), use static methods that return a static attribute
  • When there is a dependency relationship between two classes, avoid using new directly, always prefer to receive the instance via constructor or setter . This is the concept of Inversion of Control, which reduces the coupling between classes, facilitates the creation of unit tests and helps in understanding the code, since it leaves the dependency explicit.
13.10.2014 / 17:02