Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple caching in Java using hashmap

Just want to use java hashmap to cache a simple pair into memory and want to get the cached data in another instance.

I am using the below code to put some datas into cache consider the below ProcessDefinitionJavaCode.java code.

package Folder.ProcessDefinition;
import java.util.*;
import java.io.*;
public class ProcessDefinitionJavaCode{
/****** START SET/GET METHOD, DO NOT MODIFY *****/
    protected String string_1 = "";
    protected String string_2 = "";
    public String getstring_1() {
        return string_1;
    }
    public void setstring_1(String val) {
        string_1 = val;
    }
    public String getstring_2() {
        return string_2;
    }
    public void setstring_2(String val) {
        string_2 = val;
    }
/****** END SET/GET METHOD, DO NOT MODIFY *****/
    public ProcessDefinitionJavaCode() {
    }
    public void invoke() throws Exception {
/* Available Variables: DO NOT MODIFY
    In  : String string_1
    In  : String string_2
* Available Variables: DO NOT MODIFY *****/

HashMap<Integer,String> cache = new HashMap<Integer,String>();
cache.put(21, "Twenty One");
cache.put(31, "Thirty One");

}
}

What should I be doing If I want to get the datas I added just now in cache in another java class temp.java. I am sorry if it is very silly, I am not a Java expert..

like image 430
Abdul Kader Avatar asked Sep 02 '26 05:09

Abdul Kader


1 Answers

You pass the cache Hashmap to the other class in a constructor or a setter method.

HashMap<Integer,String> cache = new HashMap<Integer,String>();
cache.put(21, "Twenty One");
cache.put(31, "Thirty One");

NewClass newClass = new newClass(cache);

or

NewClass newClass = new newClass();
newClass.setCache(cache);
like image 90
Gilbert Le Blanc Avatar answered Sep 03 '26 18:09

Gilbert Le Blanc