使用 hashmap 在 Java 中进行简单缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16961428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple caching in Java using hashmap
提问by Abdul Kader
Just want to use java hashmap to cache a simple pair into memory and want to get the cached data in another instance.
只是想使用 java hashmap 将一个简单的对缓存到内存中,并希望在另一个实例中获取缓存的数据。
I am using the below code to put some datas into cache consider the below ProcessDefinitionJavaCode.java code.
我正在使用下面的代码将一些数据放入缓存,考虑下面的 ProcessDefinitionJavaCode.java 代码。
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..
如果我想在另一个java类temp.java的缓存中获取我刚刚添加的数据,我该怎么办。对不起,如果它很傻,我不是Java专家..
采纳答案by Gilbert Le Blanc
You pass the cache Hashmap to the other class in a constructor or a setter method.
您在构造函数或 setter 方法中将缓存 Hashmap 传递给另一个类。
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);
回答by Manuel Pires
The only method I know to retrieve the values from a map is creating a function to do so.
我知道从地图中检索值的唯一方法是创建一个函数来执行此操作。
That way you can create a function like this:
这样你就可以创建一个这样的函数:
String[] getValues(){
String[] aux=new String[cache.size()];
int i=0;
for (Integer integer : cache.keySet()) {
aux[i++]=cache.get(integer);
}
System.out.println(Arrays.toString(aux));
return aux;
}
回答by Victor Sorokin
How about this simple approach (it may be not exactly what you want, but you may get an useful idea):
这个简单的方法怎么样(它可能不是你想要的,但你可能会得到一个有用的想法):
class PairOfStrings {
final String a, b;
PairOfStrings(String a, String b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "PairOfStrings{" +
"a='" + a + '\'' +
", b='" + b + '\'' +
'}';
}
}
class Sample {
public static void main(String[] args) {
HashMap<Integer, PairOfStrings> pairs = new HashMap<Integer, PairOfStrings>();
pairs.put(1, new PairOfStrings("first", "second"));
pairs.put(2, new PairOfStrings("third", "fourth"));
System.out.println(pairs.get(1));
}
}