remove String Duplicates in String List - Android java.util

Android examples for java.util:List

Description

remove String Duplicates in String List

Demo Code


//package com.java2s;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    /**//  w w w .j a  v a2  s.c  o m
     * @return a list of strings that has duplicates removed
     */
    @Nullable
    public static List<String> removeStringDuplicates(List<String> list) {
        if (isEmpty(list)) {
            return null;
        }

        Set<String> set = new HashSet<>();
        set.addAll(list);
        return new ArrayList<>(set);
    }

    /**
     * Is this list empty. Checks for null as well as size.
     *
     * @return true if empty, false if not
     */
    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Is this list empty. Checks for null as well as size.
     *
     * @return true if empty, false if not
     */
    public static boolean isEmpty(Object[] collection) {
        return collection == null || collection.length == 0;
    }
}

Related Tutorials