The power of Java stream now available in Python
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
What is Stream?
Stream represents a sequence of objects from a source, which supports aggregate operations.
Following are the characteristics of a Stream:
-
Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.
-
Source − Stream takes Collections, Arrays, or I/O resources as input source.
-
Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, and so on.
-
Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. toList() and toSet() methods are terminals operation which is normally present at the end of the pipelining operation to mark the end of the stream.
-
Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.
Follow this steps for install this tool in the right way.
That's all you need to use Streams:
- python3
sudo apt install python3
- pip
sudo apt install python3-pip
- Install the module with pip
pip install java-stream
- Import the module in your project
from stream import Stream
Here some example of how to use Streams:
- Generate a list of 100 random numbers
Stream.randint(1, 100).limit(100).toList()
- Print the numbers from 1 to 100
Stream.integers().limit(100).forEach(print)
- Generate a list made of zeros with a length of 10
Stream.constant(0).limit(10).toList()
- Generate a list of squares of the number from 1 to 20
IntStream.integers().map(lambda x: x**2).limit(20).toList()
- Generate a set of the first 100 odds number
IntStream.odds().limit(100).toSet()
- Generate a list of all the primes number smaller than 1000
Stream.primes().takeWhile(lambda x: x < 1000).toList()
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the GNU License. See LICENSE
for more information.
Alessandro Mazzoli - @alessandro.py - [email protected]
Project Link: https://github.com/alemazzo/python-java-stream