java-stream

Generate String Permutations from multiple Set values (Java 8 Streams)

荒凉一梦 提交于 2020-01-25 08:53:05
问题 I have two Sets - country and state. I want to create all possible permutations from both. import java.util.*; import java.util.stream.Collectors; public class HelloWorld{ public static void main(String []args){ System.out.println("Hello World"); Set<String> countryPermutations = new HashSet<>(Arrays.asList("United States of america", "USA")); Set<String> statePermutations = new HashSet<>(Arrays.asList("Texas", "TX")); Set<String> stateCountryPermutations = countryPermutations.stream()

Nested ArrayList.ParallelStream() in custom ForkJoinPool uses threads unevenly [duplicate]

若如初见. 提交于 2020-01-24 14:19:44
问题 This question already has answers here : Why does stream parallel() not use all available threads? (2 answers) Closed yesterday . I want to use my custom ForkJoinPool to have more parallelism with ArrayList.parallelStream() (by default it uses common pool). I do this: List<String> activities = new ArrayList<>(); for (int i = 0; i < 3000; i++) { activities.add(String.valueOf(i)); } ForkJoinPool pool = new ForkJoinPool(10); pool.submit(() -> activities.parallelStream() .map(s -> { try { System

Nested ArrayList.ParallelStream() in custom ForkJoinPool uses threads unevenly [duplicate]

天涯浪子 提交于 2020-01-24 14:19:17
问题 This question already has answers here : Why does stream parallel() not use all available threads? (2 answers) Closed yesterday . I want to use my custom ForkJoinPool to have more parallelism with ArrayList.parallelStream() (by default it uses common pool). I do this: List<String> activities = new ArrayList<>(); for (int i = 0; i < 3000; i++) { activities.add(String.valueOf(i)); } ForkJoinPool pool = new ForkJoinPool(10); pool.submit(() -> activities.parallelStream() .map(s -> { try { System

Order guarantees using streams and reducing chain of consumers

*爱你&永不变心* 提交于 2020-01-24 12:01:05
问题 So as it goes in the current scenario, we have a set of APIs as listed below: Consumer<T> start(); Consumer<T> performDailyAggregates(); Consumer<T> performLastNDaysAggregates(); Consumer<T> repopulateScores(); Consumer<T> updateDataStore(); Over these, one of our schedulers performs the tasks e.g. private void performAllTasks(T data) { start().andThen(performDailyAggregates()) .andThen(performLastNDaysAggregates()) .andThen(repopulateScores()) .andThen(updateDataStore()) .accept(data); }

Example of stream reduction with distinct combiner and accumulator

∥☆過路亽.° 提交于 2020-01-24 03:00:11
问题 The question is about java.util.stream.Stream.reduce(U identity,BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) method. One of the requirements is that the combiner function must be compatible with the accumulator function; for all u and t, the following must hold: combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t) (*) If the combiner and accumulator are the same, the above equality is automatically true. A BinaryOperator is actually extending

Random Hibernate Exception when using Java 8 ParallelStream in Action Bean

和自甴很熟 提交于 2020-01-23 19:20:09
问题 In my Action Bean I load Entities from a database, use data from those Entities to create new EntityObjects using Java 8 ParallelStream, and store those EntityObjects in a List for later use on a web page. I use the following to create these Objects using the Hibernate mapped Entities: List<Entity> entities = dao.getEntities(); List<Object> entityObjects = new ArrayList<>(); entityObjects.addAll( entities.parallelStream() .map(EntityObject::new) .collect(Collectors.toList()) ); with a

Java Stream API storing lambda expression as variable

只愿长相守 提交于 2020-01-23 09:46:29
问题 This title sounds stupid even to me, but there must be at least somewhat clever way to achieve such effect and I don't know how else to explain it. I need to sort array using sorted in stream API. Here is my stream so far: Arrays.stream(sequence.split(" ")) .mapToInt(Integer::parseInt) .boxed() .sorted((a, b) -> a.compareTo(b)) .forEach(a -> System.out.print(a + " ")); Now I have two different sorts of course - ascending and descending and the sort I need to use is specified in the user input

Map<String,List<String>> to Pair<String,String>

我的未来我决定 提交于 2020-01-23 08:27:34
问题 Using Java 8 Stream API how can I flat a Map to Pair list where left pair value is the map key? Example: If given map was 1 => {1, 2, 3} 2 => {2, 4} Then desired output is the stream of five pairs: (1,1) , (1,2) , (1,3) , (2,2) , (2,4) 回答1: List<Pair<String, String>> result = map.entrySet() .stream() .flatMap( entry -> entry.getValue() .stream() .map(string -> new Pair<>(entry.getKey(), string))) .collect(Collectors.toList()); 来源: https://stackoverflow.com/questions/32189147/mapstring

Does Java's ArrayList.stream().anyMatch() guarantee in-order processing?

穿精又带淫゛_ 提交于 2020-01-23 07:07:07
问题 I have this code: ArrayList<Detector> detectors; detectors.stream().anyMatch(d -> d.detectRead(impendingInstruction, fieldName)); But I would also like to have guarantees that: The list is processed in order, from the first element to the last; As soon an element returns true , evaluations stops immediately Is this always true, or if not, is it at least for all common JDK implementations? 回答1: Your question implies a concern about side-effects of stream operations, otherwise you wouldn't care

java stream Collectors.groupingBy() multiple fields

佐手、 提交于 2020-01-23 06:50:06
问题 Stream<Map.Entry<String, Long>> duplicates = notificationServiceOrderItemDto.getService() .getServiceCharacteristics() .stream() .collect( Collectors.groupingBy( ServiceCharacteristicDto::getName, Collectors.counting() ) ) .entrySet() .stream() .filter(e -> e.getValue() > 1); Optional<String> dupName = duplicates.map(Map.Entry::getKey).findFirst(); works perfect. But I wold like to find duplicates not just with name but also name + value + key That means if name + value + key is the same this