添加到私有hashmap< integer,hashset&exampleObject>> gt;在Java进行测试

发布于 2025-02-02 21:01:47 字数 1456 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2025-02-09 21:01:47

您的add()方法返回布尔值,在您的示例中无法使用。
您需要创建数据结构标签。
例如:

private static HashMap<Integer, HashSet<exampleObject>> test = new HashMap<>();
private static HashSet<exampleObject> hashSet = new HashSet<>(); 

public static void main(String[] args) {
    hashSet.add(new exampleObject("key", "value")); // <-- returns boolean
    test.put(1, hashSet);
    for (exampleObject element : test.get(1)) {
        System.out.println(element);
    }
}

PS示例Object在此处有2个字符串字段。而且,不要忘记在示例视频中覆盖equals()和hashcode(),以打印对象的正确输出。

Your add() method return boolean and can't be used in your example.
And you need to create a data structure HashSet.
For example:

private static HashMap<Integer, HashSet<exampleObject>> test = new HashMap<>();
private static HashSet<exampleObject> hashSet = new HashSet<>(); 

public static void main(String[] args) {
    hashSet.add(new exampleObject("key", "value")); // <-- returns boolean
    test.put(1, hashSet);
    for (exampleObject element : test.get(1)) {
        System.out.println(element);
    }
}

P.S. exampleObject have 2 String fields here. And don't forget to override equals() and hashCode() in exampleObject for the correct output of the object to print.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文