Sets is an utility class to work with
Set instances. By using Sets class you can achieve following.
By using Sets class, you can perform
following operations.
a. Get Cartesian product between sets
b. Find complement of a collection
c. Find difference between two sets
d. Filter set data
e. Create immutable EnumSet
f. Find intersection of two sets
g. Create concurrent HashSet
h. Create CopyOnWriteArraySet
i. Create new EnumSet
j. Create new IdentityHashSet
k. Create new LinkedHashSet
l. Create a Set backed by Map
m. Create new TreeSet
n. Compute PowerSet of a set
o. find symmetric difference between two sets
p. Find union of two sets
q. Create NavigableSet
Get Cartesian product between sets.
Sets class provides following methods
to get Cartesian product of given sets.
public static <B> Set<List<B>> cartesianProduct(Set<?
extends B>... sets)
public static <B> Set<List<B>>
cartesianProduct(List<? extends Set<? extends B>> sets)
import java.util.HashSet; import java.util.List; import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static void main(String args[]) { Set<Integer> intSet = new HashSet<>(); Set<Character> charSet = new HashSet<>(); intSet.add(1); intSet.add(2); intSet.add(3); charSet.add('A'); charSet.add('B'); charSet.add('C'); charSet.add('D'); Set<List<Object>> cartesianSet = Sets.cartesianProduct(intSet, charSet); System.out.println("cartesianSet size " + cartesianSet.size()); for (List<Object> list : cartesianSet) { System.out.println(list); } } }
Output
cartesianSet size 12 [1, A] [1, B] [1, C] [1, D] [2, A] [2, B] [2, C] [2, D] [3, A] [3, B] [3, C] [3, D]
Find complement of a collection
Sets class provides following methods
to create an EnumSet consisting of all enum values that are not in the
specified collection.
public static <E extends Enum<E>> EnumSet<E>
complementOf(Collection<E> collection)
public static <E extends Enum<E>> EnumSet<E>
complementOf(Collection<E> collection, Class<E> type)
import java.util.EnumSet; import com.google.common.collect.Sets; public class SetsEx { enum Day { MON, TUE, WED, THU, FRI, SAT, SUN; }; public static void main(String args[]) { EnumSet<Day> origSet = EnumSet.of(Day.MON, Day.TUE); EnumSet<Day> complementSet = Sets.complementOf(origSet); System.out.println(complementSet); } }
Output
[WED, THU, FRI, SAT, SUN]
Find difference between two sets
You can find difference between two
sets using difference method of Sets class.
public static <E> SetView<E> difference(final Set<E>
set1, final Set<?> set2)
Returns an unmodifiable view of the
difference of two sets.
import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static void main(String args[]) { Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4); Set<Integer> set2 = Sets.newHashSet(3, 4, 5, 6); System.out.println("(set1-set2) : " + Sets.difference(set1, set2)); System.out.println("(set2-set1) : " + Sets.difference(set2, set1)); } }
Output
(set1-set2) : [1, 2]
(set2-set1) : [5, 6]
Filter set data
Sets class provides following methods
to filter data in sets.
public static <E> Set<E> filter(Set<E> unfiltered,
Predicate<? super E> predicate)
public static <E> SortedSet<E> filter(SortedSet<E>
unfiltered, Predicate<? super E> predicate)
public static <E> NavigableSet<E> filter(NavigableSet<E>
unfiltered, Predicate<? super E> predicate)
For example, following application,
remove all odd numbers from given set.
import java.util.Set; import com.google.common.base.Predicate; import com.google.common.collect.Sets; public class SetsEx { public static Predicate<Integer> evenPredicate = new Predicate<Integer>() { @Override public boolean apply(Integer input) { return (input % 2 == 0); } }; public static void main(String args[]) { Set<Integer> set = Sets.newHashSet(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Set<Integer> filteredSet = Sets.filter(set, evenPredicate); System.out.println("Elements in set are " + set); System.out.println("Elements in filteredSet are " + filteredSet); } }
Output
Elements in set are [1, 2, 3, 4, 5, 6,
7, 8, 9, 10]
Elements in filteredSet are [2, 4, 6,
8, 10]
Create immutable Set
Sets class provides following methods
to create Immutable set.
public static <E extends Enum<E>> ImmutableSet<E>
immutableEnumSet(E anElement, E... otherElements)
public static <E extends Enum<E>> ImmutableSet<E>
immutableEnumSet(Iterable<E> elements)
Above methods return an immutable set
instance containing the given enum elements.
import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; public class SetsEx { public static enum Day { MON("Monday", "Day to analyze work"), TUE("Tuesday", "Day to preapre plan"), WED("Wednesday", "Dat to Design"), THU( "Thursday", "Day to code"), FRI("Friday", "Day to test and deploy"), SAT("Saturday", "Day to enjoy"), SUN( "Sunday", "Day to sleep"); String day, message; Day(String day, String message) { this.day = day; this.message = message; } public String toString() { return day + " : " + message; } }; public static void main(String args[]) { ImmutableSet<Day> weekEndSet = Sets.immutableEnumSet(Day.SAT, Day.SUN); System.out.println(weekEndSet); } }
Output
[Saturday : Day to enjoy, Sunday : Day
to sleep]
Find intersection of two sets
Sets class provides intersection method
to find intersection between two sets.
public static <E>
SetView<E> intersection(final Set<E> set1, final Set<?> set2)
import java.util.Set; import com.google.common.collect.Sets; import com.google.common.collect.Sets.SetView; public class SetsEx { public static void main(String args[]) { Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set<Integer> set2 = Sets.newHashSet(3, 4, 5, 6, 7, 8); SetView<Integer> setView = Sets.intersection(set1, set2); System.out.println(setView); } }
Output
[3, 4, 5]
Create concurrent HashSet
Sets class provide newConcurrentHashSet
method to create new Concurrent HashSet.
public static <E> Set<E>
newConcurrentHashSet(Iterable<? extends E> elements)
public static <E> Set<E>
newConcurrentHashSet()
import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static void main(String args[]) { Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5); Set<Integer> mySet = Sets.newConcurrentHashSet(set1); System.out.println(mySet); } }
Output
[1, 2, 3, 4, 5]
Create CopyOnWriteArraySet
Sets class provides following methods
to create new CopyOnWriteArraySet. CopyOnWriteArraySet use internal
CopyOnWriteArrayList for all of its operations.
public static <E> CopyOnWriteArraySet<E>
newCopyOnWriteArraySet()
public static <E> CopyOnWriteArraySet<E>
newCopyOnWriteArraySet(Iterable<? extends E> elements)
Create new EnumSet
Sets class provide newEnumSet method to
create new EnumSet with given elements.
import java.util.EnumSet; import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static enum Day { MON("Monday", "Day to analyze work"), TUE("Tuesday", "Day to preapre plan"), WED("Wednesday", "Dat to Design"), THU( "Thursday", "Day to code"), FRI("Friday", "Day to test and deploy"), SAT("Saturday", "Day to enjoy"), SUN( "Sunday", "Day to sleep"); String day, message; Day(String day, String message) { this.day = day; this.message = message; } public String toString() { return day + " : " + message; } }; public static void main(String args[]) { Set<Day> set = Sets.newHashSet(Day.MON, Day.THU, Day.SUN); EnumSet<Day> enumSet = Sets.newEnumSet(set, Day.class); System.out.println(enumSet); } }
Output
[Monday : Day to analyze work, Thursday
: Day to code, Sunday : Day to sleep]
Create new HashSet
Sets class provides following methods
to create new empty HashSet, HashSet with elements.
public static <E> HashSet<E> newHashSet()
public static <E>
HashSet<E> newHashSet(E... elements)
public static <E>
HashSet<E> newHashSetWithExpectedSize(int expectedSize)
public static <E>
HashSet<E> newHashSet(Iterable<? extends E> elements)
public static <E>
HashSet<E> newHashSet(Iterator<? extends E> elements)
Create new IdentityHashSet
Sets class provides following method to
create new IdentityHashSet.
public static <E> Set<E>
newIdentityHashSet()
Create new LinkedHashSet
Sets class provides following methods
to create LinkedHashSet instances.
public static <E> LinkedHashSet<E> newLinkedHashSet()
public static <E> LinkedHashSet<E>
newLinkedHashSetWithExpectedSize(int expectedSize)
public static <E> LinkedHashSet<E>
newLinkedHashSet(Iterable<? extends E> elements)
Create a Set backed by Map
Sets class provides following method to
create Set backed by Map.
public static <E> Set<E> newSetFromMap(Map<E, Boolean>
map)
Create new TreeSet
Sets class provides following methods
to create new TreeSet.
public static <E extends
Comparable> TreeSet<E> newTreeSet()
public static <E>
TreeSet<E> newTreeSet(Comparator<? super E> comparator)
public static <E extends
Comparable> TreeSet<E> newTreeSet(Iterable<? extends E>
elements)
Compute PowerSet of a set
You can compute PowerSet of a set using
powerSet method of Sets class.
public static <E>
Set<Set<E>> powerSet(Set<E> set)
import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static void main(String args[]) { Set<Integer> set = Sets.newHashSet(1, 2, 3, 4); Set<Set<Integer>> powerSet = Sets.powerSet(set); for (Set<Integer> mySet : powerSet) { System.out.println(mySet); } } }
Output
[] [1] [2] [1, 2] [3] [1, 3] [2, 3] [1, 2, 3] [4] [1, 4] [2, 4] [1, 2, 4] [3, 4] [1, 3, 4] [2, 3, 4] [1, 2, 3, 4]
Find symmetric difference between two sets
Sets class provides symmetricDifference
method to compute symmetric difference between two sets.
import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static void main(String args[]) { Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4); Set<Integer> set2 = Sets.newHashSet(3, 4, 5, 6); Sets.SetView<Integer> symmDiff = Sets.symmetricDifference(set1, set2); System.out.println(symmDiff); } }
Output
[1, 2, 5, 6]
Find union of two sets
Sets class provides union method to
find union of two sets.
import java.util.Set; import com.google.common.collect.Sets; public class SetsEx { public static void main(String args[]) { Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4); Set<Integer> set2 = Sets.newHashSet(3, 4, 5, 6); Sets.SetView<Integer> union = Sets.union(set1, set2); System.out.println(union); } }
Output
[1, 2, 3, 4, 5, 6]
Create NavigableSet
Sets class provide following methods to
create NavigableSet.
public static <E> NavigableSet<E>
unmodifiableNavigableSet(NavigableSet<E> set)
public static <E> NavigableSet<E>
synchronizedNavigableSet(NavigableSet<E> navigableSet)
No comments:
Post a Comment