How to initialize HashSet by construction in Java

2 Answers

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

public class MyClass {

    public static void main(String args[]) {
         
        Set<String> hs = new HashSet<>(Arrays.asList("a", "b", "c", "d"));

        System.out.println(hs);
    }
}
 
 
 
 
/*
run:
 
[a, b, c, d]
 
*/

 



answered Sep 14, 2023 by avibootz
0 votes
import java.util.Set;
import java.util.Arrays;
import java.util.HashSet;

public class MyClass {

    public static void main(String args[]) {
         
        final String[] array = new String[] { "a", "b", "c", "d" };
        final Set<String> hs = new HashSet<>(Arrays.asList(array));
        
        System.out.println(hs);
    }
}
 
 
 
 
/*
run:
 
[a, b, c, d]
 
*/

 



answered Sep 14, 2023 by avibootz

Related questions

1 answer 128 views
1 answer 162 views
162 views asked Feb 16, 2021 by avibootz
1 answer 174 views
1 answer 138 views
138 views asked Jul 18, 2022 by avibootz
1 answer 140 views
140 views asked Jul 18, 2022 by avibootz
2 answers 237 views
2 answers 158 views
...