Skip to content

Commit e2eb27a

Browse files
authored
Merge pull request eugenp#6629 from sushant57/master
Guide to Guava Multiset
2 parents a3c1b2a + 4316706 commit e2eb27a

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

guava-collections-set/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

guava-collections-set/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>guava-collections-set</artifactId>
6+
<version>0.1.0-SNAPSHOT</version>
7+
<name>guava-collections-set</name>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-java</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-java</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
<!-- test scoped -->
18+
<dependency>
19+
<groupId>org.assertj</groupId>
20+
<artifactId>assertj-core</artifactId>
21+
<version>${assertj.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<finalName>guava-collections-set</finalName>
28+
</build>
29+
30+
<properties>
31+
<!-- util -->
32+
<guava.version>27.1-jre</guava.version>
33+
<!-- testing -->
34+
<assertj.version>3.6.1</assertj.version>
35+
</properties>
36+
37+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.baeldung.guava;
2+
3+
import com.google.common.collect.HashMultiset;
4+
import com.google.common.collect.Multiset;
5+
import org.junit.Test;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12+
13+
public class GuavaMultiSetUnitTest {
14+
15+
@Test
16+
public void givenMultiSet_whenAddingValues_shouldReturnCorrectCount() {
17+
Multiset<String> bookStore = HashMultiset.create();
18+
bookStore.add("Potter");
19+
bookStore.add("Potter");
20+
bookStore.add("Potter");
21+
22+
assertThat(bookStore.contains("Potter")).isTrue();
23+
assertThat(bookStore.count("Potter")).isEqualTo(3);
24+
}
25+
26+
@Test
27+
public void givenMultiSet_whenRemovingValues_shouldReturnCorrectCount() {
28+
Multiset<String> bookStore = HashMultiset.create();
29+
bookStore.add("Potter");
30+
bookStore.add("Potter");
31+
32+
bookStore.remove("Potter");
33+
assertThat(bookStore.contains("Potter")).isTrue();
34+
assertThat(bookStore.count("Potter")).isEqualTo(1);
35+
}
36+
37+
@Test
38+
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
39+
Multiset<String> bookStore = HashMultiset.create();
40+
bookStore.setCount("Potter", 50);
41+
assertThat(bookStore.count("Potter")).isEqualTo(50);
42+
}
43+
44+
@Test
45+
public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() {
46+
Multiset<String> bookStore = HashMultiset.create();
47+
assertThatThrownBy(() -> bookStore.setCount("Potter", -1))
48+
.isInstanceOf(IllegalArgumentException.class);
49+
}
50+
51+
@Test
52+
public void givenMultiSet_whenSettingCountWithEmptySet_shouldBeSuccessful() {
53+
Multiset<String> bookStore = HashMultiset.create();
54+
assertThat(bookStore.setCount("Potter", 0, 2)).isTrue();
55+
}
56+
57+
@Test
58+
public void givenMultiSet_whenSettingCountWithCorrectValue_shouldBeSuccessful() {
59+
Multiset<String> bookStore = HashMultiset.create();
60+
bookStore.add("Potter");
61+
bookStore.add("Potter");
62+
63+
assertThat(bookStore.setCount("Potter", 2, 52)).isTrue();
64+
}
65+
66+
@Test
67+
public void givenMultiSet_whenSettingCountWithIncorrectValue_shouldFail() {
68+
Multiset<String> bookStore = HashMultiset.create();
69+
bookStore.add("Potter");
70+
bookStore.add("Potter");
71+
72+
assertThat(bookStore.setCount("Potter", 5, 52)).isFalse();
73+
}
74+
75+
@Test
76+
public void givenMap_compareMultiSetOperations() {
77+
Map<String, Integer> bookStore = new HashMap<>();
78+
bookStore.put("Potter", 3);
79+
80+
assertThat(bookStore.containsKey("Potter")).isTrue();
81+
assertThat(bookStore.get("Potter")).isEqualTo(3);
82+
83+
bookStore.put("Potter", 2);
84+
assertThat(bookStore.get("Potter")).isEqualTo(2);
85+
86+
bookStore.put("Potter", null);
87+
assertThat(bookStore.containsKey("Potter")).isTrue();
88+
89+
bookStore.put("Potter", -1);
90+
assertThat(bookStore.containsKey("Potter")).isTrue();
91+
}
92+
}

0 commit comments

Comments
 (0)