zoukankan      html  css  js  c++  java
  • guava入门学习3(集合工具)

    3.1 Iterables

    3.1.1concat() 拼接集合
    List<String> l1 = Lists.newArrayList("1");
    List<String> l2 = Lists.newArrayList("2");
    Iterable<String> concat = Iterables.concat(l1, l2);
    concat.forEach(System.out::println);

    3.1.2元素在指定集合中出现的次数
    int frequency = Iterables.frequency(l1, "1”);

    3.1.3Partition() 按指定元素个数拆分集合
    List<String> l1 = Lists.newArrayList("1","2","3","4","5");
    Iterable<List<String>> partition = Iterables.partition(l1, 2);
    partition.forEach(System.out::println);

    3.1.4比较两个集合,当两个集合中的元素值都相等且顺序一样时,返回true
    List<String> l1 = Lists.newArrayList("1","2","3","4","51");
    List<String> l2 = Lists.newArrayList("1","2","3","4",new String("5")+"1");
    boolean b = Iterables.elementsEqual(l1, l2);

    3.1.5截取集合的前指定位数作为视图遍历
    List<String> l1 = Lists.newArrayList("1","2","3","4",”5”,”6”,”7");
    Iterable<String> limit = Iterables.limit(l1, 6);


    3.2Sets

    3.2.1powerSet() 返回所有的子集,包含空集和自身全集
    Set<String> set1 = Sets.newHashSet("1","2");
    Set<Set<String>> sets = Sets.powerSet(set1);
    sets.forEach(System.out::println);

    3.2.2Sets.cartesianProduct(Set ...) 返回集合的笛卡尔积 集合个数不限
    Set<String> set1 = Sets.newHashSet("1","2");
    Set<String> set2 = Sets.newHashSet("a","b");
    Set<List<String>> lists = Sets.cartesianProduct(Lists.newArrayList(set1, set2));
    lists.forEach(System.out::println);


    3.3Maps

    3.3.1difference.entriesInCommon 返回两个map中健值对都相等
    Map<String,String> m1 = Maps.newHashMapWithExpectedSize(16);
    Map<String,String> m2 = Maps.newHashMapWithExpectedSize(16);
    m1.put("a","a1");
    m2.put("a","a1");
    m1.put("b","b1");
    m2.put("b","b2");
    m1.put("c","c1");
    MapDifference<String, String> difference = Maps.difference(m1, m2);
    Map<String, String> stringStringMap = difference.entriesInCommon();
    System.out.println(stringStringMap);

    3.3.2difference.entriesDiffering()  返回不相等的键值对
    Map<String, MapDifference.ValueDifference<String>> stringValueDifferenceMap = difference.entriesDiffering();
    System.out.println(stringValueDifferenceMap);

    3.3.3只存在于左边的键值对
    Map<String, String> onlyOnLeftMap = difference.entriesOnlyOnLeft();
    System.out.println(onlyOnLeftMap);

    3.3.4只存在于右边的键值对
    Map<String, String> onlyOnRightMap = difference.entriesOnlyOnRight();
    System.out.println(onlyOnRightMap);


    3.4Tables

    3.4.1转置二维表
    Table<String,String,String> table = HashBasedTable.create();
    table.put("r1","c1","r1c1");
    table.put("r1","c2","r1c2");
    table.put("r2","c1","r2c1");
    System.out.println(table);
    Table<String, String, String> transpose = Tables.transpose(table);
    System.out.println(transpose);
    —输出:
    {r1={c1=r1c1, c2=r1c2}, r2={c1=r2c1}}
    {c1={r1=r1c1, r2=r2c1}, c2={r1=r1c2}}
     
     
  • 相关阅读:
    【背包问题】
    【CodeVS1037】取数游戏
    【CodeVS2226】飞行棋
    网线主管
    第一周计划
    毕业设计每日总结2020/2/16
    毕业设计每日总结2020/2/15
    毕业设计每日总结2020/2/14
    毕业设计每日总结2020/2/13
    毕业设计每日总结2020/2/12
  • 原文地址:https://www.cnblogs.com/input4hua/p/7859559.html
Copyright © 2011-2022 走看看