Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alxkm/streamer
Java stream library
https://github.com/alxkm/streamer
collections collections-framework collections-java java java-8 javastreams javautility javautils steam-api
Last synced: 19 days ago
JSON representation
Java stream library
- Host: GitHub
- URL: https://github.com/alxkm/streamer
- Owner: alxkm
- License: mit
- Created: 2022-10-08T20:01:18.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-03T09:13:42.000Z (3 months ago)
- Last Synced: 2024-10-20T21:36:26.700Z (26 days ago)
- Topics: collections, collections-framework, collections-java, java, java-8, javastreams, javautility, javautils, steam-api
- Language: Java
- Homepage:
- Size: 82 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Streamer
[![Java CI with Gradle](https://github.com/alxkm/streamer/actions/workflows/gradle.yml/badge.svg)](https://github.com/alxkm/streamer/actions/workflows/gradle.yml)[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
### Java stream utility library, which someone can find useful
### Usage and StreamUtils class overview
## Overview
`StreamUtils` is a utility class providing various helpful methods for working with Java Streams. It includes methods for stream creation, transformation, collection, and more.## Methods
### unique
```java
public static Collector, List> unique()
```
Returns a collector that accumulates elements into a list while ensuring uniqueness.**Usage Example:**
```java
Stream stream = Stream.of("apple", "banana", "apple");
List uniqueList = stream.collect(StreamUtils.unique());
```### asStream (Iterator)
```java
public static Stream asStream(Iterator iterator)
```
Converts an `Iterator` into a `Stream`.**Usage Example:**
```java
Iterator iterator = List.of("a", "b", "c").iterator();
Stream stream = StreamUtils.asStream(iterator);
```### asStream (Iterable)
```java
public static Stream asStream(Iterable iterable)
```
Converts an `Iterable` into a `Stream`.**Usage Example:**
```java
Iterable iterable = List.of("a", "b", "c");
Stream stream = StreamUtils.asStream(iterable);
```### arrayToCollection
```java
public static Collection arrayToCollection(Class extends Collection> collectionType, T[] array)
```
Converts an array to a collection of the specified type.**Parameters:**
- `collectionType`: The class representing the desired collection type.
- `array`: The array to be converted to a collection.**Usage Example:**
```java
String[] array = {"a", "b", "c"};
List list = (List) StreamUtils.arrayToCollection(ArrayList.class, array);
```### filterByType
```java
public static Stream filterByType(Stream> stream, Class clazz)
```
Filters elements of a stream by a specific type and casts them to that type.**Parameters:**
- `stream`: The original stream.
- `clazz`: The class to filter by.**Usage Example:**
```java
Stream stream = Stream.of(1, "a", 2, "b", 3);
Stream stringStream = StreamUtils.filterByType(stream, String.class);
```### toList
```java
public static List toList(Stream stream)
```
Collects elements of a stream into a list.**Usage Example:**
```java
Stream stream = Stream.of("a", "b", "c");
List list = StreamUtils.toList(stream);
```### toSet
```java
public static Set toSet(Stream stream)
```
Collects elements of a stream into a set.**Usage Example:**
```java
Stream stream = Stream.of("a", "b", "c", "a");
Set set = StreamUtils.toSet(stream);
```### toMap
```java
public static Map toMap(Stream stream, Function super T, ? extends K> keyMapper, Function super T, ? extends U> valueMapper)
```
Collects elements of a stream into a map.**Parameters:**
- `stream`: The original stream.
- `keyMapper`: A function to generate keys.
- `valueMapper`: A function to generate values.**Usage Example:**
```java
Stream stream = Stream.of("a", "bb", "ccc");
Map map = StreamUtils.toMap(stream, String::length, Function.identity());
```### groupBy
```java
public static Map> groupBy(Stream stream, Function super T, ? extends K> classifier)
```
Groups elements of a stream by a classifier function.**Parameters:**
- `stream`: The original stream.
- `classifier`: A function to classify elements.**Usage Example:**
```java
Stream stream = Stream.of("apple", "banana", "apricot", "cherry");
Map> grouped = StreamUtils.groupBy(stream, s -> s.charAt(0));
```### partitionBy
```java
public static Map> partitionBy(Stream stream, Predicate super T> predicate)
```
Partitions elements of a stream by a predicate.**Parameters:**
- `stream`: The original stream.
- `predicate`: A predicate to partition elements.**Usage Example:**
```java
Stream stream = Stream.of(1, 2, 3, 4, 5);
Map> partitioned = StreamUtils.partitionBy(stream, x -> x % 2 == 0);
```### streamOfNullable
```java
public static Stream streamOfNullable(T element)
```
Returns a stream containing a single element if the element is non-null, otherwise returns an empty stream.**Parameters:**
- `element`: The element.**Usage Example:**
```java
Stream stream = StreamUtils.streamOfNullable("a");
```### concatStreams
```java
@SafeVarargs
public static Stream concatStreams(Stream... streams)
```
Concatenates multiple streams into one stream.**Usage Example:**
```java
Stream stream1 = Stream.of("a", "b");
Stream stream2 = Stream.of("c", "d");
Stream result = StreamUtils.concatStreams(stream1, stream2);
```### zip
```java
public static Stream> zip(Stream a, Stream b)
```
Zips two streams into a single stream of pairs.**Parameters:**
- `a`: The first stream.
- `b`: The second stream.**Usage Example:**
```java
Stream streamA = Stream.of("a", "b", "c");
Stream streamB = Stream.of(1, 2, 3);
Stream> zipped = StreamUtils.zip(streamA, streamB);
```### peekAndReturn
```java
public static Stream peekAndReturn(Stream stream, Consumer super T> action)
```
Performs an action on each element of a stream and returns the stream.**Parameters:**
- `stream`: The original stream.
- `action`: The action to perform.**Usage Example:**
```java
Stream stream = Stream.of("a", "b", "c");
Stream result = StreamUtils.peekAndReturn(stream, System.out::println);
```### findFirst
```java
public static Optional findFirst(Stream stream)
```
Finds the first element of a stream, if present.**Usage Example:**
```java
Stream stream = Stream.of("a", "b", "c");
Optional first = StreamUtils.findFirst(stream);
```### batchProcess
```java
public static Stream> batchProcess(Stream stream, int batchSize)
```
Batches elements of a stream into lists of a given size.**Parameters:**
- `stream`: The original stream.
- `batchSize`: The size of the batches.**Usage Example:**
```java
Stream stream = Stream.of(1, 2, 3, 4, 5);
Stream> batches = StreamUtils.batchProcess(stream, 2);
```### parallelFilter
```java
public static Stream parallelFilter(Stream stream, Predicate super T> predicate)
```
Filters elements of a stream in parallel based on a predicate.**Parameters:**
- `stream`: The original stream.
- `predicate`: The predicate to filter elements.**Usage Example:**
```java
Stream stream = Stream.of(1, 2, 3, 4, 5);
List evenNumbers = StreamUtils.parallelFilter(stream, x -> x % 2 == 0).collect(Collectors.toList());
```### parallelMap
```java
public static Stream parallelMap(Stream stream, Function super T, ? extends R> mapper)
```
Maps elements of a stream in parallel using a mapper function.**Parameters:**
- `stream`: The original stream.
- `mapper`: The mapper function to apply to elements.**Usage Example:**
```java
Stream stream = Stream.of(1, 2, 3, 4, 5);
List doubled = StreamUtils.parallelMap(stream, x -> x * 2).collect(Collectors.toList());
```### distinctByKey
```java
public static Predicate distinctByKey(Function super T, ?> keyExtractor)
```
Creates a predicate that maintains state to allow only distinct elements based on a key extractor function.**Parameters:**
- `keyExtractor`: The function to extract keys.**Usage Example:**
```java
Stream stream = Stream.of("apple", "banana", "apricot", "cherry");
List distinct = stream.filter(StreamUtils.distinctByKey(s -> s.charAt(0))).collect(Collectors.toList());
```### streamify (Iterator)
```java
public static Stream streamify(Iterator iterator)
```
Creates a stream from an iterator.**Usage Example:**
```javaIterator iterator = List.of("a", "b", "c").iterator();
Stream stream = StreamUtils.streamify(iterator);
```### streamify (Iterable)
```java
public static Stream streamify(Iterable iterable)
```
Creates a stream from an iterable.**Usage Example:**
```java
Iterable iterable = List.of("a", "b", "c");
Stream stream = StreamUtils.streamify(iterable);
```### flatMapToPair
```java
public static Stream> flatMapToPair(Stream stream, Function super T, Stream> mapper)
```
Flattens a stream of collections to a stream of pairs.**Parameters:**
- `stream`: The original stream.
- `mapper`: The function to generate a stream from each element.**Usage Example:**
```java
Stream stream = Stream.of("a", "b");
Stream> pairs = StreamUtils.flatMapToPair(stream, s -> Stream.of(s.length()));
```### filterNot
```java
public static Stream filterNot(Stream stream, Predicate super T> predicate)
```
Filters elements that do not match the given predicate.**Parameters:**
- `stream`: The original stream.
- `predicate`: The predicate to filter elements.**Usage Example:**
```java
Stream stream = Stream.of(1, 2, 3, 4, 5);
Stream oddNumbers = StreamUtils.filterNot(stream, x -> x % 2 == 0);
```### takeWhile
```java
public static Stream takeWhile(Stream stream, Predicate super T> predicate)
```
Takes elements while the predicate is true.**Parameters:**
- `stream`: The original stream.
- `predicate`: The predicate to test elements.**Usage Example:**
```java
Stream stream = Stream.of(1, 2, 3, 4, 5);
Stream taken = StreamUtils.takeWhile(stream, x -> x < 4);
```### mapToIndex
```java
public static Stream> mapToIndex(Stream stream)
```
Maps elements to their index positions.**Parameters:**
- `stream`: The original stream.**Usage Example:**
```java
Stream stream = Stream.of("a", "b", "c");
Stream> indexed = StreamUtils.mapToIndex(stream);
```## Installation
Include this library in your project by adding the respective files to your classpath.## Requirements
Java 8 or higher is required to use this library.## Testing
The repository includes JUnit tests that validate the functionality of each cache implementation. These tests cover repository described functionality.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details.
Feel free to fork and modify these implementations for your own use cases or contribute to enhance them further. If you have any questions or suggestions, please feel free to reach out or open an issue!
## Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
## Acknowledgments
This repository was inspired by multithreading technics and adapted for educational purposes.
## Contact
For any questions or suggestions, please feel free to reach out or open an issue!