开发者

Java: "myString[hey, there]" to HashSet?

开发者 https://www.devze.com 2023-04-05 07:47 出处:网络
I had a Ha开发者_Go百科shSet 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 had a Ha开发者_Go百科shSet 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消