前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Google Guava--2.常用集合方法

Google Guava--2.常用集合方法

作者头像
IT云清
发布2019-01-22 11:21:22
9030
发布2019-01-22 11:21:22
举报
文章被收录于专栏:IT云清IT云清

记录一下Guava中常用的集合方法

代码语言:javascript
复制
/**
 * Author: momo
 * Date: 2018/6/7
 * Description:
 */
public class ListTest {

    public static void main(String[] args) {
        /**List的常见用法*/

        //构造list
        List<Integer> list1 = Lists.newArrayList(1,2,3,4,5,6,7,8,9);
        System.out.println(list1);

        //反转list
        List<Integer> reverseList1 = Lists.reverse(list1);
        System.out.println(reverseList1);

        //切割集合
        List<List<Integer>> partition = Lists.partition(list1, 4);
        partition.stream().forEach(list->System.out.println(list));

        //拷贝为不可变集合
        List list2 = new ArrayList();
        list2.add(11);
        list2.add(41);
        list2.add(51);
        list2.add(12);
        ImmutableList immutableList = ImmutableList.copyOf(list2);

        //创建不可变集合
        ImmutableList<Integer> imList = ImmutableList.of(1, 2, 4, 12);

        //获取不可变字符集合
        ImmutableList<Character> asff = Lists.charactersOf("asff");
        asff.stream().forEach(character -> System.out.println(character));
        
        /**Map的常见用法*/

        Map<String,Object> leftMap = ImmutableMap.of("name", "汪", "age", 18, "address", "陕西", "city", "西安","love","张");
        Map<String,Object> rightMap = ImmutableMap.of("name", "张", "age", 16, "address", "陕西", "city", "西安","home","美国");
        MapDifference<String, Object> deffMap = Maps.difference(leftMap, rightMap);
        //相同的
        Map<String, Object> map = deffMap.entriesInCommon();
        System.out.println("相同的:"+map);
        //同key不同value
        Map<String, MapDifference.ValueDifference<Object>> stringValueDifferenceMap = deffMap.entriesDiffering();
        System.out.println("同key不同value:"+stringValueDifferenceMap);
        //仅仅左边有的
        Map<String, Object> onlyLeft = deffMap.entriesOnlyOnLeft();
        System.out.println("仅仅左边有的:"+onlyLeft);
        //仅仅右边有的
        Map<String, Object> onlyRight = deffMap.entriesOnlyOnRight();
        System.out.println("仅仅右边有的:"+onlyRight);


        /**BiMap*/
        BiMap<Object, Object> biMap = HashBiMap.create();
        biMap.put("张三",54);
        biMap.put("李四",23);
        biMap.put("程思",33);
        biMap.put("吴楠",16);

        //key相同value不同,后面的会覆盖前面的
        biMap.put("吴楠",46);
        //启动程序会报错 java.lang.IllegalArgumentException: value already present: 23
        //biMap.put("张刚",23);
        //强行添加,会覆盖
        //biMap.forcePut("张刚",23);
        System.out.println(biMap);

        //反转 key和value反转
        BiMap<Object, Object> inverseMap = biMap.inverse();
        System.out.println(inverseMap);

    }
}

结果:

代码语言:javascript
复制
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4]
[5, 6, 7, 8]
[9]
a
s
f
f
相同的:{address=陕西, city=西安}
同key不同value:{name=(汪, 张), age=(18, 16)}
仅仅左边有的:{love=张}
仅仅右边有的:{home=美国}
{张三=54, 李四=23, 程思=33, 吴楠=46}
{54=张三, 23=李四, 33=程思, 46=吴楠}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年06月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档