This commit is related to BAEL-7854 (#16489)

This commit aims to add a test class named "HashMapCharacterCountUnitTest".
This commit is contained in:
Mo Helmy 2024-04-25 18:20:58 +02:00 committed by GitHub
parent 211df8196f
commit 7a5cbf3971
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.baeldung.hashmapcharactercount;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static java.util.stream.Collectors.toMap;
import static org.junit.Assert.assertEquals;
public class HashMapCharacterCountUnitTest {
String str = "abcaadcbcb";
@Test
public void givenString_whenUsingStreams_thenVerifyCounts() {
Map<Character, Integer> charCount = str.chars()
.boxed()
.collect(toMap(
k -> (char) k.intValue(),
v -> 1,
Integer::sum));
assertEquals(3, charCount.get('a').intValue());
}
@Test
public void givenString_whenUsingLooping_thenVerifyCounts() {
Map<Character, Integer> charCount = new HashMap<>();
for (char c : str.toCharArray()) {
charCount.merge(c,
1,
Integer::sum);
}
assertEquals(3, charCount.get('a').intValue());
}
}