java-stream

Can a Stream be sequentially processed for part of the pipeline, and then as parallel?

眉间皱痕 提交于 2019-12-10 13:13:01
问题 I have the following code which does not work as I intended (a random line, instead of the first, is skipped): Files.lines(path) .skip(1) .parallel() .forEach( System.out::println ) I have a feeling I misunderstood the behavior of Streams. The question is: Can I first treat a stream as sequential (and use "stateful intermediate operations") and then feed it into a parallel forEach ? 回答1: The entire pipeline is either parallel of sequential. Try using forEachOrdered instead of forEach . In my

Is it worth using distinct() with collect(toSet())

余生长醉 提交于 2019-12-10 12:34:51
问题 When collecting the elements of a stream into a set, is there any advantage (or drawback) to also specifying .distinct() on the stream? For example: return items.stream().map(...).distinct().collect(toSet()); Given that the set will already remove duplicates, this seems redundant, but does it offer any performance advantage or disadvantage? Does the answer depend on whether the stream is parallel/sequential or ordered/unordered? 回答1: According to the javadoc, distinct is a stateful

Java 8 Collection stream Collectors.toMap

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:25:36
问题 Why this code is not compile for me? I try to convert List to Map using stream and toMap option List<CountryToPaymentMethodsDisplayRules> paymentMethodDisplayRulesByCountryList = gatway.getCountryPaymentMethodsDisplayRulesByCountry(); Map<PaymentMethod, CountryToPaymentMethodsDisplayRules> countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList .stream() .collect(Collectors.toMap(type -> type.getCountryToPaymentMethodsDisplayRules().getPaymentMethod(), type -> type));

Converting Map<String,String> to List<Object> in Java8 [duplicate]

跟風遠走 提交于 2019-12-10 11:39:59
问题 This question already has an answer here : Java 8 stream Map<K,V> to List<T> (1 answer) Closed last year . I have API which returns Map<String,String> which needs convert into DTO. SubjectIdAndNameDTO (id, name constructor args) id name current implementation using traditional for loop and Map.EnterSet. How can i use feature of Java8 to simply the following code. Map<String, String> map = getSubjectIdAndNameMap(); // How can this code can be improved by using Java8 Stream and method

java 8 use reduce and Collectors grouping by to get list

天涯浪子 提交于 2019-12-10 11:34:29
问题 EDIT **Request to provide answer to First approach also using reduce method ** public class Messages { int id; String message; String field1; String field2; String field3; int audId; String audmessage; //constructor //getter or setters } public class CustomMessage { int id; String msg; String field1; String field2; String field3; List<Aud> list; //getters and setters } public class Aud { int id; String message; //getters and setters } public class Demo { public static void main(String args[])

Collection.toArray() vs Collection.stream().toArray()

前提是你 提交于 2019-12-10 11:28:02
问题 Consider the following code: List<String> myList = Arrays.asList(1, 2, 3); String[] myArray1 = myList.toArray(new String[myList.size()]); String[] myArray2 = myList.stream().toArray(String[]::new); assert Arrays.equals(myArray1, myArray2); It seems to me that using a stream is much simpler. Therefore, I tested the speed of each. List<String> myList = Arrays.asList("1", "2", "3"); double start; start = System.currentTimeMillis(); for (int i = 0; i < 10_000_000; i++) { String[] myArray1 =

How to combine list elements and find the price of largest combination

◇◆丶佛笑我妖孽 提交于 2019-12-10 11:27:49
问题 I have a class that holds details of a particular item like the following: Detail.class Long detailsId; Integer price; List<Long> stackableDetails; /*getters and setters*/ Now, I have a sample dataset like the following: DetailId Price StackableDetails ------------------------------------------ 1011 4$ 1012, 1014 1012 6$ 1011,1013 1013 10$ 1012 1014 8$ 1011 This data set maps to List sampleDetails. Now, based on the stackableDetails information, I have to combine the details and pick the

Do I need a custom Spliterator to avoid extra .stream() call?

巧了我就是萌 提交于 2019-12-10 11:18:28
问题 I have this code which works fine, but I find it ugly. @EqualsAndHashCode public abstract class Actions { @Getter private List<ActionsBloc> blocs; public Actions mergeWith(@NotNull Actions other) { this.blocs = Stream.of(this.blocs, other.blocs) .flatMap(Collection::stream) .collect(groupingBy(ActionsBloc::getClass, reducing(ActionsBloc::mergeWith))) .values() .stream() .filter(Optional::isPresent) .map(Optional::get) .collect(toList()); return this; } } ActionsBloc is a super type which

Java 8 - Streams Nested ForEach with different Collection

∥☆過路亽.° 提交于 2019-12-10 10:39:55
问题 I try to understand the new Java 8 Streams and I tried for days to transfer nested foreach loops over collection in Java 8 Streams. Is it possible to refactor the following nested foreach loops including the if-conditions in Java-8-Streams? If yes what would it look like. ArrayList<ClassInq> Inq = new ArrayList<>(); TreeMap<String, SalesQuot> Quotations = new TreeMap<>(); ArrayList<ClassInq> tempInqAndQuot = new ArrayList<>(); ArrayList<SalesQuot> tempQuotPos = new ArrayList<>(); for(ClassInq

Filter on map of map

陌路散爱 提交于 2019-12-10 10:37:49
问题 I have below map of map and want to filter it based on a value. The result should be assigned back to same map. Please let know what is the best approach for this. Map<String, Map<String, Employee>> employeeMap; < dep1, <"empid11", employee11> <"empid12",employee12> dep2, <"empid21", employee21> <"empid22",employee22> > Filter: employee.getState="MI" I tried like below but i was not able to access the employee object currentMap = currentMap.entrySet().stream() **.filter(p->p.getValue()