zoukankan      html  css  js  c++  java
  • Guava学习二(集合、并发、文件)

    不可变集合

    1. ImmutableXXX系列,of,copyOf,builder构造
    2. JDK也提供了Collections.unmodifiableXXX方法把集合包装为不可变形式
    3. 所有的Immutable系列均不接受null
    4. 所有的Immutable系列提供ImmutableList方法方面读取集合中内容
    //有意思的builder写法
    ImmutableSet<Color> GOOGLE_COLORS =
            ImmutableSet.<Color>builder()
                .addAll(WEBSAFE_COLORS)
                .add(new Color(0, 191, 255))
                .build();
    

    新集合类型

    1. MultiSet,ArrayList和Map<T,Integer>混合体,elementSet()不重复元素
      setCount(e,0)等于移除所有e
      实现类:HashMultiSet,ConcurrentHashMultiset
    Multiset<Integer> set = HashMultiset.<Integer>create();
    
    1. MultiMap,单key多值,ListMultimap或SetMultimap接口,它们分别把键映射到List或Set
      实现类:ArraylistMultiMap,HashMultiMap等
    HashMultimap<Integer, Integer> map = HashMultimap.create();   
    
    1. Table<R,C,V>,可以rowMap,row(r)等
      实现类:HashBasedTable(create方法),TreeBasedTable等
    // 使用LinkedHashMaps替代HashMaps
        Table<String, Character, Integer> table = Tables.newCustomTable(
        Maps.<String, Map<Character, Integer>>newLinkedHashMap(),
        new Supplier<Map<Character, Integer>> () {
        public Map<Character, Integer> get() {
        return Maps.newLinkedHashMap();
        }
        });
    
    

    集合工具类

    1. 对应关系,Collections对应Collections2,其余加个s,如Map对应Maps
    2. 提供了静态工厂方法,来初始化集合,通过为工厂方法命名(Effective Java第一条),我们可以提高集合初始化大小的可读性
    List<String> theseElements = Lists.newArrayList("alpha", "beta", "gamma");
    Set<Type> approx100Set = Sets.newHashSetWithExpectedSize(100);
    Multiset<String> multiset = HashMultiset.create();
    
    1. Iterables、FluentIterable工具类

    并发

    1. ListenableFuture可以注册回调
    2. Monitor类,Monitor.Guard完成线程控制
    3. RateLimiter类(RateLimiter.create(4.0)),acquire、tryAcquire

    文件读写

    1. Files类read和writeappend
    2. 文件hash,HashCode hashCode = Files.hash(file, Hashing.md5());
    List<String> readLines = Files.readLines(file,Charsets.UTF_8);
    
    

    Closer关闭

    保证调用Closer.close方法时,所有注册的Closeable对象都正确的关闭

    Closer closer = Closer.create();
    closer.register(reader);
    closer.register(writer);
     catch (Throwable t) {
                throw closer.rethrow(t);
            } finally {
                closer.close();
            }
    
  • 相关阅读:
    ubuntu防火墙设置通过某端口
    pandas入门
    pyplot入门
    numpy教程
    跨域请求 spring boot
    spring boot 启动流程
    代理配置访问
    AOP,拦截器
    spring boot 启动不连接数据库
    Python 3.x 连接数据库(pymysql 方式)
  • 原文地址:https://www.cnblogs.com/lknny/p/12218752.html
Copyright © 2011-2022 走看看