Java tutorial
//package com.java2s; import java.io.Serializable; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { /** * Build a set of bean from the provided iterable * @param source any kind of iterable * @return a {@link Serializable} {@link Set} containing the same element set */ public static <Bean> Set<Bean> asSet(Iterable<Bean> source) { if (source instanceof Set && source instanceof Serializable) { return (Set<Bean>) source; } else if (source == null) { return Collections.emptySet(); } else { Set<Bean> returned = new HashSet<Bean>(); for (Bean b : source) { returned.add(b); } return returned; } } }