- Notifications
You must be signed in to change notification settings - Fork1
Java stream library
License
MIT, MIT licenses found
Licenses found
alxkm/streamer
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
StreamUtils
is a utility class providing various helpful methods for working with Java Streams. It includes methods for stream creation, transformation, collection, and more.
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());
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);
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);
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);
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);
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);
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);
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());
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));
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);
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");
@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);
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);
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);
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);
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);
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());
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());
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());
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);
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);
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()));
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);
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);
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);
Include this library in your project by adding the respective files to your classpath.
Java 8 or higher is required to use this library.
The repository includes JUnit tests that validate the functionality of each cache implementation. These tests cover repository described functionality.
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!
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This repository was inspired by multithreading technics and adapted for educational purposes.
For any questions or suggestions, please feel free to reach out or open an issue!
About
Java stream library