Java:“myString[嘿,那里]”到哈希集?

发布于 2024-12-04 22:25:22 字数 259 浏览 4 评论 0原文

我有一个 HashSet,并使用 .toString() 将其内容存储在字符串中。有没有什么简单的方法可以将字符串重新放入 HashSet 中?

我在文本文件中保存了程序的一些配置,现在我正在尝试读取配置。

我可能可以通过删除第一个和最后一个字符 [ ] 来做到这一点,然后使用 split() 将其拆分。然后循环并将其添加到 HashSet 中。不过感觉绕了很大的弯路,应该有和.toString()相反的吧?

那么,有没有比我描述的方法更简单的方法呢?

I had a HashSet and i stored the content of it in a String using .toString(). Is there any simple way to get the String back into a HashSet?

I saved some configurations for a program in a textfile, and now i'm trying to read the config.

I could probably do it by removing the first and the last characters [ ], and then just split it using split(). And then loop through and add it to a HashSet. But it feels like a big detour, there should be an opposite to .toString()?

So, is there any simpler way than the way i described?

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

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

发布评论

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

评论(2

月亮邮递员 2024-12-11 22:25:22

您必须编写一个原始解析器,它将解析您的字符串并重建您的配置对象

you will have to write a primitive parser, which will parse your string and reconstruct your configuration objects

橘亓 2024-12-11 22:25:22

toString() 不是转换方法,类通常不实现 static fromString 方法。所以你描述的是最简单的方法。

请注意,如果元素本身包含逗号字符,它将无法正确重建Set。

The toString() is not a conversion methods and classes usually don't implement a static fromString method. So what you describe is the simplest way.

Note that it will not reconstruct the Set correctly if the elements themselves contain comma character.

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