java-8

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

Java 8 optional time part not working

流过昼夜 提交于 2020-07-04 12:00:51
问题 I am trying to create date time format for optional time part currently I implemented this import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.text.ParseException; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { System.out.println(Ideone.getDate("2017-07-01T00:00:00.0Z")); System.out.println(Ideone.getDate("2017-07

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

CompletableFuture, mutable objects and memory visibility

青春壹個敷衍的年華 提交于 2020-07-04 07:36:10
问题 I'm trying to understand how CompletableFuture in Java 8 interacts with the Java memory model. It seems to me that for programmer sanity, the following should ideally hold true: Actions in the thread that completes a CompletableFuture happen-before any completions dependent stages are executed Actions in the thread that registers a completion creates a dependent stage happen-before the completion dependent stage is executed There's a note in java.util.concurrent documentation saying that:

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 can I create a list with N objects?

泪湿孤枕 提交于 2020-07-02 07:25:41
问题 I am trying to create a list of objects with n elements. I am trying to do this in the most java 8 way as possible. Something similar to the question asked for c# here : Creating N objects and adding them to a list Something like this: List <Objects> getList(int numOfElements) { } 回答1: If I got your question right: List <Object> getList(int numOfElements){ return IntStream.range(0, numOfElements) .mapToObj(Object::new) // or x -> new Object(x).. or any other constructor .collect(Collectors

How can I create a list with N objects?

强颜欢笑 提交于 2020-07-02 07:24:29
问题 I am trying to create a list of objects with n elements. I am trying to do this in the most java 8 way as possible. Something similar to the question asked for c# here : Creating N objects and adding them to a list Something like this: List <Objects> getList(int numOfElements) { } 回答1: If I got your question right: List <Object> getList(int numOfElements){ return IntStream.range(0, numOfElements) .mapToObj(Object::new) // or x -> new Object(x).. or any other constructor .collect(Collectors

How can you make LocalDate to return Date in a specific format?

♀尐吖头ヾ 提交于 2020-06-29 06:21:49
问题 How can you make LocalDate to return Date in a specific format ? Ex . LocalDate ld = LocalDate.now(); The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 . ** Dont say use formatter because formatter will not return date , it will return String which is of no use for me . 回答1: Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A

How can you make LocalDate to return Date in a specific format?

我的未来我决定 提交于 2020-06-29 06:21:10
问题 How can you make LocalDate to return Date in a specific format ? Ex . LocalDate ld = LocalDate.now(); The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 . ** Dont say use formatter because formatter will not return date , it will return String which is of no use for me . 回答1: Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. 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(