Java equivalent to Python itertools.groupby ()?

Here is an example using itertools.groupby() in Python:

 from itertools import groupby Positions = [ ('AU', '1M', 1000), ('NZ', '1M', 1000), ('AU', '2M', 4000), ('AU', 'O/N', 4500), ('US', '1M', 2500), ] FLD_COUNTRY = 0 FLD_CONSIDERATION = 2 Pos = sorted(Positions, key=lambda x: x[FLD_COUNTRY]) for country, pos in groupby(Pos, lambda x: x[FLD_COUNTRY]): print country, sum(p[FLD_CONSIDERATION] for p in pos) # -> AU 9500 # -> NZ 1000 # -> US 2500 

Is there any language construct or library support in Java that behaves or can achieve what itertools.groupby() does above?

+6
source share
2 answers

The closest thing could be in the Apache Functor . Check out Examples of Functors, Transformers, Predicates, and Closures in Java for an example.

BTW - don't expect to see something like Python where these things are implemented in 2-3 lines of code. Java was not designed as a language with good functionality. It just does not contain much syntactic sugar like scripting languages. Perhaps in Java 8 it will be seen that all this happens together . That's why Scala came up, looked at this question that I once made when I had really good answers. As you can see in my question, implementing a recursive function is much better in Python than in Java. Java has many good features, but definitely functional programming is not one of them.

+6
source

I had the exact same question and I found neoitertools: http://code.google.com/p/neoitertools/ through the answers to this question: Is there an equivalent to Python's itertools for Java?

0
source

Source: https://habr.com/ru/post/892725/


All Articles