Tutorial by Examples

You can create a new class that inherits from HashSet: Set<String> h = new HashSet<String>() {{ add("a"); add("b"); }}; One line solution: Set<String> h = new HashSet<String>(Arrays.asList("a", "b")); Using guava: Se...
Generally, sets are a type of collection which stores unique values. Uniqueness is determined by the equals() and hashCode() methods. Sorting is determined by the type of set. HashSet - Random Sorting Java SE 7 Set<String> set = new HashSet<> (); set.add("Banana"); set.ad...
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. Set have its implementation in various classes like HashSet, TreeSet, LinkedHashSet. For example: HashSet: Set<T> set = new HashSet<T>(); Here T can be String, Integer or any ...
What is a Set? A set is a data structure which contains a set of elements with an important property that no two elements in the set are equal. Types of Set: HashSet: A set backed by a hash table (actually a HashMap instance) Linked HashSet: A Set backed by Hash table and linked list, with pre...
Using a new List List<String> list = new ArrayList<String>(listOfElements); Using List.addAll() method Set<String> set = new HashSet<String>(); set.add("foo"); set.add("boo"); List<String> list = new ArrayList<String&...
Suppose you have a collection elements, and you want to create another collection containing the same elements but with all duplicates eliminated: Collection<Type> noDuplicates = new HashSet<Type>(elements); Example: List<String> names = new ArrayList<>( Arrays....

Page 1 of 1