Convert array to Set
In this chapter you will learn:
Convert array to set
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*from ja v a 2 s .c om*/
public class Main {
public static <T> Set<T> toSet(T... objects) {
Set<T> theSet = new HashSet<T>();
addToCollection(theSet, objects);
return theSet;
}
public static <T> List<T> toList(T... objects) {
List<T> theSet = new ArrayList<T>();
addToCollection(theSet, objects);
return theSet;
}
private static <T> void addToCollection(Collection<T> theCollection,
T... objects) {
for (T object : objects) {
theCollection.add(object);
}
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Array