remove Repeat value from Collection - Java java.util

Java examples for java.util:Collection Element Remove

Description

remove Repeat value from Collection

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "book2s.com");
        removeRepeat(collection);//from  w w  w  .ja  v a2 s . c  o m
    }

    public static void removeRepeat(final Collection<?> collection) {
        Iterator<?> iterator = collection.iterator();
        while (iterator.hasNext()) {
            if (collection.contains(iterator.next())) {
                iterator.remove();
            }
        }
    }

    public static <E> List<E> asList(
            @SuppressWarnings("unchecked") final E... array) {
        List<E> list = new ArrayList<>(array.length);
        for (E element : array) {
            list.add(element);
        }
        return list;
    }

    public static <E> List<E> asList(final Iterator<? extends E> iterator) {
        List<E> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }
}

Related Tutorials