How do I remove repeated elements from ArrayList?

后端 未结 30 1579
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

相关标签:
30条回答
  • 2020-11-21 06:43
    public static void main(String[] args){
        ArrayList<Object> al = new ArrayList<Object>();
        al.add("abc");
        al.add('a');
        al.add('b');
        al.add('a');
        al.add("abc");
        al.add(10.3);
        al.add('c');
        al.add(10);
        al.add("abc");
        al.add(10);
        System.out.println("Before Duplicate Remove:"+al);
        for(int i=0;i<al.size();i++){
            for(int j=i+1;j<al.size();j++){
                if(al.get(i).equals(al.get(j))){
                    al.remove(j);
                    j--;
                }
            }
        }
        System.out.println("After Removing duplicate:"+al);
    }
    
    0 讨论(0)
  • 2020-11-21 06:44

    You can also do it this way, and preserve order:

    // delete duplicates (if any) from 'myArrayList'
    myArrayList = new ArrayList<String>(new LinkedHashSet<String>(myArrayList));
    
    0 讨论(0)
  • 2020-11-21 06:44

    LinkedHashSet will do the trick.

    String[] arr2 = {"5","1","2","3","3","4","1","2"};
    Set<String> set = new LinkedHashSet<String>(Arrays.asList(arr2));
    for(String s1 : set)
        System.out.println(s1);
    
    System.out.println( "------------------------" );
    String[] arr3 = set.toArray(new String[0]);
    for(int i = 0; i < arr3.length; i++)
         System.out.println(arr3[i].toString());
    

    //output: 5,1,2,3,4

    0 讨论(0)
  • 2020-11-21 06:44
        ArrayList<String> list = new ArrayList<String>();
        HashSet<String> unique = new LinkedHashSet<String>();
        HashSet<String> dup = new LinkedHashSet<String>();
        boolean b = false;
        list.add("Hello");
        list.add("Hello");
        list.add("how");
        list.add("are");
        list.add("u");
        list.add("u");
    
        for(Iterator iterator= list.iterator();iterator.hasNext();)
        {
            String value = (String)iterator.next();
            System.out.println(value);
    
            if(b==unique.add(value))
                dup.add(value);
            else
                unique.add(value);
    
    
        }
        System.out.println(unique);
        System.out.println(dup);
    
    0 讨论(0)
  • 2020-11-21 06:44

    If you want to remove duplicates from ArrayList means find the below logic,

    public static Object[] removeDuplicate(Object[] inputArray)
    {
        long startTime = System.nanoTime();
        int totalSize = inputArray.length;
        Object[] resultArray = new Object[totalSize];
        int newSize = 0;
        for(int i=0; i<totalSize; i++)
        {
            Object value = inputArray[i];
            if(value == null)
            {
                continue;
            }
    
            for(int j=i+1; j<totalSize; j++)
            {
                if(value.equals(inputArray[j]))
                {
                    inputArray[j] = null;
                }
            }
            resultArray[newSize++] = value;
        }
    
        long endTime = System.nanoTime()-startTime;
        System.out.println("Total Time-B:"+endTime);
        return resultArray;
    }
    
    0 讨论(0)
  • 2020-11-21 06:45

    This is used for your Custom Objects list

       public List<Contact> removeDuplicates(List<Contact> list) {
        // Set set1 = new LinkedHashSet(list);
        Set set = new TreeSet(new Comparator() {
    
            @Override
            public int compare(Object o1, Object o2) {
                if (((Contact) o1).getId().equalsIgnoreCase(((Contact) o2).getId()) /*&&
                        ((Contact)o1).getName().equalsIgnoreCase(((Contact)o2).getName())*/) {
                    return 0;
                }
                return 1;
            }
        });
        set.addAll(list);
    
        final List newList = new ArrayList(set);
        return newList;
    }
    
    0 讨论(0)
提交回复
热议问题