Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Nancy Den

Let us first create a Stream:

Stream<String> stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");

Now convert Stream to TreeSet:

Set<String> set = stream.collect(Collectors.toCollection(TreeSet::new));

The following is an example to convert String to TreeSet in Java:

Example

import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Stream<String> stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");
      Set<String> set = stream.collect(Collectors.toCollection(TreeSet::new));
      set.forEach(val -> System.out.println(val));
   }
}

Output

Armenia
Australia
Canada
India
Poland
UK
US

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.