这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.baeldung.map.randomkey;

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

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.junit.jupiter.api.Test;

class RandomKeyUnitTest {

@Test
void givenMap_whenUsingRandomClass_thenGetRandomKey() {
Map<String, String> countries = Map.of("Japan", "Tokyo", "Spain", "Madrid");

List<String> countryNames = new ArrayList<String>(countries.keySet());
int randomIndex = new Random().nextInt(countryNames.size());
String randomCountry = countryNames.get(randomIndex);

assertThat(countries).containsKey(randomCountry);
}

@Test
void givenMap_whenUsingMathRandomMethod_thenGetRandomKey() {
Map<String, String> countries = Map.of("Japan", "Tokyo", "Spain", "Madrid");

List<String> countryNames = new ArrayList<String>(countries.keySet());
int randomIndex = (int) (Math.random() * countryNames.size());
String randomCountry = countryNames.get(randomIndex);

assertThat(countries).containsKey(randomCountry);
}

@Test
void givenMap_whenUsingThreadLocalRandomClass_thenGetRandomKey() {
Map<String, String> countries = Map.of("Japan", "Tokyo", "Spain", "Madrid");

List<String> countryNames = new ArrayList<String>(countries.keySet());
int randomIndex = ThreadLocalRandom.current()
.nextInt(countryNames.size());
String randomCountry = countryNames.get(randomIndex);

assertThat(countries).containsKey(randomCountry);
}

@Test
void givenMap_whenUsingSecureRandomClass_thenGetRandomKey() {
Map<String, String> countries = Map.of("Japan", "Tokyo", "Spain", "Madrid");

List<String> countryNames = new ArrayList<String>(countries.keySet());
int randomIndex = new SecureRandom().nextInt(countryNames.size());
String randomCountry = countryNames.get(randomIndex);

assertThat(countries).containsKey(randomCountry);
}

@Test
void givenMap_whenUsingCollectionsShuffleMethod_thenGetRandomKey() {
Map<String, String> countries = Map.of("Japan", "Tokyo", "Spain", "Madrid");

List<String> countryNames = new ArrayList<String>(countries.keySet());
Collections.shuffle(countryNames);
String randomCountry = countryNames.get(0);

assertThat(countries).containsKey(randomCountry);
}

}