假设我有一个元组 (1, 2)。我想创建一个流,它为我提供该元组值的所有可能组合。 所以我想创建一个如下所示的流:
[(1, 1), (1, 2), (2, 1), (2, 2)]
这是我的元组课程:
public class Tupel<T> {
private final T first;
private final T second;
public Tupel(T first, T second) {
assert first != null;
assert second != null;
this.first = first;
this.second = second;
}
public T getFirst() {
return first;
}
public T getSecond() {
return second;
}
@Override
...
这是我的元组对的课程:
public class TupelPairs<T> {
private TupleSet<T> tupleSet;
public TupelPais(TupleSet<T> tupleSet) {
this.tupelSet = tupleSet;
//This is where I want to create my tuple pairs
}
public Stream<Tupel<T>> getElements() {
return; //This is where I want to return my Stream of those tuple pairs
}
我如何创建这些元组对?
首先,你需要从你的集合中生产出每一双,这是我现在能想到的最简单的方法
List<T> vals = new ArrayList(tupleSet);
Set<Tuple<T>> set = new HashSet();
for(int i = 0; i < vals.size() - 1; i++) {
for (int j = i+1; j < vals.size(); j++) {
set.add(new Tuple(vals.get(i), vals.get(j)));
}
}
现在对于每个元组,我们需要生成其元素的排列,因为元组中只有两个元素,所以总是有 4 个排列。
你可以在网上找到生成排列的算法,你可以将每个排列组合存储在元组中,所以你上面在for循环中生成的一个元组将生成4个元组
[(0,1), (1,2)] //we created this in for loop
这将使用这样的算法产生
[(0,0),(0,1),(1,1),(1,0),(1,1),(1,2),(2,1),(2,2)]
现在有了这个列表,您可以使用为每个集合提供的流API函数轻松地流式传输它。
编辑:因为,这里的要求是用 2 个数字生成所有可能的对,所以您不需要组合逻辑(问题描述不太清楚),您可以简单地运行两个循环并生成所需的所有元组,像这样
public class TupelPairs<T> {
private TupleSet<T> tupleSet;
private final List<Tuple<T>> tuples;
public TupelPais(TupleSet<T> tupleSet) {
this.tupelSet = tupleSet;
// Convert your TupleSet<T> to some list here
// nums here is the List<T>, as I am not sure what is TupleSet here
tuples = new ArrayList<>();
// here we are generating tuple pairs
for(int i = 0; i < nums.size(); i++) {
for (T num : nums) {
tuples.add(new Tuple<T>(nums.get(i), num));
}
}
}
public Stream<Tupel<T>> getElements() {
return tuples.stream(); // Initialized in constructor
}
}
如果假设调用(假设 TupleSet 是 T 的某种集合或列表,并且 T 是整数),这将给出输出
TuplePairs<Integer> pairs = new TuplePairs<Integer>(asList(1,2,3));
pairs.getElements(); // -> [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
这是解决您的任务的示例代码:
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Set<Tuple<Integer>> tuples = createTuples();
Set<Integer> allIntegers = tuples.stream()
.flatMap(integerTuple -> Stream.of(integerTuple.getFirst(), integerTuple.getSecond()))
.collect(Collectors.toSet());
List<Tuple> allNumberCombinations = allIntegers.stream()
.flatMap(firstNumber -> allIntegers.stream().map(secondNumber -> new Tuple<>(firstNumber, secondNumber)))
.sorted(ascendingComparator())
.collect(Collectors.toList());
System.out.println(allNumberCombinations);
}
private static Set<Tuple<Integer>> createTuples() {
return new HashSet<Tuple<Integer>>() {
{
add(new Tuple<>(1, 2));
add(new Tuple<>(3, 2));
}
};
}
private static Comparator<Tuple<Integer>> ascendingComparator() {
Comparator<Tuple<Integer>> comparingFirst = Comparator.comparing(Tuple::getFirst);
Comparator<Tuple<Integer>> comparingSecond = Comparator.comparing(Tuple::getSecond);
return comparingFirst.thenComparing(comparingSecond);
}
@Getter
@ToString
@AllArgsConstructor
@EqualsAndHashCode
private final static class Tuple<T> {
private final T first;
private final T second;
}
}
结果将是:
[Main.Tuple(first=1, second=1), Main.Tuple(first=1, second=2), Main.Tuple(first=1, second=3), Main.Tuple(first=2, second=1), Main.Tuple(first=2, second=2), Main.Tuple(first=2, second=3), Main.Tuple(first=3, second=1), Main.Tuple(first=3, second=2), Main.Tuple(first=3, second=3)]
所提供代码的简短摘要: