Map比较

Posted maduar

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]
 * @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);
    }
}

  

以上是关于Map比较的主要内容,如果未能解决你的问题,请参考以下文章

将多个输出中的hls属性设置为单独的片段代码

android google map supportmap片段无法在片段中初始化

JavaScript 代码片段

Java如何快速构造JSON字符串

代码片段 - Golang 实现集合操作

Android Studio Map setOnMarkerClickListener 不在片段上工作