Java:“myString[嘿,那里]”到哈希集?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须编写一个原始解析器,它将解析您的字符串并重建您的配置对象
you will have to write a primitive parser, which will parse your string and reconstruct your configuration objects
toString()
不是转换方法,类通常不实现static fromString
方法。所以你描述的是最简单的方法。请注意,如果元素本身包含
逗号
字符,它将无法正确重建Set。The
toString()
is not a conversion methods and classes usually don't implement astatic 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.