将一系列整数分组为一个函数的答案 [英] Grouping a range of integers to the answer of a function

查看:132
本文介绍了将一系列整数分组为一个函数的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一系列整数,我想应用(昂贵的)操作,只筛选那些有趣答案的整数,然后将答案分组。



<这个第一个代码片段很有效,但它在代码和计算中复制了操作(模数2):

  IntStream。范围(1,10).boxed()
.filter(p - >(p%2!= 0))//昂贵的计算
.collect(Collectors.groupingBy(p - >( p%2))); //同样昂贵的
//计算!

// {1 = [1,3,5,7,9]}(正确答案)

我试图先映射到答案,然后过滤,然后分组 - 但最初的整数当然会丢失。

  IntStream.range(1,10).boxed()
.map(p - > p%2)//昂贵的计算
.filter(p - > p!= 0)
.collect(Collectors.groupingBy(p - > p));
$ b $ // {1 = [1,1,1,1,1]}(当然,错误的答案)

我想映射到一个元组或类似的东西,但还没有找到一个干净的方式来做到这一点。

好吧,既然我描述了我会做什么,并且有一些关于它的讨论,我想我应该写下我描述的内容:

  class IntPair {
final int input,result;
IntPair(int i,int r){input = i;结果= r; }
}

映射<整数,列表<整数>>输出=
IntStream.range(1,10)
.mapToObj(i - >新的IntPair(i,i%2))
.filter(pair - > pair.result! (pair - > pair.input,toList())));(b)

请注意,helper类可以(也可能应该)是某种类型的嵌套类,甚至一个本地类。



有一个让字段名称很好的事情是,它可以让您更容易理解正在发生的事情。当我最初撰写这篇文章时,我无意中在分组操作中颠倒了 input result 的角色,因此I得到了不正确的结果。在重新读取代码之后,我很容易发现我正在按输入值而不是 result 值进行分组,而且它也很容易修复。如果我不得不使用 arr [0] arr [1] tuple.t1 tuple.t2


For a range of integers, I would like to apply an ("expensive") operation, filter out only those integers with interesting answers, then group on the answer.

This first snippet works, but it duplicates the operation ("modulus 2") both in code and computation:

IntStream.range(1, 10).boxed()
         .filter(p -> (p % 2 != 0))                     // Expensive computation
         .collect(Collectors.groupingBy(p -> (p % 2))); // Same expensive
                                                        // computation!

// {1=[1, 3, 5, 7, 9]} (Correct answer)

I tried mapping to the answer first, then filter, then group - but the initial Integer of course gets lost along the way:

IntStream.range(1, 10).boxed()
         .map(p -> p % 2)                        // Expensive computation
         .filter(p -> p != 0)
         .collect(Collectors.groupingBy(p -> p));

// {1=[1, 1, 1, 1, 1]} (Of course, wrong answer)

I would like to map to a tuple or something like that, but haven't figured out a clean way to do it.

解决方案

Well, since I described what I'd do, and there's been a bit of discussion about it, I figured I should write down what I was describing:

    class IntPair {
        final int input, result;
        IntPair(int i, int r) { input = i; result = r; }
    }

    Map<Integer, List<Integer>> output =
        IntStream.range(1, 10)
            .mapToObj(i -> new IntPair(i, i % 2))
            .filter(pair -> pair.result != 0)
            .collect(groupingBy(pair -> pair.result,
                mapping(pair -> pair.input, toList())));

Note that the helper class can (and probably should) be a nested class of some sort, or even a local class.

One thing that's nice about having names for the fields is that it does make it easier to understand what's going on. When I initially wrote this up, I had inadvertently reversed the roles of input and result in the grouping operation and thus I got the incorrect result. After rereading the code it was quite easy for me to see that I was grouping by the input values instead result values, and it was also very easy to fix. This would be harder to diagnose and fix if I had to use arr[0] and arr[1] or tuple.t1 and tuple.t2.

这篇关于将一系列整数分组为一个函数的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆