zoukankan      html  css  js  c++  java
  • guava库

    官方文档(翻墙访问):http://google.github.io/guava/releases/snapshot-jre/api/docs/

    使用详情:

    1.Set<Category> categorySet = Sets.newHashSet();

    2.List<Integer> categoryIdList = Lists.newArrayList();

    3.

    1 //传统方法:将productIds分割后转成数组,再遍历数组才能添加到集合当中
    2 //这里使用guava提供的方法,直接将其转成集合
    3 List<String> productList = Splitter.on(".").splitToList(productIds);
    4 //这里也使用guava提供的集合判空
    5 if(CollectionUtils.isEmpty(productList)) {
    6     return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    7 }
    View Code

    4.Map result = Maps.newHashMap();

    常用类库介绍:

    以前这么用:

    Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long, List<String>>>();

    guava简化:

    Map<String, Map<Long, List<String>>map = Maps.newHashMap();  

     

    针对不可变集合:

    以前这么用:

    List<String> list = new ArrayList<String>();  

    list.add("a");  

    list.add("b"); 

    guava简化:

    ImmutableList<String> of = ImmutableList.of("a", "b");  

    ImmutableMap<String, String> map = ImmutableMap.of("key1", "value1", "key2", "value2");

    基本类型比较:

    guava简化:

    int compare = Ints.compare(a, b);  

    set的并集,差集和交集:

    SetView union = Sets.union(setA, setB);//并集  

    SetView difference = Sets.difference(setA, setB);//差集  

    SetView intersection = Sets.intersection(setA, setB);//交集  

      

  • 相关阅读:
    并不对劲的网络流
    并不对劲的[noi2006]网络收费
    并不对劲的spoj1812
    48.孩子们的游戏(圆圈中最后剩下的数)
    47.扑克牌顺子
    46.翻转单词顺序
    45.左旋转字符串
    44.和为S的两个数字
    43.和为S的连续正数序列
    42.数组中只出现一次的数字
  • 原文地址:https://www.cnblogs.com/cing/p/7827941.html
Copyright © 2011-2022 走看看