Merge pull request #6890 from sushant57/master
Move Set Related Articles to new module
This commit is contained in:
commit
696fa0984d
|
@ -1,4 +1,8 @@
|
|||
# Guava
|
||||
|
||||
### Relevant Articles:
|
||||
## Relevant Articles:
|
||||
|
||||
- [Guava – Sets](http://www.baeldung.com/guava-sets)
|
||||
- [Guide to Guava RangeSet](http://www.baeldung.com/guava-rangeset)
|
||||
- [Guava Set + Function = Map](http://www.baeldung.com/guava-set-function-map-tutorial)
|
||||
- [Guide to Guava Multiset](https://www.baeldung.com/guava-multiset)
|
||||
|
|
|
@ -1,14 +1,9 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
|
||||
|
||||
private class SingleEntry implements Entry<K, V> {
|
|
@ -1,15 +1,14 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import com.google.common.base.Function;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class GuavaMapFromSetUnitTest {
|
||||
|
|
@ -1,13 +1,12 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import org.junit.Test;
|
||||
import com.google.common.collect.ImmutableRangeSet;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.RangeSet;
|
||||
import com.google.common.collect.TreeRangeSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class GuavaRangeSetUnitTest {
|
||||
|
||||
|
@ -122,6 +121,5 @@ public class GuavaRangeSetUnitTest {
|
|||
.add(Range.closed(0, 2))
|
||||
.add(Range.closed(3, 5))
|
||||
.add(Range.closed(5, 8)).build();
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class GuavaSetOperationsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCalculatingUnionOfSets_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> union = Sets.union(first, second);
|
||||
assertThat(union, containsInAnyOrder('a', 'b', 'c', 'd'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingCartesianProductOfSets_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b');
|
||||
final Set<Character> second = ImmutableSet.of('c', 'd');
|
||||
final Set<List<Character>> result = Sets.cartesianProduct(ImmutableList.of(first, second));
|
||||
|
||||
final Function<List<Character>, String> func = new Function<List<Character>, String>() {
|
||||
@Override
|
||||
public final String apply(final List<Character> input) {
|
||||
return Joiner
|
||||
.on(" ").join(input);
|
||||
}
|
||||
};
|
||||
|
||||
final Iterable<String> joined = Iterables.transform(result, func);
|
||||
assertThat(joined, containsInAnyOrder("a c", "a d", "b c", "b d"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSetIntersection_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> intersection = Sets.intersection(first, second);
|
||||
assertThat(intersection, containsInAnyOrder('b', 'c'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSetSymmetricDifference_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> intersection = Sets.symmetricDifference(first, second);
|
||||
assertThat(intersection, containsInAnyOrder('a', 'd'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingPowerSet_thenCorrect() {
|
||||
final Set<Character> chars = ImmutableSet.of('a', 'b');
|
||||
final Set<Set<Character>> result = Sets.powerSet(chars);
|
||||
|
||||
final Set<Character> empty = ImmutableSet.<Character> builder().build();
|
||||
final Set<Character> a = ImmutableSet.of('a');
|
||||
final Set<Character> b = ImmutableSet.of('b');
|
||||
final Set<Character> aB = ImmutableSet.of('a', 'b');
|
||||
|
||||
assertThat(result, contains(empty, a, b, aB));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingRangeOfIntegersSet_thenCreated() {
|
||||
final int start = 10;
|
||||
final int end = 30;
|
||||
final ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(start, end), DiscreteDomain.integers());
|
||||
|
||||
assertEquals(21, set.size());
|
||||
assertEquals(10, set.first().intValue());
|
||||
assertEquals(30, set.last().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRangeSet_thenCorrect() {
|
||||
final RangeSet<Integer> rangeSet = TreeRangeSet.create();
|
||||
rangeSet.add(Range.closed(1, 10));
|
||||
rangeSet.add(Range.closed(12, 15));
|
||||
|
||||
assertEquals(2, rangeSet.asRanges().size());
|
||||
|
||||
rangeSet.add(Range.closed(10, 12));
|
||||
assertTrue(rangeSet.encloses(Range.closed(1, 15)));
|
||||
assertEquals(1, rangeSet.asRanges().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertDuplicatesInMultiSet_thenInserted() {
|
||||
final Multiset<String> names = HashMultiset.create();
|
||||
names.add("John");
|
||||
names.add("Adam", 3);
|
||||
names.add("John");
|
||||
|
||||
assertEquals(2, names.count("John"));
|
||||
names.remove("John");
|
||||
assertEquals(1, names.count("John"));
|
||||
|
||||
assertEquals(3, names.count("Adam"));
|
||||
names.remove("Adam", 2);
|
||||
assertEquals(1, names.count("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetTopOcurringElementsWithMultiSet_thenCorrect() {
|
||||
final Multiset<String> names = HashMultiset.create();
|
||||
names.add("John");
|
||||
names.add("Adam", 5);
|
||||
names.add("Jane");
|
||||
names.add("Tom", 2);
|
||||
|
||||
final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet();
|
||||
final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2);
|
||||
assertEquals(2, topTwo.size());
|
||||
assertEquals("Adam", topTwo.get(0));
|
||||
assertEquals("Tom", topTwo.get(1));
|
||||
}
|
||||
}
|
|
@ -11,13 +11,10 @@
|
|||
- [Filtering and Transforming Collections in Guava](http://www.baeldung.com/guava-filter-and-transform-a-collection)
|
||||
- [Guava – Join and Split Collections](http://www.baeldung.com/guava-joiner-and-splitter-tutorial)
|
||||
- [Guava – Lists](http://www.baeldung.com/guava-lists)
|
||||
- [Guava – Sets](http://www.baeldung.com/guava-sets)
|
||||
- [Guava – Maps](http://www.baeldung.com/guava-maps)
|
||||
- [Guide to Guava Multimap](http://www.baeldung.com/guava-multimap)
|
||||
- [Guide to Guava RangeSet](http://www.baeldung.com/guava-rangeset)
|
||||
- [Guide to Guava RangeMap](http://www.baeldung.com/guava-rangemap)
|
||||
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](http://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [Guava Set + Function = Map](http://www.baeldung.com/guava-set-function-map-tutorial)
|
||||
- [Guide to Guava Table](http://www.baeldung.com/guava-table)
|
||||
- [Guide to Guava ClassToInstanceMap](http://www.baeldung.com/guava-class-to-instance-map)
|
|
@ -111,120 +111,6 @@ public class GuavaCollectionTypesUnitTest {
|
|||
assertThat(immutable, contains("John", "Adam", "Jane", "Tom"));
|
||||
}
|
||||
|
||||
// sets
|
||||
|
||||
@Test
|
||||
public void whenCalculateUnionOfSets_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> union = Sets.union(first, second);
|
||||
assertThat(union, containsInAnyOrder('a', 'b', 'c', 'd'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculateSetsProduct_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b');
|
||||
final Set<Character> second = ImmutableSet.of('c', 'd');
|
||||
final Set<List<Character>> result = Sets.cartesianProduct(ImmutableList.of(first, second));
|
||||
|
||||
final Function<List<Character>, String> func = new Function<List<Character>, String>() {
|
||||
@Override
|
||||
public final String apply(final List<Character> input) {
|
||||
return Joiner.on(" ").join(input);
|
||||
}
|
||||
};
|
||||
|
||||
final Iterable<String> joined = Iterables.transform(result, func);
|
||||
assertThat(joined, containsInAnyOrder("a c", "a d", "b c", "b d"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSetIntersection_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> intersection = Sets.intersection(first, second);
|
||||
assertThat(intersection, containsInAnyOrder('b', 'c'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSetSymmetricDifference_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> intersection = Sets.symmetricDifference(first, second);
|
||||
assertThat(intersection, containsInAnyOrder('a', 'd'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingPowerSet_thenCorrect() {
|
||||
final Set<Character> chars = ImmutableSet.of('a', 'b');
|
||||
final Set<Set<Character>> result = Sets.powerSet(chars);
|
||||
|
||||
final Set<Character> empty = ImmutableSet.<Character> builder().build();
|
||||
final Set<Character> a = ImmutableSet.of('a');
|
||||
final Set<Character> b = ImmutableSet.of('b');
|
||||
final Set<Character> aB = ImmutableSet.of('a', 'b');
|
||||
|
||||
assertThat(result, contains(empty, a, b, aB));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateRangeOfIntegersSet_thenCreated() {
|
||||
final int start = 10;
|
||||
final int end = 30;
|
||||
final ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(start, end), DiscreteDomain.integers());
|
||||
|
||||
assertEquals(21, set.size());
|
||||
assertEquals(10, set.first().intValue());
|
||||
assertEquals(30, set.last().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateRangeSet_thenCreated() {
|
||||
final RangeSet<Integer> rangeSet = TreeRangeSet.create();
|
||||
rangeSet.add(Range.closed(1, 10));
|
||||
rangeSet.add(Range.closed(12, 15));
|
||||
|
||||
assertEquals(2, rangeSet.asRanges().size());
|
||||
|
||||
rangeSet.add(Range.closed(10, 12));
|
||||
assertTrue(rangeSet.encloses(Range.closed(1, 15)));
|
||||
assertEquals(1, rangeSet.asRanges().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertDuplicatesInMultiSet_thenInserted() {
|
||||
final Multiset<String> names = HashMultiset.create();
|
||||
names.add("John");
|
||||
names.add("Adam", 3);
|
||||
names.add("John");
|
||||
|
||||
assertEquals(2, names.count("John"));
|
||||
names.remove("John");
|
||||
assertEquals(1, names.count("John"));
|
||||
|
||||
assertEquals(3, names.count("Adam"));
|
||||
names.remove("Adam", 2);
|
||||
assertEquals(1, names.count("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetTopUsingMultiSet_thenCorrect() {
|
||||
final Multiset<String> names = HashMultiset.create();
|
||||
names.add("John");
|
||||
names.add("Adam", 5);
|
||||
names.add("Jane");
|
||||
names.add("Tom", 2);
|
||||
|
||||
final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet();
|
||||
final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2);
|
||||
assertEquals(2, topTwo.size());
|
||||
assertEquals("Adam", topTwo.get(0));
|
||||
assertEquals("Tom", topTwo.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateImmutableMap_thenCreated() {
|
||||
final Map<String, Integer> salary = ImmutableMap.<String, Integer> builder().put("John", 1000).put("Jane", 1500).put("Adam", 2000).put("Tom", 2000).build();
|
||||
|
|
Loading…
Reference in New Issue