gpt4 book ai didi

Java,如何使用 computeIfAbsent computeIfPresent 来实现这个目的?

转载 作者:行者123 更新时间:2023-12-02 11:10:12 26 4
gpt4 key购买 nike

我正在尝试制作具有以下功能的工厂:

  • 它应该总是返回一个 Filter 对象。
  • 从 hashmap = 如果字符串字母(键)已经存在于 hashmap 中,它应该只从 hashmap 中获取它的值(对象)并返回它。
  • 或者如果哈希映射没有该键,则创建一个新对象,然后创建一个新对象并返回它。

  • 该程序应根据用户输入按以下顺序运行:
     //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作为值(value)。
  • 接下来我想比较之前是否没有输入 key ,如果 key 已经存在则创建一个新对象,然后返回它的对象。

  • 这是我暂时写的:

    (工厂类)
     //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 文档上,这是写的和 hmap.computeIfAbsent(letters, k -> new Filter (k) );
    现在我不明白这个“k”是什么意思,也不知道这个“->”是什么意思
    我尝试像上面那样使用它,但我遇到了一些错误:
  • k 无法解析为变量
  • token “-”、“--”应有语法错误
  • l 无法解析为变量
  • 第一个问题,当使用 computeIfAbsent 时,代码应该是什么样子?
  • 无论如何我可以在不使用那些computeIfAbsent和coputeIfPresent的情况下获得我想要的东西吗?

  • 我已经在类过滤器中有以下内容
    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 的 Key 都是过滤器构造函数的参数。
  • 对于方法引用,它是隐含的。
  • 对于 lambda,它是局部变量(在上面的示例中为 k)。

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

    关于Java,如何使用 computeIfAbsent computeIfPresent 来实现这个目的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56622257/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com