java-stream

Is there a good way to extract chunks of data from a java 8 stream?

空扰寡人 提交于 2019-12-28 03:04:18
问题 I an ETL process I'm retrieving a lot of entities from a Spring Data Repository. I'm then using a parallel stream to map the entities to different ones. I can either use a consumer to store those new entities in another repository one by one or collect them into a List and store that in a single bulk operation. The first is costly while the later might exceed the available memory. Is there a good way to collect a certain amount of elements in the stream (like limit does), consume that chunk,

Simplest way to print an `IntStream` as a `String`

十年热恋 提交于 2019-12-28 02:49:26
问题 With Java-8 I can easily treat a String (or any CharSequence ) as an IntStream using either the chars or the codePoints method. IntStream chars = "Hello world.".codePoints(); I can then manipulate the contents of the stream IntStream stars = chars.map(c -> c == ' ' ? ' ': '*'); I have been hunting for a tidy way to print the results and I cant even find a simple way. How do I put this stream of int s back into a form that can be printed like I can a String . From the above stars I hope to

In Stream reduce method, must the identity always be 0 for sum and 1 for multiplication?

ε祈祈猫儿з 提交于 2019-12-28 02:05:23
问题 I proceed java 8 learning. I have found interesting behaviour: lets see code sample: // identity value and accumulator and combiner Integer summaryAge = Person.getPersons().stream() //.parallel() //will return surprising result .reduce(1, (intermediateResult, p) -> intermediateResult + p.age, (ir1, ir2) -> ir1 + ir2); System.out.println(summaryAge); and model class: public class Person { String name; Integer age; ///... public static Collection<Person> getPersons() { List<Person> persons =

Java 8 is not maintaining the order while grouping

假如想象 提交于 2019-12-27 14:45:22
问题 I m using Java 8 for grouping by data. But results obtained are not in order formed. Map<GroupingKey, List<Object>> groupedResult = null; if (!CollectionUtils.isEmpty(groupByColumns)) { Map<String, Object> mapArr[] = new LinkedHashMap[mapList.size()]; if (!CollectionUtils.isEmpty(mapList)) { int count = 0; for (LinkedHashMap<String, Object> map : mapList) { mapArr[count++] = map; } } Stream<Map<String, Object>> people = Stream.of(mapArr); groupedResult = people .collect(Collectors.groupingBy

Java 8 is not maintaining the order while grouping

喜欢而已 提交于 2019-12-27 14:45:07
问题 I m using Java 8 for grouping by data. But results obtained are not in order formed. Map<GroupingKey, List<Object>> groupedResult = null; if (!CollectionUtils.isEmpty(groupByColumns)) { Map<String, Object> mapArr[] = new LinkedHashMap[mapList.size()]; if (!CollectionUtils.isEmpty(mapList)) { int count = 0; for (LinkedHashMap<String, Object> map : mapList) { mapArr[count++] = map; } } Stream<Map<String, Object>> people = Stream.of(mapArr); groupedResult = people .collect(Collectors.groupingBy

Java8 : how to aggregate objects from a stream?

混江龙づ霸主 提交于 2019-12-25 18:37:34
问题 Edit IMHO : I think it is not a duplicate because the two questions are trying to solve the problem in different ways and especially because they provide totally different technological skills (and finally, because I ask myself these two questions). Question How to aggregate items from an ordered stream, preferably in an intermediate operation ? Context Following my other question : Java8 stream lines and aggregate with action on terminal line I've got a very large file of the form : MASTER

Convert List of List of Object to a map - using lambdas in java 8

妖精的绣舞 提交于 2019-12-25 18:19:21
问题 I have object structure like below: Order { int code; int id; List<Item>; } Item { int code; int quantity; List<Suborder>; } Suborder { int code; int quantity; } I have an object of O and I want a map from code to B. Whats the correct way to do this? What I tried : 1 - Not working order.getOrderItems().stream().flatMap(l -> l.getOrderItemSuborder().stream()).collect(Collectors.toMap(x -> x.getCode() , Function.identity())); // x.getCode() seems to be not available here :( 2 - Working order

how can i get value through complex logic from list

谁说胖子不能爱 提交于 2019-12-25 17:26:19
问题 I have filter personCountFilter=3 , and have list as below: Rate{ PersonCount:1, LOS:1} Rate{ PersonCount:1, LOS:2} Rate{ PersonCount:1, LOS:3} Rate{ PersonCount:2, LOS:1} Rate{ PersonCount:2, LOS:2} Rate{ PersonCount:2, LOS:3} Rate{ PersonCount:3, LOS:2} Rate{ PersonCount:3, LOS:4} Rate{ PersonCount:3, LOS:5} Rate{ PersonCount:3, LOS:6} Rate{ PersonCount:4, LOS:3} Rate{ PersonCount:5, LOS:7} Rate{ PersonCount:6, LOS:7} After filter my expected: Rate{ PersonCount:2, LOS:1} Rate{ PersonCount:3

Java 8 streams: Convert String[] to Float[] [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 17:17:33
问题 This question already has an answer here : Converting an array of strings to an array of floats in java [duplicate] (1 answer) Closed 2 years ago . I have an array String[] and I'd like to convert to array Float[] Consider e is a String[] supplied via HttpServletRequest::getParameterMap() . I tried: Arrays.stream(e.getValue()).mapToDouble(Float::parseFloat).boxed().toArray(Float[]::new)); Got exception: java.lang.ArrayStoreException: java.lang.Double So then I tried: Arrays.stream(e.getValue(

Java-8 simplify lambda expression with embedded Streams

眉间皱痕 提交于 2019-12-25 09:15:16
问题 Is there a more simple and performant way of doing this, At the end I would need a list of scheduleContainers (List<ScheduleContainer>) final List<ScheduleResponseContent> scheduleResponseContents = new ArrayList<>(); scheduleResponseWrappers.parallelStream().forEach(srw -> scheduleResponseContents.addAll(srw.getScheduleResponseContents())); final List<List<ScheduleContainer>> schedulesOfWeek = new ArrayList<>(); scheduleResponseContents.parallelStream().forEach(src -> schedulesOfWeek.addAll