Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,442 answers

573 users

How to convert array to set in Java

3 Answers

0 votes
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
  
public class MyClass {
    public static void main(String args[]) {
        String arr[] = { "java", "c", "c++", "python", "c#", "c#", "c", "c" };
  
        Set<String>set = new HashSet<>(Arrays.asList(arr));
          
        System.out.println(set);
    }
}
  
  
  
/*
run:
  
[c#, c++, python, java, c]
  
*/

 



answered May 18, 2020 by avibootz
edited May 9, 2024 by avibootz
0 votes
import java.util.Set;
import java.util.Arrays;
import java.util.stream.Collectors; 
 
public class MyClass {
    public static void main(String args[]) {
        String array[] = { "java" , "c", "c++", "php", "c#", "c#", "java" };
                                   
        Set<String> set =  Arrays.stream(array).collect(Collectors.toSet()); 
  
        System.out.println(set);
    }
}
   
   
   
/*
run:
   
[c#, c++, java, c, php]
   
*/

 



answered May 18, 2020 by avibootz
edited May 9, 2024 by avibootz
0 votes
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
 
public class MyClass {
    public static void main(String args[]) {
        Integer[] arr = { 4, 9, 0, 3, 8, 7, 4, 4, 1, 1, 3, 3 };
     
        Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
         
        System.out.println(set);
    }
}
 
 
 
/*
run:
 
[0, 1, 3, 4, 7, 8, 9]
 
*/

 



answered May 9, 2024 by avibootz

Related questions

1 answer 200 views
1 answer 188 views
1 answer 193 views
3 answers 291 views
1 answer 941 views
2 answers 149 views
149 views asked Aug 15, 2024 by avibootz
2 answers 180 views
180 views asked Nov 11, 2023 by avibootz
...