• guava集合类(二)


    有经验的同学都知道,如果Abc是一个类,那么它没有实现的功能,可能会出现在Abcs类中。

    比如Arrays是对数组功能的扩展,Collections是对集合功能的扩展。Files、Paths是对File、Path类相关操作的扩展。

    guava由此延伸,构建了更多对原生集合的加强操作,都集中在Abcs类中。

    一、构造

    这些工具类中,首先会有基于构建者模式的静态构造方法。

    InterfaceJDK or Guava?Corresponding Guava utility class
    Collection JDK Collections2
    List JDK Lists
    Set JDK Sets
    SortedSet JDK Sets
    Map JDK Maps
    SortedMap JDK Maps
    Queue JDK Queues
    Multiset Guava Multisets
    Multimap Guava Multimaps
    BiMap Guava Maps
    Table Guava Tables

    //
    Iterables Iterable<Integer> concatenated = Iterables.concat( Ints.asList(1, 2, 3), Ints.asList(4, 5, 6)); // concatenated has elements 1, 2, 3, 4, 5, 6 String lastAdded = Iterables.getLast(myLinkedHashSet); String theElement = Iterables.getOnlyElement(thisSetIsDefinitelyASingleton); //Sets Set<String> wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight"); Set<String> primes = ImmutableSet.of("two", "three", "five", "seven"); SetView<String> intersection = Sets.intersection(primes, wordsWithPrimeLength); //Lists List<Integer> countDown = Lists.reverse(theList); // {5, 4, 3, 2, 1} List<List<Integer>> parts = Lists.partition(countUp, 2); // {{1, 2}, {3, 4}, {5}} //Maps ImmutableMap<Integer, String> stringsByIndex = Maps.uniqueIndex(strings, new Function<String, Integer> () { public Integer apply(String string) { return string.length(); } }); Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3); Map<String, Integer> right = ImmutableMap.of("b", 2, "c", 4, "d", 5); MapDifference<String, Integer> diff = Maps.difference(left, right); //Multisets Multiset<String> multiset1 = HashMultiset.create(); multiset1.add("a", 2); Multiset<String> multiset2 = HashMultiset.create(); multiset2.add("a", 5); multiset1.containsAll(multiset2); // returns true: all unique elements are contained, // even though multiset1.count("a") == 2 < multiset2.count("a") == 5 Multisets.containsOccurrences(multiset1, multiset2); // returns false Multisets.removeOccurrences(multiset2, multiset1); // multiset2 now contains 3 occurrences of "a" multiset2.removeAll(multiset1); // removes all occurrences of "a" from multiset2, even though multiset1.count("a") == 2 multiset2.isEmpty(); // returns true //Multimaps ImmutableSet<String> digits = ImmutableSet.of( "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); Function<String, Integer> lengthFunction = new Function<String, Integer>() { public Integer apply(String string) { return string.length(); } }; ImmutableListMultimap<Integer, String> digitsByLength = Multimaps.index(digits, lengthFunction); //Tables 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(); } });

    二、反转

    ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
    multimap.putAll("b", Ints.asList(2, 4, 6));
    multimap.putAll("a", Ints.asList(4, 2, 1));
    multimap.putAll("c", Ints.asList(2, 5, 3));
    
    TreeMultimap<Integer, String> inverse = Multimaps.invertFrom(multimap, TreeMultimap.<String, Integer> create());
    // note that we choose the implementation, so if we use a TreeMultimap, we get results in order
    /*
     * inverse maps:
     *  1 => {"a"}
     *  2 => {"a", "b", "c"}
     *  3 => {"c"}
     *  4 => {"a", "b"}
     *  5 => {"c"}
     *  6 => {"b"}
     */
    
    
    Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
    SetMultimap<String, Integer> multimap = Multimaps.forMap(map);
    // multimap maps ["a" => {1}, "b" => {1}, "c" => {2}]
    Multimap<Integer, String> inverse = Multimaps.invertFrom(multimap, HashMultimap.<Integer, String> create());
    // inverse maps [1 => {"a", "b"}, 2 => {"c"}]
    
    
    
    ImmutableSet<String> digits = ImmutableSet.of(
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine");
    Function<String, Integer> lengthFunction = new Function<String, Integer>() {
      public Integer apply(String string) {
        return string.length();
      }
    };
    ImmutableListMultimap<Integer, String> digitsByLength = Multimaps.index(digits, lengthFunction);
    /*
     * digitsByLength maps:
     *  3 => {"one", "two", "six"}
     *  4 => {"zero", "four", "five", "nine"}
     *  5 => {"three", "seven", "eight"}
     */
  • 相关阅读:
    实现业务逻辑的几种不同方法,及其优缺点 事务脚本、表模块、活动记录、领域模型
    JQuery Tree Jquery树型菜单插件
    SQL实现表名更改,列名更改,约束更改
    Differences Between NHibernate and Entity Framework
    CSS菜单,图片阴影,表单样式
    事务
    纯CSS三列布局
    Quartz Develop Practice One
    创建WinPE启动盘、常用imagex指令、常用dism指令
    Using User Defined Types in COM & ATL
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15880482.html
Copyright © 2020-2023  润新知