GUAVA的常用方法汇总

代码笔记

@RunWith(JUnit4.class)
public class SimpleTest {

    /**
     * 
     *    com.google.guava
     *    guava
     *    19.0
     * 
     */
    @Test
    public void testGuava() {
//        basicOperate();
        CollectionOperate();
    }

    private void CollectionOperate() {
        /*
        * set集合交集, 并集, 差集
        * */
        HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5);
        HashSet<Integer> set2 = Sets.newHashSet(4, 5, 6, 7, 8);

        //交集
        Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);//45
        //并集
        Sets.SetView<Integer> union = Sets.union(set1, set2);//12345678
        //差集,set1比set2多出的部分
        Sets.SetView<Integer> difference1 = Sets.difference(set1, set2);//123
        Sets.SetView<Integer> difference2 = Sets.difference(set2, set1);//678
    }

    private void typeTrans() {
        /*
        * 集合类型转换
        * */
        List<Integer> integers = Lists.newArrayList(1, 2, 3, 4);
        //注意包路径
        List<String> strings = com.google.common.collect.Lists.transform(integers, Functions.toStringFunction());
    }

    private void basicOperate() {
        /*
         * 一般的可变集合直接使用Maps./Lists./Sets.等方法直接调用
         * 不可变集合如下所示:
         * (1)在多线程操作下,是线程安全的
         * (2)所有不可变集合会比可变集合更有效的利用资源
         * (3)中途不可改变,使用add、remove等方法直接抛异常
         * */
        ImmutableList<String> list = ImmutableList.of("a", "b");
        ImmutableMap<Integer, String> map = ImmutableMap.of(1, "a", 2, "b", 3, "c");
        ImmutableSet<String> set = ImmutableSet.of("a", "a");//排重

        /*
        * 将list作为value,任意不重复主键作为key形成一个map>
        * */
        ArrayListMultimap<Integer, String> multimap = ArrayListMultimap.create();
        multimap.put(1, "a");
        multimap.put(1, "a");
        multimap.put(1, "c");
        List<String> strings = multimap.get(1); //返回的就是list集合
        System.out.println(JacksonUtil.obj2json(strings));

        /*
        * 双主键table,根据行和列进行查询,可以查询行得到列map,或者根据列得到行map进行遍历
         */
        Table<Integer, String, String> tables = HashBasedTable.create();
        tables.put(1, "property", "rj");
        tables.put(2, "property", "kk");
        tables.put(2, "value", "airline");
        String value = tables.get(2, "value");

        /*
        * 把集合转换为特定规则的字符串
        * */
        String join = Joiner.on(",").withKeyValueSeparator("=").join(map); //1=a,2=b,3=c
        String join1 = Joiner.on(",").join(list); //a,b 如果集合初始化并且没数据,则直接为空字符串;guava使用快速失败操作,在编写时告警未初始化集合

        /*
        * 将字符串转为集合
        * */
        String str1="1-2-3-4- 5-  6  ";
        String str2 = "1=A,  2=B,  3=C ";
        List<String> split1 = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(str1);//去除-,去除空串,去除空格
        Map<String, String> split2 = Splitter.on(",").omitEmptyStrings().trimResults().withKeyValueSeparator("=").split(str2);

        /*
        * 正则分隔字符串
         */
        String input = "aa.dd,,ff,,.";
        List<String> s = Splitter.onPattern("[.|,]").omitEmptyStrings().trimResults().splitToList(input);
    }
}

你可能感兴趣的:(Java之常用基础类)