Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Java stream library

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md
NotificationsYou must be signed in to change notification settings

alxkm/streamer

Repository files navigation

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

publicstatic <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)

publicstatic <T>Stream<T>asStream(Iterator<T>iterator)

Converts anIterator into aStream.

Usage Example:

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

asStream (Iterable)

publicstatic <T>Stream<T>asStream(Iterable<T>iterable)

Converts anIterable into aStream.

Usage Example:

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

arrayToCollection

publicstatic <T>Collection<T>arrayToCollection(Class<?extendsCollection>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

publicstatic <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

publicstatic <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

publicstatic <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

publicstatic <T,K,U>Map<K,U>toMap(Stream<T>stream,Function<?superT, ?extendsK>keyMapper,Function<?superT, ?extendsU>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

publicstatic <T,K>Map<K,List<T>>groupBy(Stream<T>stream,Function<?superT, ?extendsK>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

publicstatic <T>Map<Boolean,List<T>>partitionBy(Stream<T>stream,Predicate<?superT>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

publicstatic <T>Stream<T>streamOfNullable(Telement)

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

@SafeVarargspublicstatic <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

publicstatic <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

publicstatic <T>Stream<T>peekAndReturn(Stream<T>stream,Consumer<?superT>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

publicstatic <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

publicstatic <T>Stream<List<T>>batchProcess(Stream<T>stream,intbatchSize)

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

publicstatic <T>Stream<T>parallelFilter(Stream<T>stream,Predicate<?superT>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

publicstatic <T,R>Stream<R>parallelMap(Stream<T>stream,Function<?superT, ?extendsR>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

publicstatic <T>Predicate<T>distinctByKey(Function<?superT, ?>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)

publicstatic <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)

publicstatic <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

publicstatic <T,U>Stream<Pair<T,U>>flatMapToPair(Stream<T>stream,Function<?superT,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

publicstatic <T>Stream<T>filterNot(Stream<T>stream,Predicate<?superT>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

publicstatic <T>Stream<T>takeWhile(Stream<T>stream,Predicate<?superT>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

publicstatic <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 theLICENSE 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!

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp