简体   繁体   English

关于Java Streams中Collector和Function参数排序问题

[英]Question about ordering of parameters of Collector and Function in Java Streams

So I'm looking into Java Streams and I have some questions about the meaning of the order of certain parameters.所以我正在研究 Java 流,我对某些参数的顺序的含义有一些疑问。

Some of the context:一些上下文:

public static List<Person> createPeople() {
    return List.of(new Person("Sara", 20), 
                   new Person("Sara", 22), 
                   new Person("Bob", 20), 
                   new Person("Paula",32), 
                   new Person("Paul", 32), 
                   new Person("Jack", 3), 
                   new Person("Jack", 72), 
                   new Person("Jill", 11));
    }
    

List<Person> people = Section.createPeople();

Consider the following code:考虑以下代码:

Map<String, Integer> countByName3 = people.stream()
                                          .collect(groupingBy(Person::getName,
                                                              collectingAndThen(counting(), Long::intValue)));

The point of this code is to output a key value pair where the key is the name of the person and the value is the count of people with that particular name.这段代码的重点是 output 一个键值对,其中键是人名,值是具有该特定名称的人数。

Now where I am at in my understanding is that the collect method is taking Collector objects that are generated by functions in the Collectors class such as groupingBy as its argument.现在,我的理解是,collect 方法将由 Collectors class 中的函数(例如 groupingBy)生成的 Collector 对象作为其参数。

I also understand that there is a Function in the parameter of some of these methods.我也了解到其中一些方法的参数中有一个Function。 Something that interests me is the role that the order of the Function and the Collector in the method that generates a Collector object plays.我感兴趣的是 Function 的顺序和收集器在生成收集器 object 的方法中所起的作用。

For instance, groupingBy() takes its arguments in the order (Function, Collector), while collectingAndThen() takes its arguments in the order (Collector, Function).例如,groupingBy() 按顺序(函数、收集器)获取其 arguments,而 collectAndThen() 按顺序(收集器、函数)获取其 arguments。 Both return Collectors if I am not mistaken.如果我没记错的话,两者都会返回 Collectors。

Thus, what is the meaning of the reversed order, is it indicative of how something is processed under the hood?因此,颠倒顺序的含义是什么,它是否表明某些东西是如何在幕后处理的? And if there is a difference/meaning behind the order, can I get an explanation for that?如果订单背后有差异/含义,我可以得到解释吗?

Thanks very much.非常感谢。

There's nothing magic happening here.这里没有什么神奇的事情发生。 Collectors is just a class. Collectors只是一个 class。 Like any other.像任何其他人一样。 It has static methods.它有 static 方法。 You can invoke them and there is absolutely no magic here.你可以调用它们,这里绝对没有魔法。 You can make your own class with static methods returning instances of the Collector type if you want.如果需要,您可以使用返回Collector类型实例的 static 方法制作自己的 class。

Given that they are just methods, there is absolutely nothing inherent in the language about the order of parameters.鉴于它们只是方法,语言中绝对没有关于参数顺序的固有内容。 The author of the collectingAndThen method respectively the groupingBy method made a decision. collectingAndThen方法的作者分别对groupingBy方法做出了决定。 That's all.就这样。

If you want an explanation for why these authors decided to put the arguments in these orders, you'd have to ask them - that's not an appropriate question for Stack Overflow so I'll give you the benefit of the doubt and assume you knew that and are asking for folks to opine as to the why.如果你想解释为什么这些作者决定将 arguments 放在这些订单中,你必须问他们——这对于 Stack Overflow 来说不是一个合适的问题,所以我会给你怀疑的好处,并假设你知道并要求人们就原因发表意见。 That's not really appropriate for SO either.这也不太适合 SO。 But, to indulge you:但是,为了满足你:

It makes sense to me.对于我,这说得通。 collectingAndThen is in the same order as the very name of that method: "Make a new collector that operates by first applying THIS collector (first argument), but applying THIS finishing transformation after collecting (second argument)". collectingAndThen的顺序与该方法的名称相同:“创建一个新的收集器,首先应用此收集器(第一个参数),但在收集后应用此完成转换(第二个参数)”。

The arguments are in the order one would explain what it does. arguments 的顺序可以解释它的作用。

groupingBy, does the exact same thing: Create a collector by grouping by a certain key; groupingBy,做同样的事情:通过按某个键分组创建一个收集器; to derive this key, toss all things in the stream through THIS transforming function.要导出此密钥,请通过转换 function 将 stream 中的所有东西折腾。 Then, actually collect it all by applying this key value using THIS collector.然后,通过使用这个收集器应用这个键值来实际收集它。

Both are in this 'in the order you mention the parameters if you would explain what it does' order.两者都处于“如果您要解释它的作用,请按照您提到参数的顺序”顺序。 The fact that groupingBy 's first 'thing' happens to be a Function and its second 'thing' happens to be a Collector , whereas for collectingAndThen having its first 'thing' be a Collector and its second 'thing' be a Function is total coincidence. groupingBy的第一个“东西”恰好是Function而它的第二个“东西”恰好是一个Collector ,而collectingAndThen的第一个“东西”是一个Collector ,它的第二个“东西”是一个Function是总计巧合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM