zoukankan      html  css  js  c++  java
  • 使用Guava来计算笛卡尔积

    以前做项目的时候计算笛卡尔积的时候,总是使用各种for循环来嵌套,最后往往在Sonar代码检查的时候总是会报警说for循环嵌套过深。

    今天才知道Guava原来已经为我们提供了优雅的计算笛卡尔积的方法。

    比如我们要计算3个List的笛卡尔积,每个list的内容都是['a', 'b', 'c'], 请看下面的代码:

    public class CartesianProductUtil {
    
        public static void main(String[] args) {
            ImmutableSet<Character> charList = ImmutableSet.of('a', 'b', 'c');
            Set<List<Character>> set = Sets.cartesianProduct(charList, charList, charList);
            for (List<Character> characters : set) {
                System.out.println(characters);
            }
        }
    }
    

      输出为:

    [a, a, a]
    [a, a, b]
    [a, a, c]
    [a, b, a]
    [a, b, b]
    [a, b, c]
    [a, c, a]
    [a, c, b]
    [a, c, c]
    [b, a, a]
    [b, a, b]
    [b, a, c]
    [b, b, a]
    [b, b, b]
    [b, b, c]
    [b, c, a]
    [b, c, b]
    [b, c, c]
    [c, a, a]
    [c, a, b]
    [c, a, c]
    [c, b, a]
    [c, b, b]
    [c, b, c]
    [c, c, a]
    [c, c, b]
    [c, c, c]

  • 相关阅读:
    火狐显示不安全链接
    signal信号
    I/O缓冲
    [pe530]GCD of Divisors
    学校寒假集训作业
    纳克萨玛斯「GDOI2007」(网络流)
    [清华集训2016] 汽水
    有上下界网络流
    [AHOI2014]支线剧情(有上下界的网络流)
    [SCOI2016]妖怪
  • 原文地址:https://www.cnblogs.com/rollenholt/p/3628362.html
Copyright © 2011-2022 走看看