Google Guava 常用集合方法

/**

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

结果:

[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=吴楠}

初始化

Map<String, String> statusMap = ImmutableMap.<String, String>builder()
        .put("1", "待签署")
        .put("2", "签署中")
        .put("3", "已签署")
        .put("4", "已拒签")
        .put("5", "已过期")
        .put("6", "已撤销")
        .put("7", "待填写")
        .put("8", "填写中")
        .build();
posted @ 2021-08-17 14:16  satire  阅读(205)  评论(0编辑  收藏  举报