Java TreeMap单一创建
我有一个需要创建 TreeMap<>() 的应用程序,并且该地图只需要创建一次。我有创建地图的代码并且它可以工作。我还有保存和加载地图的工作方法。我想知道在幕后仅创建一次地图并在第一次运行应用程序之前存储它并且仅此一次的最佳方法是什么?我不想创建一个显示“创建地图”的按钮,然后再也不会访问该页面。有什么想法吗?
谢谢,
I have this application that requires the creation of TreeMap<>() and this map only requires to be created once. I have the code to create the map and it works. I also have working methods to save and load the map. I was wondering what is the best way to create the map only once behind the scenes and store it before I run the application for the first time and only this once? I don't want to create a button that says "create map" and then never visit that page again. Any ideas?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在静态字段中声明它。这只会创建一次,如果您在静态块中调用 load ,它只会被调用一次。
例如
You can declare it in a static field. This will be created only once and if you call load on it in a static block, it is called only once.
e.g.
以下类将在第一次调用其 get 方法期间实例化地图。您只需在每次需要地图时调用
Bla.getMap()
即可,无需担心其初始化。The following class will instanciate the map during the first call to its get method. You just need to call
Bla.getMap()
everytime you need the map, without worrying about its initialization.