Java int[] array to HashSet

前端 未结 7 2133
感情败类
感情败类 2020-12-29 20:54

I have an array of int:

int[] a = {1, 2, 3};

I need a typed set from it:

Set s;

If I do th

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 21:41

    Just add elements from array to Set with the below snippet

    public class RemoveDuplicateElements {
    
        public static void main(String args[]){
            int array[] =  {0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
            Set  abc = new HashSet ();
            for (Integer t:array){  
                abc.add(t); 
            }       
            System.out.println("sampleSet"+abc);  
        }
    
    }
    

提交回复
热议问题