java-stream

Parallelism with Streams created from Iterators

给你一囗甜甜゛ 提交于 2020-07-05 09:56:03
问题 Experimenting with streams I ran into the following behavior which I don't quite understand. I created a parallel stream from an iterator and I noticed that it did not seem to be exhibiting parallelism. In the below example I've printed a counter to the console for two parallel streams, one created from an iterator and the other from a list. The stream created from the list exhibited the behavior I expected which was to print the counter in non-sequential order but the stream created from the

What does it mean intermediate operations are lazily executed whereas terminal operations are eagerly executed in java Stream API?

怎甘沉沦 提交于 2020-07-05 05:06:51
问题 list.stream().filter( a-> a < 20 && a > 7).forEach(a -> System.out.println(a)); fiter is lazily executed. forEach is eagerly executed. What does that mean? 回答1: Say you had the following operation. list.stream() .map(a -> a * a) .filter(a -> a > 0 && a < 100) .map(a -> -a) .forEach(a -> System.out.println(a)); The intermediate operations are the maps and filters, the terminal operation is the forEach . If intermediate operations were eagerly executed, then .map(a -> a * a) would immediately

What does it mean intermediate operations are lazily executed whereas terminal operations are eagerly executed in java Stream API?

扶醉桌前 提交于 2020-07-05 05:06:41
问题 list.stream().filter( a-> a < 20 && a > 7).forEach(a -> System.out.println(a)); fiter is lazily executed. forEach is eagerly executed. What does that mean? 回答1: Say you had the following operation. list.stream() .map(a -> a * a) .filter(a -> a > 0 && a < 100) .map(a -> -a) .forEach(a -> System.out.println(a)); The intermediate operations are the maps and filters, the terminal operation is the forEach . If intermediate operations were eagerly executed, then .map(a -> a * a) would immediately

Java 8 streams: find items from one list that match conditions calculated based on values from another list

陌路散爱 提交于 2020-07-04 08:42:47
问题 Have two classes and two corresponding lists: class Click { long campaignId; Date date; } class Campaign { long campaignId; Date start; Date end; String type; } List<Click> clicks = ..; List<Campaign> campaigns = ..; And want to find all Click s in clicks that: Have a corresponding Campaign in campaigns list, i.e., Campaign with the same campaignId AND This Campaign has type = "prospective" AND This Campaigns.start < click.date < Campaigns.end So far I have the following implementation (which

How to find minimum of BigDecimal field in collection with java streams?

青春壹個敷衍的年華 提交于 2020-07-03 03:34:08
问题 I want to use java streams to iterate a list and find the BigDecimal minimum price. The following illustrates, but does not work (because min() cannot accept BigDecimal . class Product { public BigDecimal price; } List<Product> products; products.stream().min((Product) p -> p.price); 回答1: Since BigDecimal already is Comparable , it is as simple as : BigDecimal min = products .stream() .map(Product::getPrice) .min(Comparator.naturalOrder()) .orElse(BigDecimal.ZERO); 回答2: You don't need a

How to group by java 8 and collect ids from parent

谁说胖子不能爱 提交于 2020-06-29 04:53:58
问题 I have a class A Company { String name; Logo logo; } Logo { int color; //can have values=1 (green),2 (red),3 (blue) ... String name; String address; } Output needed : for each type 1,2,3 Group all instances of Logo by color. For each such group what were A.id Give me companies by their color logos. E.g. which companies have logo red? I tried following Input List<Company> company = {//initialization} company.stream().map(e -> e.getLogo()) .collect(Collectors.groupingBy(e -> {Logo b = new Logo(

How to group by java 8 and collect ids from parent

独自空忆成欢 提交于 2020-06-29 04:53:27
问题 I have a class A Company { String name; Logo logo; } Logo { int color; //can have values=1 (green),2 (red),3 (blue) ... String name; String address; } Output needed : for each type 1,2,3 Group all instances of Logo by color. For each such group what were A.id Give me companies by their color logos. E.g. which companies have logo red? I tried following Input List<Company> company = {//initialization} company.stream().map(e -> e.getLogo()) .collect(Collectors.groupingBy(e -> {Logo b = new Logo(

load YAML in Java

断了今生、忘了曾经 提交于 2020-06-28 13:09:22
问题 I am trying to read a YAML file in Java using the following code: public class LoadFile { public static void main(String[] args) throws IOException { Yaml yaml = new Yaml(); InputStream inputStream = LoadFile.class .getClassLoader() .getResourceAsStream("ABSOLUTE PATH TO YAML"); Object obj = yaml.load(inputStream); System.out.println(obj); } } But I get this exception which I don't understand why it occurs. Exception in thread "main" org.yaml.snakeyaml.error.YAMLException: java.io.IOException

Does Collection.parallelStream() imply a happens-before relationship?

拟墨画扇 提交于 2020-06-27 07:42:46
问题 Consider this (completely contrived) Java code: final List<Integer> s = Arrays.asList(1, 2, 3); final int[] a = new int[1]; a[0] = 100; s.parallelStream().forEach(i -> { synchronized (a) { a[0] += i; } }); System.out.println(a[0]); Is this code guaranteed to output "106"? It seems like it is not unless there is a happens-before relationship established by parallelStream() , by which we can know for sure that the first accesses to a[0] in the lambda will see 100 and not zero (according to my

Does Collection.parallelStream() imply a happens-before relationship?

隐身守侯 提交于 2020-06-27 07:42:04
问题 Consider this (completely contrived) Java code: final List<Integer> s = Arrays.asList(1, 2, 3); final int[] a = new int[1]; a[0] = 100; s.parallelStream().forEach(i -> { synchronized (a) { a[0] += i; } }); System.out.println(a[0]); Is this code guaranteed to output "106"? It seems like it is not unless there is a happens-before relationship established by parallelStream() , by which we can know for sure that the first accesses to a[0] in the lambda will see 100 and not zero (according to my