Related Articles
Program to convert Java Set of characters to a String in Scala
• Last Updated : 03 Jan, 2020

A java Set of characters can be converted to a String in Scala by utilizing toString method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples and then discuss how it works in details.
Example:1#

 `// Scala program to convert Java set ``// to a String in Scala`` ` `// Importing Scala's JavaConversions object``import` `scala.collection.JavaConversions.``_`` ` `// Creating object``object` `GfG``{ `` ` `    ``// Main method``    ``def` `main(args``:``Array[String])``    ``{``     ` `        ``// Creating set of characters in Java``        ``val` `set ``=` `new` `java.util.HashSet[Char]()``         ` `        ``// Adding characters to the set``        ``set.add(``'a'``)``        ``set.add(``'b'``)``        ``set.add(``'c'``)``         ` `        ``// Converting set to a String``        ``val` `str ``=` `set.toString``         ` `        ``// Displays output``        ``println(str)``     ` `    ``}``}`
Output:
```[a, b, c]
```

Here, the duplicate elements are eliminated and the resultant order of the elements in set is same as stated above.
Example:2#

 `// Scala program to convert Java set ``// to a String in Scala`` ` `// Importing Scala's JavaConversions object``import` `scala.collection.JavaConversions.``_`` ` `// Creating object``object` `GfG``{ `` ` `    ``// Main method``    ``def` `main(args``:``Array[String])``    ``{``     ` `        ``// Creating set of characters in Java``        ``val` `set ``=` `new` `java.util.HashSet[Char]()``         ` `        ``// Adding characters to the set``        ``set.add(``'d'``)``        ``set.add(``'b'``)``        ``set.add(``'c'``)``         ` `        ``// Converting set to a String``        ``val` `str ``=` `set.toString``         ` `        ``// Displays output``        ``println(str)``         ` `    ``}``}`
Output:
```[b, c, d]
```

Here, the stated set is not in correct order but the resultant output is in proper order.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6485035,"math_prob":0.5757237,"size":1638,"snap":"2021-04-2021-17","text_gpt3_token_len":416,"char_repetition_ratio":0.12729499,"word_repetition_ratio":0.50729924,"special_character_ratio":0.25763127,"punctuation_ratio":0.1237785,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9774385,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-19T14:40:49Z\",\"WARC-Record-ID\":\"<urn:uuid:0b1bda5e-794e-4d30-bf2a-3b157d8b672b>\",\"Content-Length\":\"92077\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d371c181-5a2b-420c-b602-c1ed6b8efd15>\",\"WARC-Concurrent-To\":\"<urn:uuid:85a89fdf-b6f2-4a22-83cb-ea40f03c4ef3>\",\"WARC-IP-Address\":\"23.222.12.49\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/program-to-convert-java-set-of-characters-to-a-string-in-scala/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:CLJNXADGXSFQFYYRIU6LQLBTTEAP4QVL\",\"WARC-Block-Digest\":\"sha1:U34MDQZHOGQLRXYXUJJT6G2VG5E4FJLD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038887646.69_warc_CC-MAIN-20210419142428-20210419172428-00607.warc.gz\"}"}