我正在尝试创建具有以下功能的工厂:
它应始终返回一个Filter对象。
无论是从哈希图中=如果哈希图中已经存在字符串字母(键),它都应该仅从哈希图中获取其值(对象)并返回它。
或一个新的(如果哈希图没有该键),然后创建一个新对象并返回它。
该程序应根据用户输入按以下顺序工作:
//user first input.
String letters="aaa"; // creat new object.
//user second input.
String letters="fff"; // creat new object.
String letters="aaa"; //dont make a new object and return the object and return the object of the first input.
为此,我想到了以下几点:
String letters
分配为键,将对象Filter
分配为值。这是我目前写的内容:
(FactoryClass)
//if i let this i will getjava.lang.NullPointerException
//private static Filter filter = null;
public static Filter getFilter(String letters){
Filter filter=new Filter(letters);
HashMap <String, Object> hmap = new HashMap< String , Object> ();
hmap.put(letters,filter);
//for the first run is true because the map has yet only one pair of <k,v>
if (hmap.containsKey(letters))
{
System.out.println("return the obj where there is a key match");//i will remove this later cz the user doesnt care about it.
//so i will return the filter object that has been created here "hmap.put(letters,filter);" by returning the value that matches the key.
return (Filter) hmap.get(letters);
} else {//if the user didn't enter the same key then a new object shall be created!.
System.out.println("new object has been generated");//i will remove this late cz the user doent care about it.
//if the entered letters(key) isnt found in the map then put it in the map and creat new object.
hmap.put(letters, filter);
return filter;
}
}
另一个类中的构造函数受到保护,并且工厂将从主方法的每个用户输入中获取字符串字母。 任何帮助将不胜感激,但请用Java代码演示您的建议。
好吧,这显然不能解决问题,但是如何解决问题呢?
所以我在线搜索,找到了computeIfAbsent,但我不知道该怎么使用。
在java orcale doc上是这样写的,并且
hmap.computeIfAbsent(letters, k -> new Filter (k) );
现在我不明白这个“ k”在这里是什么意思,也不知道这个“->”是什么意思 我尝试如上所述使用它,但出现一些错误:
l无法解析为变量
我已经在类过滤器中添加了以下内容
public class Filter {
private final String letters;
protected Filter(String letters) {
this.letters = letters;
}
public static void main(String[] args) {
while(true){
//Scanner to allow user to give input!.
Scanner in =new Scanner(System.in);
System.out.println("please enter the filter stirng!");
String filter= in.next();
}
}
public static Filter getFilter(String letters) {
Filter obj = hmap.get(letters);
if (obj == null) {
obj = new Filter(letters);
hmap.put(letters, obj);
}
return obj;
}
答案 0 :(得分:0)
尝试一下
public static void main(String[] args) {
Filter f = FilterFactory.getFilter("aaa"); // call from user1
Filter g = FilterFactory.getFilter("bbb"); // call from user2
Filter h = FilterFactory.getFilter("aaa"); // call from user3
System.out.println(f == h); // same filter
}
class FilterFactory {
private static Map<String, Filter> map = new HashMap<>();
private FilterFactory() {
}
public static Filter getFilter(String letters) {
return map.computeIfAbsent(letters, Filter::new);
}
// Pre-java 8 version
public static Filter getFilter2(String letters) {
Filter f = map.get(letters);
if (f == null) {
f = new Filter(letters);
map.put(letters, f);
}
return f;
}
class Filter {
String f;
public Filter(String f) {
this.f = f;
}
public String toString() {
return f;
}
}
在所有情况下,hashMap的键都是过滤器构造函数的参数。
注意:此功能需要Java 8 +。