java-8

if-else condition using java 8 stream [duplicate]

一世执手 提交于 2019-12-24 02:03:14
问题 This question already has answers here : How to use if-else logic in Java 8 stream forEach (4 answers) Closed last year . Scenario: There is a situation where I need to set some values to List of objects based on some field condition using the Java 8 streams API. Below is the sample of object User . public class User{ private int id; private String name; private String text; private boolean isActive; } Here is the code I have worked out List<User> users = userDao.getAllByCompanyId(companyId);

Java 8 - filter collection using many filters [duplicate]

一笑奈何 提交于 2019-12-24 01:44:30
问题 This question already has answers here : How to apply multiple predicates to a java.util.Stream? (4 answers) Closed 3 years ago . I would like to filter out my collection using multiple filters. Let's assume I have a list of Strings and a function filter() to filter out empty Strings. List<String> myList = ....... Typically, I would use streams like this: myList.stream() .filter(elem -> filterOut(elem)) .collect(Collectors.toList()); How to apply multiple filters from a collection ( List or

RxJava. Read file to observable

那年仲夏 提交于 2019-12-24 01:31:48
问题 I am totally new to RxJava and reactive programming. I have an assignment where i must read file and store it to Observable. I have tried to make a Callable with BufferedReader inside and use Observable.fromCallable(), but it didn't work much. Could you please show me how can i do that? I am using RxJava 2.0. 回答1: A basic solution where I use a nested class FileObservableSource to produce the data and then defer the creation of the Observable until an Observer subscribes: import io.reactivex

JavaFX development with just JavaScript (Nashorn)

戏子无情 提交于 2019-12-24 00:59:12
问题 Is there a way to write a JavaFX app with just JavaScript files now that Nashorn is available in Java 8? Where can I find more info or videos on this? How would you compile it? Or is there a minimal set of bootstrapping files required? 回答1: Yes, you can develop JavaFX programs with just JavaScript (Nashorn). How to access JavaFX from JavaScript Use the jjs -fx yourscript.js command line switch. The -fx flag on jjs will bootstrap scripts using a javafx.application.Application. There is no

Does Stream.boxed() preserve order?

落爺英雄遲暮 提交于 2019-12-24 00:27:39
问题 Assume I have an infinite Stream. IntStream istream = IntStream.iterate(0, i -> i + 1).limit(100); Stream<Integer> boxedStream = istream.boxed(); Does the boxed() method preserve order? Probably yes, but I cannot find it in the documentation. 回答1: Actually every intermediate operation preserves an order by default. The only exceptions are: unordered() which removes the ordering constraint. sorted() which changes the order. When it's not explicitly specified, you can assume that operation

Hibernate unable to create schema automatically on Heroku postgres

自古美人都是妖i 提交于 2019-12-23 23:57:58
问题 I have a play 2.4 hibernate postgresql application which works well locally, creates a table automatically initially. But when deployed on heroku it does not create any table though the app loaded without any errors(connected my heroku db from pgAdmin). My Configuration build.sbt name := "lms" version := "1.0" lazy val `lms` = (project in file(".")).enablePlugins(PlayJava) scalaVersion := "2.11.7" libraryDependencies ++= Seq(javaJdbc, cache, javaWs, javaJpa.exclude("org.hibernate.javax

How to count the number of trailing zeroes in an integer using Java 8 Stream/Lambda?

让人想犯罪 __ 提交于 2019-12-23 22:57:56
问题 How to count the number of trailing zeroes in an integer using Java 8 Stream/Lambda? Basically the logic should be: keep the integer dividing by 10 as long as the remainder is 0 (the quotient will be supplied to the next division) and count the occurrence(s). e.g. 12300 % 10 == 0 true 1230 % 10 == 0 true 123 % 10 == 0 false Answer: 2 Note: I prefer not to involve String here :-) 回答1: If this is a purely hypothetical question, here is a purely hypothetical answer of how you can do it: static

Java 8 and Eclipse on OS X Mavericks

喜欢而已 提交于 2019-12-23 22:40:35
问题 I want to play a bit with the recently released Java 8 on OS X Mavericks, but have problems configuring the compiler compliance level to anything beyond 1.7 . I tried this with a recent Luna build (4.4.0M6) as well as with Kepler (4.3 SR2), patched with this. What am I doing wrong? 回答1: I think I got it right now. I just re-installed the patch from here and now 1.8 is shown as compiler level. I think while the "About" dialog showed me that the patch was installed, it probably wasn't done

How to use method reference in Java 8 for Map merge?

妖精的绣舞 提交于 2019-12-23 22:19:29
问题 I have following 2 forms of calling a collect operation, both return same result, but I still cannot depend fully on method references and need a lambda. <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) For this consider the following stream consisting on 100 random numbers List<Double> dataList = new Random().doubles().limit(100).boxed() .collect(Collectors.toList()); 1) Following example uses pure lambdas Map<Boolean, Integer> partition =

Is it possible (how) to get the name of a method reference at Runtime Java? [duplicate]

孤人 提交于 2019-12-23 19:12:53
问题 This question already has answers here : Printing debug info on errors with java 8 lambda expressions (1 answer) Reflection type inference on Java 8 Lambdas (5 answers) How to get the MethodInfo of a Java 8 method reference? (10 answers) Closed last year . I've been using a lot of method references and lambdas recently, and wanted to know at runtime if i could print to screen the source of the lambda ie its name, simply for debugging reasons. I figured it might be possible using reflection,