Java tutorial
//package com.java2s; /* * Copyright (C) 2005-2014 Alfresco Software Limited. * * This file is part of Alfresco * * Alfresco is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Alfresco is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Alfresco. If not, see <http://www.gnu.org/licenses/>. */ import java.util.HashSet; import java.util.Set; public class Main { /** * This utility method converts a vararg of Objects into a Set<T>. * * @param clazz the Set type to return. * @param objects the objects to be added to the set * @return a Set of objects (any equal objects will of course not be duplicated) * @throws ClassCastException if any of the supplied objects are not of type T. */ public static <T> Set<T> asSet(Class<T> clazz, Object... objects) { Set<T> result = new HashSet<T>(); for (Object obj : objects) { @SuppressWarnings("unchecked") T cast = (T) obj; result.add(cast); } return result; } }