Skip to content
Advertisement

Tag: java-stream

reusing stream inside a stream java

Hi I just wanted to know what is wrong with my code and how can I improve it as it throws an error “stream has already been operated upon or closed” thank you here is my code: I wanted to loop inside the file and check per line if the characters there exceeded 12 characters or not Answer You cannot

Java filtering the object list to string array with stream

I have a class like this and static function like this The problem is inside of filterLength function I can’t return stream as a String array(I want to return content in the Host class) because List holds Host object how can I return as a string in 1 line code? Answer Transform each object in stream You are successfully streaming,

Reducing ArrayList using MyClass attributes

I have a class, MyClass, like this: If I want to get the sum of amount in an ArrayList of MyClass items, usually I would do this: But now to improve performance, I am trying to use Stream.reduce(). My approach: Is there a better way to do so? And how would the reduction be affected if currAmount or nextAmount is

get a list of unique date from the stream grouping

I have data: Want to get a list of dates: [“12-12-2021”, “13-12-2021”] Using stream, I can get a map: I would like to convert to list in from the stream above. Answer groupingBy is not the best choice in your case. Use distinct instead. It will automatically filter out all duplicates.

Map a List to DTO inside map – java

I have such collection: Map<Integer, List<MyObject>> collection I would like to map the whole list of MyObject to MyObjectDTO and return the whole map with the mapped list. So return will be: Map<Integer, List<MyObjectDto>> collectionWithDtos What is the easiest and quickest way? I’ve checked a couple of ways with streams but none of that produced results as I expected. Thanks

Advertisement