将 javafx.util.Properties 转换为 HashMap

发布于 2024-08-21 23:02:11 字数 206 浏览 5 评论 0原文

我想知道是否有一种简单的方法将 javafx.util.Properties 对象转换为 java.util.HashMap。

有一种显而易见的方法可以从 Properties 对象中获取每个值并将其放入 Map 中。但对于大量属性,似乎应该有一种方法来获取支持 javafx.util.Properties 的 Map(如果它是 Map)。

预先感谢您的任何建议。

I was wondering if there is an easy way to convert the javafx.util.Properties object to a java.util.HashMap.

There is the obvious way of getting each value from the Properties object and putting it in a Map. But with a large number of properties it seems like there should be a way of just getting the Map that backs javafx.util.Properties (if it is a Map).

Thanks in advance for any suggestions.

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

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

发布评论

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

评论(2

狼亦尘 2024-08-28 23:02:11

我真的不知道 javafx.util.Properties 是否由 Java Map 支持,但由于公共 API 没有提及任何获取此地图的方法,因此您可能不应该这样做尝试这样做 - 即使它是可能的(例如通过扩展 Properties 类),它也可能在未来的版本中发生变化。

我会继续复制每个元素。

I don't really know if javafx.util.Properties are backed by Java Map, but since public API does not mention any way to get this map you probably shouldn't try to do it - even if it was possible (e.g. by extending Properties class) it might change in future versions.

I would stay with copying every element.

江湖彼岸 2024-08-28 23:02:11

pazabos 的回答+1。但我会采取相反的方式:扩展 HashMap 或 java.util.Properties,然后可以导出 javafx.util.Properties (或保存一个实例)。喜欢:

class MyProperties extends HashMap {
    HashSet<String> keys = new HashSet<String>();
    javafx.util.Properties p = new Properties();

    public String get(String str) {
        return p.get(str);
    }

    public Map creatHashMap() {
        Map map = new HashMap();
        for (String k : keys) {
            map.put(k, p.get(k));
        }
        return map;
    }

    public void put() {
        //...
    }

+1 for pazabos answer. But I would go the other way around: extend HashMap or java.util.Properties which then could export javafx.util.Properties (or hold an instance) sth. like:

class MyProperties extends HashMap {
    HashSet<String> keys = new HashSet<String>();
    javafx.util.Properties p = new Properties();

    public String get(String str) {
        return p.get(str);
    }

    public Map creatHashMap() {
        Map map = new HashMap();
        for (String k : keys) {
            map.put(k, p.get(k));
        }
        return map;
    }

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