• 使用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]

  • 相关阅读:
    influxdb 使用
    【刷题】如何查找最长链
    学习中的开源框架和系统
    常见错误总结
    开发者必备网站
    计算机基础知识以及常用业务场景
    (1)、hive框架搭建和架构简介
    hadoop安装和配置
    linux基础
    UML学习目录
  • 原文地址:https://www.cnblogs.com/rollenholt/p/3628362.html
Copyright © 2020-2023  润新知