1. List转换String
2. List去除null
3. List比较
4. Map比较
import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.*; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.platform.commons.util.StringUtils; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; /** * Description: * * @author maduar maduar@163.com * @version 1.1.1 25/01/2019 */ public class JonerTest { private static String commaSign; private static String equalSign; private static String andSign; @BeforeAll public static void before() { commaSign = ","; equalSign = "="; andSign = "&"; } @Test public void test() { // init List<String> stringList = Lists.newArrayList("my", "cococ", null); // not null List<String> stringList2 = stringList.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList()); String result = "my,cococ"; String t2 = Joiner .on(commaSign) .skipNulls() .join(stringList); List<String> s3 = Lists.newArrayList(Splitter.on(",").split(t2)); // list比较 boolean sign = Iterables.elementsEqual(stringList2, s3); assertEquals(result, t2); assertTrue(sign); } @Test public void test2() { Map<String, String> map = ImmutableMap.of("a", "10", "b", "88"); Map<String, String> map5 = ImmutableMap.of("a", "10"); String result = "a=10&b=88"; // map to string String t2 = Joiner .on(andSign) .withKeyValueSeparator(equalSign) .join(map); // 字符串分割 Map<String, String> map2 = Splitter .on(andSign) .withKeyValueSeparator(equalSign) .split(t2); // map比较 boolean zero = Maps.difference(map5, map2).areEqual(); boolean zero2 = Maps.difference(map, map2).areEqual(); assertFalse(zero); assertTrue(zero2); assertEquals(result, t2); } }