Problem accessing HashMap data from another class

I have a problem accessing data in HashMap. It was created in one class and is called from another. See below;

Created

public class LoadDatabase {
    public Map virusDatabase = new HashMap();
    ...
    public void toHash(String v_Name, String signature) {
        virusDatabase.put(v_Name, signature);
    }
    ...
    public void printDatabase() {   // This method is displaying correct data, so is being stored.
        Iterator iterator = virusDatabase.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            System.out.println(key + " = " + virusDatabase.get(key));
        }
    }
    ...
}

Required Access

public class LCS {
    LoadDatabase lb = new LoadDatabase();
    Tokenizer T = new Tokenizer();
    ...
    public void buildDataLCS(String[] inTokens) {
        Iterator iterator = lb.virusDatabase.keySet().iterator();
        ...                
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            String v_sig = (String) lb.virusDatabase.get(key);
            System.out.println(v_sig);  //Example of problem, nothing printed
        ...
    }
    ...
}

Why is there a problem? Could you point me in the right direction.

+3
source share
2 answers

Or one of two questions,

  • You are not investing anything. Since I do not see your method call toHash(String v_Name, String signature).

  • You are using 2 different instances of the class LoadDatabase, somehow. Try to make LoadDatabasesingleton.

+4
source

Carlos

, , , , , , , - , . / /val, / /val, .

+1

Source: https://habr.com/ru/post/1774865/


All Articles