Generic method to create Set from array - Android java.util

Android examples for java.util:Set

Description

Generic method to create Set from array

Demo Code


//package com.book2s;

import java.util.HashSet;

import java.util.Set;

public class Main {

    public static <T> Set<T> toUniqueSet(T[] array) {
        HashSet<T> set = new HashSet<T>();

        if (array != null) {
            for (T object : array) {
                if (!set.contains(object)) {
                    set.add(object);/*from w w w .ja va2 s.co  m*/
                }
            }
        }

        return set;
    }
}

Related Tutorials