如何调用 hashmap 和 hashmap java中其他类的值?

发布于 2024-12-24 20:53:24 字数 90 浏览 1 评论 0原文

我想创建 5-6 个类,我将值存储在第一类和第二类的哈希图中。我想从 4 号、5 号和 5 号开始称呼它。第六课。如何获取任何片段或示例来实现这一点将会有所帮助,谢谢

I would like to create 5-6 classes,I am storing values in hashmap in 1st class & I would like to call it from 4th,5th & 6th class.How to get this any snippets or example to implement this will be helpful,Thanks

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

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

发布评论

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

评论(3

北斗星光 2024-12-31 20:53:24
public class Example {

    private HashMap<String, String> hashmap = new HashMap<String, String>();

    public HashMap<String, String> getHashmap() {
        return hashmap;
    }

    public void setHashmap(HashMap<String, String> hashmap) {
        this.hashmap = hashmap;
    }
}

public class AnotherClass {

    public static void main(String args[]) {
        Example ex = new Example();
        HashMap<String, String> hm = ex.getHashmap();
    } 

}
public class Example {

    private HashMap<String, String> hashmap = new HashMap<String, String>();

    public HashMap<String, String> getHashmap() {
        return hashmap;
    }

    public void setHashmap(HashMap<String, String> hashmap) {
        this.hashmap = hashmap;
    }
}

public class AnotherClass {

    public static void main(String args[]) {
        Example ex = new Example();
        HashMap<String, String> hm = ex.getHashmap();
    } 

}
梦途 2024-12-31 20:53:24

两种合理的做法。

  1. 为地图提供一个公共 getter。 Class5 将调用 class1.getMap().doSomething()。没有太多工作(好的),并且外部类可以对地图做任何他们想做的事情,例如clear(),这可能会或可能不会好。

  2. 为地图编写单独的方法,例如 putIntoMap()、removeFromMap() 等。需要更多工作,但您可以限制外部人员可以执行的操作。如果您不希望它们能够clear(),请不要编写ckearMap() 方法。

纯粹主义者有一条“德米特法则”,规定总是选择选项 2,但恕我直言,这往往是矫枉过正。

Two reasonable approaches.

  1. Have a public getter for the Map. Class5 would the call class1.getMap().doSomething(). Not much work (good) and outside classes can do anything they want to the map, e.g. clear(), which may or may not be good.

  2. Write individual methods for the map, e.g. putIntoMap(), removeFromMap(), etc. More work but you can restrict what outsiders can do. If you don't want them able to clear(), don't write a ckearMap() method.

Purists have a "Law of Demeter" that says always do option 2 but IMHO that is often overkill.

小忆控 2024-12-31 20:53:24

您应该对哈希图使用 setter 和 getter。

private HashMap h = null;
//instantiate hashmap in the constructor
public ...
//add value to hashmap
public void add(Object value)
{
    h.put(value);//eventually cast value or declare it as you did it in the hashmap 
}

//get hashmap
public HashMap getMap()
{ 
    return h;
}

//set hashmap
public void setMap(HashMap hm)
{ 
    h=hm;
}...

you should use setters and getters for the hashmap.

private HashMap h = null;
//instantiate hashmap in the constructor
public ...
//add value to hashmap
public void add(Object value)
{
    h.put(value);//eventually cast value or declare it as you did it in the hashmap 
}

//get hashmap
public HashMap getMap()
{ 
    return h;
}

//set hashmap
public void setMap(HashMap hm)
{ 
    h=hm;
}...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文