• guava集合类(一)


    你肯定认为,jdk本身提供的collection已经很多了,还没有完全搞明白。

    为什么Google guava还提供了一批新的Collection,实际的原因是程序员在开发中面对的需求千奇百怪、五花八门,jdk那些collection根本不够用。

    一、不可变集合

    jdk也提供了不可变集合的包装方法,但Google任务那些方法冗长而且丑陋,也不能完全保证不改变原始的集合对象,所以提供了一批新的不可变集合

    Where?

    InterfaceJDK or Guava?Immutable Version
    Collection JDK ImmutableCollection
    List JDK ImmutableList
    Set JDK ImmutableSet
    SortedSet/NavigableSet JDK ImmutableSortedSet
    Map JDK ImmutableMap
    SortedMap JDK ImmutableSortedMap
    Multiset Guava ImmutableMultiset
    SortedMultiset Guava ImmutableSortedMultiset
    Multimap Guava ImmutableMultimap
    ListMultimap Guava ImmutableListMultimap
    SetMultimap Guava ImmutableSetMultimap
    BiMap Guava ImmutableBiMap
    ClassToInstanceMap Guava ImmutableClassToInstanceMap
    Table Guava ImmutableTable

    这些集合的使用并不复杂,我简单描述一下,你就会用了。

    //简单好用 
    ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(
      "red",
      "orange",
      "yellow",
      "green",
      "blue",
      "purple");
    //更灵活的实现
     ImmutableSet<Color> GOOGLE_COLORS =
       ImmutableSet.<Color>builder()
           .addAll(WEBSAFE_COLORS)
           .add(new Color(0, 191, 255))
           .build();

    二、新集合类型

    在没有使用guava新数据类型之前,我也没有觉得jdk的集合类不好用,只是经常在开发中,一些很小的需求就写了一大坨代码。

    等用了之后才发现,原来大家都是这么过来的。

    之前写个wordCount需要这么实现

    Map<String, Integer> counts = new HashMap<String, Integer>();
    for (String word : words) {
      Integer count = counts.get(word);
      if (count == null) {
        counts.put(word, 1);
      } else {
        counts.put(word, count + 1);
      }
    }

    代码不算很长,但并不优雅。

    如果使用了MultiSet,你只需要这么写,是不是很方便!

    @Test
        void multiset() {
            String[] words = {"a", "b", "b", "c"};
            Multiset<String> multiset = HashMultiset.create();
            for (String word : words) {
                multiset.add(word);
            }
            System.out.println(multiset);
        }
    
    //输出
    //[a, b x 2, c]

    另外还有MultiMap,它大概相当于Map<K,List<V>>,在一个key对应多个value的场景中,也非常好用。

    @Test
        void multimap() {
            Multimap<Integer, Object> multimap = HashMultimap.create();
            multimap.put(1, "d");
            multimap.put(2, "d1");
            multimap.put(1, "d3");
            multimap.put(1, "d2");
            System.out.println(multimap);
        }
    
    //{1=[d, d2, d3], 2=[d1]}

    大部分情况下,我们根据key查找value,但有时候我们需要反过来通过value查找key,这时候你可以考虑使用bimap

    它需要一个前提条件,一个value不能对应多个key

     @Test
        void bimap() {
            HashBiMap<Object, Object> biMap = HashBiMap.create();
            biMap.put("damiao", 100);
            biMap.put("ermiao", 99);
            Object o = biMap.inverse().get(100);
            System.out.println(o);
        }

    三、Table

    简直是guava中我最爱的对象,它相当于在内存中实现了一张二维表,有rowKey和columnKey和他们唯一映射的一个Value。

    考虑到数据库中存储的都是表,你就能明白这个Table类有多好用。

    Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
    weightedGraph.put(v1, v2, 4);
    weightedGraph.put(v1, v3, 20);
    weightedGraph.put(v2, v3, 5);
    
    weightedGraph.row(v1); 
    weightedGraph.column(v3); 

    四、以Class对象和它的实例作为K,V的Map

      @Test
        void classToInstanceMap(){
            ClassToInstanceMap<Number> numberDefaults = MutableClassToInstanceMap.create();
            numberDefaults.putInstance(Integer.class, Integer.valueOf(0));
            numberDefaults.putInstance(Long.class, 1L);
            numberDefaults.putInstance(Long.class, 2L);
            Long instance = numberDefaults.getInstance(Long.class);
            System.out.println(instance);
        }

    五、以一个区间作为元素和K的RangeSet和RangeMap

     @Test
        void rangeSet() {
            RangeSet<Integer> rangeSet = TreeRangeSet.create();
            rangeSet.add(Range.closed(1, 10));
            rangeSet.add(Range.closed(11, 20));
            boolean contains = rangeSet.contains(4);
            Range<Integer> range = rangeSet.rangeContaining(4);
            System.out.println(range);
            System.out.println(contains);
        }
    
        @Test
        void rangeMap() {
            RangeMap<Integer, Object> rangeMap = TreeRangeMap.create();
            rangeMap.put(Range.closedOpen(1, 10), "hw-node4");
            rangeMap.put(Range.closedOpen(10, 50), "hw-node5");
            Object o = rangeMap.get(34);
            System.out.println(o);
        }
  • 相关阅读:
    xpath语法速查
    负载均衡设计
    Nginx负载均衡配置实例详解
    实现基于DNS的负载均衡
    建立双线服务器(双线游戏服务器)
    python学习
    python多线程概念
    python 多线程就这么简单(续)
    打包一沓开源的 C/C++ 包管理工具送给你!
    讲解开源项目:功能强大的 JS 文件上传库
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15880084.html
Copyright © 2020-2023  润新知