How to initialize a set with strings in Java

2 Answers

0 votes
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String args[]) {
        Set<String> set = new HashSet<>(Arrays.asList("java", "c", "c++", "python", "c#"));

        System.out.println(set);
    }
}



   
   
/*
run:
   
[c#, c++, python, java, c]
 
*/

 



answered Jul 17, 2022 by avibootz
0 votes
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String args[]) {
        Set<String> set = new HashSet<>();
        
        Collections.addAll(set, "java", "c", "c++", "python", "c#");

        System.out.println(set);
    }
}



   
   
/*
run:
   
[c#, c++, python, java, c]
 
*/

 



answered Jul 17, 2022 by avibootz

Related questions

2 answers 253 views
2 answers 235 views
1 answer 242 views
1 answer 249 views
1 answer 222 views
1 answer 223 views
223 views asked Dec 7, 2021 by avibootz
...