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 148 views
2 answers 130 views
1 answer 156 views
1 answer 162 views
1 answer 142 views
1 answer 156 views
156 views asked Dec 7, 2021 by avibootz
...