---
title: "使用Guava来计算笛卡尔积"
author: "wen66"
date: "2014-05-05T14:11:14Z"
tags: []
categories: ["编程语言"]
slug: "guava"
draft: false
lastmod: "2014-05-05T14:11:14Z"
id: 2060715
url: "https://wen66.iteye.com/blog/2060715"
---

比如我们要计算3个List的笛卡尔积，每个list的内容都是\['a', 'b', 'c']， 请看下面的代码： \[code="java"] public class CartesianProductUtil { public static void main(String\[] args) { ImmutableSet charList = ImmutableSet.of('a', 'b', 'c'); Set&gt; set = Sets.cartesianProduct(charList, charList, charList); for (List characters : set) { System.out.println(characters); } } } \[/code] 输出 \[code="java"] \[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] \[/code]