Java,如何使用computeIfAbsent computeIfPresent来达到这个目的?

Java, how to use computeIfAbsent computeIfPresent to fulfil this purpose?

我正在尝试创建具有以下功能的工厂:

程序将根据用户输入按以下顺序运行:

 //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.

为此,我想到了以下几点:

这是我暂时写的:

(工厂类)

 //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;
        }
    }

另一个 class 中的构造函数受到保护,工厂将从 main 方法中获取每个用户输入的字符串字母。 任何帮助将不胜感激,但请在 java 代码中展示您的建议。

好吧,这显然没有解决问题,但是如何解决问题呢?

所以我在网上搜索并找到了 computeIfAbsent,但我不知道如何使用它。 在 java orcale 文档上,这是写的
hmap.computeIfAbsent(letters, k -> new Filter (k) );

现在我不明白这个 'k' 在这里是什么意思,也不明白这个“->”是什么意思 我尝试像上面那样使用它,但出现了一些错误:

我已经在 class 过滤器中包含以下内容

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;
       }

试试这个


    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 的键是过滤器构造函数的参数。

  • 对于方法引用,它是隐含的。
  • 对于 lambda 表达式,它是局部变量(上述示例中的 k)。

注意:Java此功能需要 8+。