Code Monkey home page Code Monkey logo

streamer's Introduction

Streamer

Java CI with GradleLicense: 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

public static <T> Collector<T, Set<T>, List<T>> unique()

Returns a collector that accumulates elements into a list while ensuring uniqueness.

Usage Example:

Stream<String> stream = Stream.of("apple", "banana", "apple");
List<String> uniqueList = stream.collect(StreamUtils.unique());

asStream (Iterator)

public static <T> Stream<T> asStream(Iterator<T> iterator)

Converts an Iterator into a Stream.

Usage Example:

Iterator<String> iterator = List.of("a", "b", "c").iterator();
Stream<String> stream = StreamUtils.asStream(iterator);

asStream (Iterable)

public static <T> Stream<T> asStream(Iterable<T> iterable)

Converts an Iterable into a Stream.

Usage Example:

Iterable<String> iterable = List.of("a", "b", "c");
Stream<String> stream = StreamUtils.asStream(iterable);

arrayToCollection

public static <T> Collection<T> 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:

String[] array = {"a", "b", "c"};
List<String> list = (List<String>) StreamUtils.arrayToCollection(ArrayList.class, array);

filterByType

public static <T> Stream<T> filterByType(Stream<?> stream, Class<T> 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:

Stream<Object> stream = Stream.of(1, "a", 2, "b", 3);
Stream<String> stringStream = StreamUtils.filterByType(stream, String.class);

toList

public static <T> List<T> toList(Stream<T> stream)

Collects elements of a stream into a list.

Usage Example:

Stream<String> stream = Stream.of("a", "b", "c");
List<String> list = StreamUtils.toList(stream);

toSet

public static <T> Set<T> toSet(Stream<T> stream)

Collects elements of a stream into a set.

Usage Example:

Stream<String> stream = Stream.of("a", "b", "c", "a");
Set<String> set = StreamUtils.toSet(stream);

toMap

public static <T, K, U> Map<K, U> toMap(Stream<T> 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:

Stream<String> stream = Stream.of("a", "bb", "ccc");
Map<Integer, String> map = StreamUtils.toMap(stream, String::length, Function.identity());

groupBy

public static <T, K> Map<K, List<T>> groupBy(Stream<T> 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:

Stream<String> stream = Stream.of("apple", "banana", "apricot", "cherry");
Map<Character, List<String>> grouped = StreamUtils.groupBy(stream, s -> s.charAt(0));

partitionBy

public static <T> Map<Boolean, List<T>> partitionBy(Stream<T> 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:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Map<Boolean, List<Integer>> partitioned = StreamUtils.partitionBy(stream, x -> x % 2 == 0);

streamOfNullable

public static <T> Stream<T> 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:

Stream<String> stream = StreamUtils.streamOfNullable("a");

concatStreams

@SafeVarargs
public static <T> Stream<T> concatStreams(Stream<T>... streams)

Concatenates multiple streams into one stream.

Usage Example:

Stream<String> stream1 = Stream.of("a", "b");
Stream<String> stream2 = Stream.of("c", "d");
Stream<String> result = StreamUtils.concatStreams(stream1, stream2);

zip

public static <A, B> Stream<Pair<A, B>> zip(Stream<A> a, Stream<B> b)

Zips two streams into a single stream of pairs.

Parameters:

  • a: The first stream.
  • b: The second stream.

Usage Example:

Stream<String> streamA = Stream.of("a", "b", "c");
Stream<Integer> streamB = Stream.of(1, 2, 3);
Stream<Pair<String, Integer>> zipped = StreamUtils.zip(streamA, streamB);

peekAndReturn

public static <T> Stream<T> peekAndReturn(Stream<T> 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:

Stream<String> stream = Stream.of("a", "b", "c");
Stream<String> result = StreamUtils.peekAndReturn(stream, System.out::println);

findFirst

public static <T> Optional<T> findFirst(Stream<T> stream)

Finds the first element of a stream, if present.

Usage Example:

Stream<String> stream = Stream.of("a", "b", "c");
Optional<String> first = StreamUtils.findFirst(stream);

batchProcess

public static <T> Stream<List<T>> batchProcess(Stream<T> 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:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Stream<List<Integer>> batches = StreamUtils.batchProcess(stream, 2);

parallelFilter

public static <T> Stream<T> parallelFilter(Stream<T> 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:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
List<Integer> evenNumbers = StreamUtils.parallelFilter(stream, x -> x % 2 == 0).collect(Collectors.toList());

parallelMap

public static <T, R> Stream<R> parallelMap(Stream<T> 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:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
List<Integer> doubled = StreamUtils.parallelMap(stream, x -> x * 2).collect(Collectors.toList());

distinctByKey

public static <T> Predicate<T> 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:

Stream<String> stream = Stream.of("apple", "banana", "apricot", "cherry");
List<String> distinct = stream.filter(StreamUtils.distinctByKey(s -> s.charAt(0))).collect(Collectors.toList());

streamify (Iterator)

public static <T> Stream<T> streamify(Iterator<T> iterator)

Creates a stream from an iterator.

Usage Example:

Iterator<String> iterator = List.of("a", "b", "c").iterator();
Stream<String> stream = StreamUtils.streamify(iterator);

streamify (Iterable)

public static <T> Stream<T> streamify(Iterable<T> iterable)

Creates a stream from an iterable.

Usage Example:

Iterable<String> iterable = List.of("a", "b", "c");
Stream<String> stream = StreamUtils.streamify(iterable);

flatMapToPair

public static <T, U> Stream<Pair<T, U>> flatMapToPair(Stream<T> stream, Function<? super T, Stream<U>> 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:

Stream<String> stream = Stream.of("a", "b");
Stream<Pair<String, Integer>> pairs = StreamUtils.flatMapToPair(stream, s -> Stream.of(s.length()));

filterNot

public static <T> Stream<T> filterNot(Stream<T> 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:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Stream<Integer> oddNumbers = StreamUtils.filterNot(stream, x -> x % 2 == 0);

takeWhile

public static <T> Stream<T> takeWhile(Stream<T> 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:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Stream<Integer> taken = StreamUtils.takeWhile(stream, x -> x < 4);

mapToIndex

public static <T> Stream<Pair<Integer, T>> mapToIndex(Stream<T> stream)

Maps elements to their index positions.

Parameters:

  • stream: The original stream.

Usage Example:

Stream<String> stream = Stream.of("a", "b", "c");
Stream<Pair<Integer, String>> 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 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!

streamer's People

Contributors

alxkm avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

foobarsome

streamer's Issues

Adding various features for stream processing

Need to add next set of features to library:

filterByType: Filters a stream by a specific class type.
toList: Collects the elements of a stream into a List.
toSet: Collects the elements of a stream into a Set.
toMap: Collects the elements of a stream into a Map.
groupBy: Groups the elements of a stream by a classifier function.
partitionBy: Partitions the elements of a stream by a predicate.
streamOfNullable: Creates a stream from a nullable element.
concatStreams: Concatenates multiple streams into one.
zip: Zips two streams into one.
peekAndReturn: Applies an action to each element of the stream and returns the same element.
findFirst: Returns the first element of the stream, if any.
batchProcess: Processes elements of the stream in batches.
parallelFilter: Filters a stream in parallel.
parallelMap: Maps elements of a stream in parallel.
distinctByKey: Ensures distinct elements based on a key extractor function.
streamify: Converts an Iterator or Iterable into a Stream.

also change documentation and tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.