这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core-java-modules/core-java-string-algorithms-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
</parent>

<dependencies>
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik</artifactId>
<version>${jqwik.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
Expand All @@ -22,6 +28,7 @@

<properties>
<emoji-java.version>4.0.0</emoji-java.version>
<jqwik.version>1.7.4</jqwik.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung.uniqueint;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.CRC32;

class StringToUniqueInt {

private static final Map<String, Integer> lookupMap = new HashMap<>();
private static final AtomicInteger counter = new AtomicInteger(Integer.MIN_VALUE);

public static int toIntByHashCode(String value) {
return value.hashCode();
}

public static int toIntByCR32(String value) {
CRC32 crc32 = new CRC32();
crc32.update(value.getBytes());
return (int) crc32.getValue();
}

public static int toIntByCharFormula(String value) {
return value.chars()
.reduce(17, (a, b) -> a * 13 + (b / a));
}

public static int toIntByMD5(String value) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(value.getBytes());
return ((hash[0] & 0xFF) << 24) | ((hash[1] & 0xFF) << 16) | ((hash[2] & 0xFF) << 8) | (hash[3] & 0xFF);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 not supported", e);
}
}

public static int toIntByLookup(String value) {
var found = lookupMap.get(value);
if (found != null) {
return found;
}

var intValue = counter.incrementAndGet();
lookupMap.put(value, intValue);
return intValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.uniqueint;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import net.jqwik.api.Arbitraries;

class StringToUniqueIntUnitTest {

@ParameterizedTest
@MethodSource("implementations")
public void given1kElements_whenMappedToInt_thenItShouldHaveNoDuplicates(Function<String, Integer> implementation) {
Stream<String> strings = uniqueStringsOfSize(1_000); // increase to test higher guarantee

List<Integer> integers = strings.map(implementation)
.toList();

assertThat(integers).doesNotHaveDuplicates();
}

private static Stream<Arguments> implementations() {
return Stream.of(Arguments.of(Named.<Function<String, Integer>> of("toIntByHashCode", StringToUniqueInt::toIntByHashCode)),
Arguments.of(Named.<Function<String, Integer>> of("toIntByCR32", StringToUniqueInt::toIntByCR32)),
Arguments.of(Named.<Function<String, Integer>> of("toIntByCharFormula", StringToUniqueInt::toIntByCharFormula)),
Arguments.of(Named.<Function<String, Integer>> of("toIntByMD5", StringToUniqueInt::toIntByMD5)),
Arguments.of(Named.<Function<String, Integer>> of("toIntByLookup", StringToUniqueInt::toIntByLookup))
);
}

private static Stream<String> uniqueStringsOfSize(int size) {
return Arbitraries.strings()
.filter(it -> !it.isBlank())
.stream()
.ofMinSize(size)
.ofMaxSize(size)
.uniqueElements()
.sample();
}
}