• Map比较


    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);
        }
    }
    

      

  • 相关阅读:
    蓝桥杯基础练习 杨辉三角形
    蓝桥杯基础练习 回文数 特殊的数字
    普及图论三题 P1807 P1113 P 4017
    P3916 图的遍历
    [转载][总结]图论入门:建图,DFS,BFS,拓扑排序
    如何转载博客园的文章
    P1892 [BOI2003]团伙
    P1621 集合
    [模板]线性筛素数(欧拉筛)
    P5076 普通二叉树(简化版)
  • 原文地址:https://www.cnblogs.com/maduar/p/10322243.html
Copyright © 2020-2023  润新知