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 132 views
1 answer 168 views
168 views asked Feb 16, 2021 by avibootz
1 answer 176 views
1 answer 144 views
144 views asked Jul 18, 2022 by avibootz
1 answer 148 views
148 views asked Jul 18, 2022 by avibootz
2 answers 242 views
2 answers 166 views
...