Java 8按顺序生成唯一的数字对

时间:2015-01-11 08:18:14

标签: java functional-programming

我开发了可以生成唯一数字对的代码,并且工作正常

   Stream<String> stream = IntStream.range(0, 5).boxed().flatMap(a -> IntStream.range(0, 5).mapToObj(b -> a + ":" + b).distinct());
   stream.forEach(System.out::println);

输出

0:0
0:1
0:2
0:3
0:4
1:0
1:1
1:2
1:3
1:4
2:0
2:1
2:2
2:3
2:4
3:0
3:1
3:2
3:3
3:4
4:0
4:1
4:2
4:3
4:4

但是如何生成对,使输出看起来像

0:0
0:1
1:0
1:1
0:2
2:0
1:2
2:1
2:2
0:3
3:0
3:1
1:3
2:3
3:2
3:3
0:4
4:0
1:4
4:1
2:4
4:2
3:4
4:3
4:4

这个想法不是首先使用0然后使用1生成所有对,依此类推,我想生成像x:x x:y y:x y:y这样的对,依此类推。请仅在Java8中提供答案,因为对于java&lt; 8,我可以轻松编写for循环并实现它。谢谢

3 个答案:

答案 0 :(得分:3)

如果您需要不同的订单,可以在生成它们时对它们进行排序。

一种方法可能是生成所有id,最多为0,1,2,3,4等。

此解决方案符合给定的顺序

// 0 to 5-1 boxed so we can flatMap IntStream to Stream<String> later.
IntStream.range(0, 5).boxed()
        // generate pairs of ?::n and n::? or only one if the same.
        .flatMap(n -> IntStream.rangeClosed(0, n).boxed()
                 // generate the pair, dropping duplicates.
                 .flatMap(m -> Stream.of(m + ":" + n, n + ":" + m).distinct())
        // print the results.
        ).forEach(System.out::println);

使用import static它可能看起来像这样。

  

range(0,5).boxed()。flatMap(n - &gt; range(0,n + 1).boxed()。flatMap(m - &gt; of(m +“:”+ n,n +“:”+ m).distinct()))   .forEach(下::的println);

打印

0:0
0:1
1:0
1:1
0:2
2:0
1:2
2:1
2:2
0:3
3:0
1:3
3:1
2:3
3:2
3:3
0:4
4:0
1:4
4:1
2:4
4:2
3:4
4:3
4:4

这产生了以不同方式提出的要求。

// numbers 0 to 5-1
IntStream.range(0, 5)
         // make them boxed so we can do a flatMap to a Stream<String> later.
         // IntStream can't flapMap to a stream of Objects. 
         .boxed()
         // create two streams of ?::n and n::?
        .flatMap(n -> Stream.of(
                // stream is 0 .. n-1 to avoid duplicate n with next stream.
                // generate ?::n
                IntStream.range(0, n).mapToObj(m -> m + ":" + n),
                // stream is 0 .. n of n::?
                IntStream.rangeClosed(0, n).mapToObj(m -> n + ":" + m)
        // combine the two streams into one so that stream can be flatMappped.
        ).flatMap(s -> s))
        // print the results.
        .forEach(System.out::println);

打印

0:0
0:1
1:0
1:1
0:2
1:2
2:0
2:1
2:2
0:3
1:3
2:3
3:0
3:1
3:2
3:3
0:4
1:4
2:4
3:4
4:0
4:1
4:2
4:3
4:4

答案 1 :(得分:2)

此代码有效:

Stream<String> stream =
    IntStream.range(0, 5)
             .boxed()
             .flatMap(a -> IntStream.range(0, 5)
                                    .boxed()
                                    // keep only pairs where a>=b
                                    .filter(b-> {return a>=b;})
                                    // for each pair (a,b) , generate two pairs -
                                    // (b:a) and (a:b) 
                                    .flatMap(b -> Stream.of(b+":"+a,a+":"+b))
                                    .distinct());
stream.forEach(System.out::println);

输出:

0:0
0:1
1:0
1:1
0:2
2:0
1:2
2:1
2:2
0:3
3:0
1:3
3:1
2:3
3:2
3:3
0:4
4:0
1:4
4:1
2:4
4:2
3:4
4:3
4:4

答案 2 :(得分:0)

像这样:

        System.out.println("0;0");
        for (int i = 1; i < 5; i++) {
            for (int j = 0; j < i; j++) {
                System.out.println(j + ";" + i);
                System.out.println(i + ";" + j);
            }
            System.out.println(i + ";" + i);
        }
相关问题