在Java中查找笛卡尔积
我想找到一组元素的笛卡尔积。下面是一个示例
示例 1:
sets : (ab) (bc) (ca)
笛卡尔积是:
abc aba acc aca bbc bba bcc bca
示例 2:
sets : (zyx) b c
笛卡尔积是:
zbc ybc xbc
所以我正在考虑一种在 Java 中执行的算法,它可以找到在编译时定义的特定数量的组的笛卡尔积。
I want to find cartesian product of set of elements. Here's an example
example 1:
sets : (ab) (bc) (ca)
cartesian product is:
abc aba acc aca bbc bba bcc bca
example 2:
sets : (zyx) b c
cartesian product is:
zbc ybc xbc
So I am thinking of an algorithm to execute in Java which can find cartesian product of particular amount of groups defined at compile time at the start.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
Sets.cartesianProduct()
来自 Google 的方法用于生成笛卡尔积的 Guava 库:如果一切都这么简单就好了!
You can use the
Sets.cartesianProduct()
method from Google's Guava libraries to generate Cartesian products:If only everything was that easy!
定义您自己的迭代器/迭代器:
并使用您的数据进行测试:
结果:
Define your own Iterator/Iterable:
And test it with your Data:
Result:
注意:
Set
是不包含重复元素的集合。如果不同集合中有重复元素,则笛卡尔积中的每个集合将仅包含其中一个。您可以创建一个通用方法来获取笛卡尔积并指定存储它的集合类型。例如,
Set
或List
。使用map 和reduce 方法的笛卡尔积
map
方法将集合中的每个元素表示为单例集合,并指定结果的格式。reduce
方法将成对的 2D 集合求和为单个 2D 集合。<一href="https://tio.run/##rVZNb9swDL33V3A5FHbgOmuOWZqtDQrssAJDcxx2UG2mUSpLhiS3y9b@9oySP9M4aQfMgJFQIh8p8pHymj2ys3X6sN3yLFfawpoW4sJyEQ8/neytLQuZW K5kvCjyXHDUPTrGamSZMz/JizvBE0gEMwZuGJfw5wToqdaNZZZ@HhVPIaPdYGE1l/c/fgLT9yaslN3zjRs7XSC9XmM2A4PWwIXfiNUyaDTrh5Td@uByEMHgahBGzcqVW5l3V@Zu5XIQhhT yYY9JTiK5TJi2aDiT37VKi8QGX5lZ0dZkIvEp8oER0A5OCdYCObkP6VJrtnGbu1gN2GgEq rB5YSNQOkVNWdvAI9ObRmOxMRazmlLTinNxZIQORSs@BP0DYie24souj1n4pYxgNh6XxEL7k TLMMEiUM2BUClxSWEwV6hoBadiTzykzOlfBmpuKRU3dyebyO5SvD6fUMvKLd5FgbocAMPa3Fw8a3PcYabaFlb6zVlq8emXrsumSQlzWrYbJCWJ4L7AEa9XB@eh3BLeAvizI1MG9s6Hyzxu EeP3ZYXreg03T5jLownw9hu4J1O4solawweQC@7NTR5wstakOtiCCVBVkI0ViRcuBreHHhN@D5uaRCJYd1Wp3QEq7NtanGRBDudS5FJJU887BMpl7ALLeb/dR2nnjJBcUbkA6c@WPChyq 201Mnxob/xiCEGXzs9akx12gcjZAlqy6nWJfWjDIChjpEoFVyj2vdiDKWd8I5fOAdA3Tqi3 KE0mzCMK5cBFUhlTaxVW1VfR3C8DAsna3Em1a8mh2OoNeXMwv6PBCyKbKM1f2eM06EoT9cS mrpo@XSSIzGwCXoPHKq47BK1PnbmXKkVdkdl971O122TBHM3lCqvcl55XX8ttcd7zJh1F/0 kmerGk4cI0RvvX0Q492il4FF5ZnGbwe0d7KWHZNJeax/AHkn345WR7gLzt8AbZX@I@sUTSr 9xA3SgIByNjiP@yxT@loY7OTDxF69At@93EblbeT7nu5UoAG4UmlUTS3X90ulie0WUxrj7p b0ds1Eb0ZuddNXv9Dcqb7/3j2Z@4Zld6aUoPFa0YfTYOB7qCdbfcl1Ji4e99nTJuFlu/0L" rel="nofollow noreferrer" title="Java (JDK) – 在线尝试">在线尝试!
另请参阅:任意数量集合的笛卡尔积
Note: A
Set
is a collection that contains no duplicate elements. If you have duplicate elements in different sets, then each set from the Cartesian product will contain only one of them.You can create a generic method to get a Cartesian product and specify the types of collections to store it. For example, a
Set
or aList
.Cartesian product using map and reduce approach
The
map
method represents each element of a collection as a singleton collection and specifies the format of the result.The
reduce
method sums pairs of 2D collections into a single 2D collection.Try it online!
See also: Cartesian product of an arbitrary number of sets