java-stream

Java 8 shows this error. Local variable itemList defined in an enclosing scope must be final or effectively final

﹥>﹥吖頭↗ 提交于 2019-12-22 21:58:52
问题 I am writing the code using java 8 but I iterate a List and then find RestaurantOrderBook using category type. and put that List into a Map . it shows this error: Local variable itemList defined in an enclosing scope must be final or effectively final Query query = new Query(); String categ = category; query.addCriteria(Criteria.where("restaurantId").is(restaurantId)); List<RestaurantOrderBook> itemList = new ArrayList<RestaurantOrderBook>(); itemList = mongoTemplate.find(query,

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

[亡魂溺海] 提交于 2019-12-22 18:52:42
问题 I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

╄→гoц情女王★ 提交于 2019-12-22 18:52:23
问题 I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the

Java 8 stream compare two objects and run a function on them

回眸只為那壹抹淺笑 提交于 2019-12-22 18:20:19
问题 I've a a stream which I want to partition into smaller parts based on matching Id and then apply some proccessing logic on each of the part/element. class BigRequest{ String bId; List<Parts> parts; //getters and setter here } Class Parts{ String pId; String partId; //getters and setter here } I want to segregate and create a list of Parts of size 10 when the partId of different parts are same. How to use the filter or reduce or groupingBy function to compare the two elements and put them to a

Java 8 Stream: Defining collectors based on other collectors

﹥>﹥吖頭↗ 提交于 2019-12-22 18:19:27
问题 I'm new to using Java 8 Stream APIs but I'm looking to use it for the following problem. Say I have a POJO called InputRecord containing name , fieldA , and fieldB properties that can represent each row record of the following: name | fieldA | fieldB ---------------------- A | 100 | 1.1 A | 150 | 2.0 B | 200 | 1.5 A | 120 | 1.3 InputRecord would look like: public class InputRecord { private String name; private Integer fieldA; private BigDecimal fieldB; public String getName() { return name;

How to remove only one max (min) from a list using Java 8 stream API in O(N) time and O(C) space complexity

末鹿安然 提交于 2019-12-22 14:17:14
问题 Here is a code for removing only one of the max values (in this case the first one, but this is irrelevant) from the list. It is O(n) in time and O(n) in space (beyond the input). public List<Integer> removeOneOfTheMax(List<Integer> nums) { int max = Integer.MIN_VALUE; int maxIndex = -1; Iterator<Integer> it = nums.iterator(); for (int i = 0; it.hasNext(); i++) { Integer temp = it.next(); if (max < temp) { maxIndex = i; max = temp; } } nums.remove(maxIndex); return nums; } 1. What would be

Why does Java stream map reduce count my result twice?

耗尽温柔 提交于 2019-12-22 10:59:18
问题 I have this code a: ComparisonResults comparisonResults = requestsList .stream() .map(item -> getResponse(item)) .map(item -> compareToBl(item)) .reduce(new ComparisonResults(), (result1, result2) -> { result1.addSingleResult(result2); // return result1; return new ComparisonResults(result1); }); and this code b: ComparisonResults comparisonResults = requestsList .parallelStream() .map(item -> getResponse(item)) .map(item -> compareToBl(item)) .reduce(new ComparisonResults(), (result1,

Union query with multiple selects post java 8

你。 提交于 2019-12-22 10:43:45
问题 Here is a query that I want to try out in MySQL SELECT A.x FROM A WHERE A.y = 'P' UNION SELECT A.x FROM A WHERE A.y = 'Q' The above is a cut-down, much simpler version of the original query that I am trying. In my original query, each SELECT statement involves multiple tables with INNER JOIN If the possible number of values in 'y' column of table 'A' that I need to query upon is 'n', then my query will involve doing 'n-1' unions on 'n' SELECT statements I know that JOOQ can do union of

java lambda - how to traverse optional list/stream of optionals

£可爱£侵袭症+ 提交于 2019-12-22 10:34:18
问题 Having an Optional List of Optional's like: Optional<List<Optional<String>>> optionalList = Optional.of( Arrays.asList( Optional.empty(), Optional.of("ONE"), Optional.of("TWO"))); How to traverse optionalList to print out the string's ONE and TWO ? What about having an Optional Stream of Optionals? Optional<Stream<Optional<String>>> optionalStream = Optional.of( Stream.of( Optional.empty(), Optional.of("ONE"), Optional.of("TWO"))); Update: Thanks for answers, solution for optionalStream (non

when calculate a^b why parallel not work but parallelStream could

烈酒焚心 提交于 2019-12-22 09:56:27
问题 I want to calculate a^b , e.g. 2^30, public long pow(final int a, final int b) first I used this manner return LongStream.range(0, b).reduce(1, (acc, x) -> a * acc); // 1073741824 Got right result. Then I want to calculate it parallelly, so naturally I changed it to return LongStream.range(0, b).parallel().reduce(1, (acc, x) -> a * acc); // 32 but in this case the result is just 32 . Why? So for supporting parallel I changed it again return Collections.nCopies(b,a).parallelStream().reduce(1,