java-8

Filtering a list using Java 8 lambda expressions

非 Y 不嫁゛ 提交于 2020-06-22 15:28:12
问题 I have a Project class: class Project { List<Name> names; int year; public List<Name> getNames(){ return names; } } Then I have another main function where I have a List<Project> and have to filter that list of projects on the basis of year and get names list as the result. Can you please tell me how to do it using java 8 lambda expressions? Thanks 回答1: Well, you didn't state the exact filtering condition, but assuming you wish to filter elements by a given year: List<Name> names = projects

Interleave elements in a stream with separator

穿精又带淫゛_ 提交于 2020-06-22 13:46:26
问题 Is there a nice way to use Java streams to interleave elements in a stream with a separator of the same type? // Expected result in is list: [1, 0, 2, 0, 3] List<Integer> is = Stream.of(1, 2, 3).intersperse(0).collect(toList()); This is similar to the intersperse function in Haskell and other functional languages. I have seen many examples of how to join strings in a similar way but have not found any solutions for general lists. 回答1: You can do it with flatMap, but you'll get an additional

Interleave elements in a stream with separator

谁说胖子不能爱 提交于 2020-06-22 13:46:11
问题 Is there a nice way to use Java streams to interleave elements in a stream with a separator of the same type? // Expected result in is list: [1, 0, 2, 0, 3] List<Integer> is = Stream.of(1, 2, 3).intersperse(0).collect(toList()); This is similar to the intersperse function in Haskell and other functional languages. I have seen many examples of how to join strings in a similar way but have not found any solutions for general lists. 回答1: You can do it with flatMap, but you'll get an additional

Getting the the current Instant in a specific TimeZone

亡梦爱人 提交于 2020-06-22 12:37:07
问题 I have tried to get the current Instance on a specific timeZone but it does not work as expected. Any idea on how to do this ? Here is what i did: Instant.now(Clock.system(ZoneId.of("America/Los_Angeles"))).truncatedTo(ChronoUnit.SECONDS) However the instant time returned is always UTC. I changed many time the ZoneID and it is always wrong. Please advise. EDIT: I'm interacting with an application that generate log with timeStamp and i need to operate over those event. For instant if I start

Stream of maps to map

こ雲淡風輕ζ 提交于 2020-06-22 11:11:36
问题 How can I flatten a Stream of Map s (of the same types) to a single Map in Java 8? Map<String, Long> toMap(Stream<Map<String, Long>> stream) { return stream. ??? } 回答1: My syntax may be a bit off, but flatMap should do most of the work for you : Map<String, Long> toMap(Stream<Map<String, Long>> stream) { return stream.flatMap (map -> map.entrySet().stream()) // this would create a flattened // Stream of all the map entries .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); //

How can I return LocalDate.now() in milliseconds?

a 夏天 提交于 2020-06-22 09:35:21
问题 I create date now: ZoneId gmt = ZoneId.of("GMT"); LocalDateTime localDateTime = LocalDateTime.now(); LocalDate localDateNow = localDateTime.toLocalDate(); Then I want return this date in milliseconds: localDateNow.atStartOfDay(gmt) - 22.08.2017 localDateNow.atStartOfDay(gmt).toEpochSecond(); - 1503360000 (18.01.70) How can I return LocalDate.now() in milliseconds? 回答1: Calling toInstant().toEpochMilli() , as suggested by @JB Nizet's comment, is the right answer, but there's a little and

No static method metafactory (OptaPlanner)

丶灬走出姿态 提交于 2020-06-17 14:54:08
问题 Recently I added OptaPlanner dependency to my build.gradle in order to use the Vehicle Routing Problem of the Library. When I tried to use the solver I received the following error java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its

Is there a way to parse two different dateTime formats into LocalDateTime or Instant in java?

99封情书 提交于 2020-06-17 13:08:27
问题 How can we validate two formats "yyyy-MM-dd" and "yyyy-MM-dd'T'HH:mm:SSX" for a given string which can be of any of the two formats and either convert it to Instant or LocalDateTime? LocalDateTime dateTime; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd[\'T\'HH:mm:SSX]"); TemporalAccessor temporalAccessor = formatter.parseBest(now, LocalDateTime::from, LocalDate::from); if (temporalAccessor instanceof LocalDateTime) { dateTime = (LocalDateTime)temporalAccessor; } else {

Is there a way to parse two different dateTime formats into LocalDateTime or Instant in java?

牧云@^-^@ 提交于 2020-06-17 13:07:49
问题 How can we validate two formats "yyyy-MM-dd" and "yyyy-MM-dd'T'HH:mm:SSX" for a given string which can be of any of the two formats and either convert it to Instant or LocalDateTime? LocalDateTime dateTime; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd[\'T\'HH:mm:SSX]"); TemporalAccessor temporalAccessor = formatter.parseBest(now, LocalDateTime::from, LocalDate::from); if (temporalAccessor instanceof LocalDateTime) { dateTime = (LocalDateTime)temporalAccessor; } else {

Split stream into substreams with N elements

此生再无相见时 提交于 2020-06-16 21:19:52
问题 Can we somehow split stream into substreams with no more than N elements in Java? For example Stream<Integer> s = Stream.of(1,2,3,4,5); Stream<Stream<Integer>> separated = split(s, 2); // after that separated should contain stream(1,2), stream(3,4), stream(5) splitting by two streams solution is correct only for 2 streams, the same for N streams will be very ugly and write-only. 回答1: You can't split a Stream into 2 or more Streas s easily and directly. The only way the procedural one