package com.baeldung.guava; 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 static org.junit.Assert.assertTrue; public class GuavaMapFromSetUnitTest { @Test public void givenStringSet_whenMapsToElementLength_thenCorrect() { Function function = new Function() { @Override public String apply(Integer from) { return Integer.toBinaryString(from); } }; Set set = new TreeSet<>(Arrays.asList(32, 64, 128)); Map map = new GuavaMapFromSet(set, function); assertTrue(map.get(32).equals("100000") && map.get(64).equals("1000000") && map.get(128).equals("10000000")); } @Test public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() { Function function = new Function() { @Override public Integer apply(String from) { return from.length(); } }; Set set = new TreeSet<>(Arrays.asList( "four", "three", "twelve")); Map map = new GuavaMapFromSet(set, function); assertTrue(map.get("four") == 4 && map.get("three") == 5 && map.get("twelve") == 6); } @Test public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() { Function function = new Function() { @Override public Integer apply(String from) { return from.length(); } }; Set set = new TreeSet<>(Arrays.asList( "four", "three", "twelve")); Map map = new GuavaMapFromSet(set, function); set.add("one"); assertTrue(map.get("one") == 3 && map.size() == 4); } }