java-8

Is there any advantage of calling map after mapToInt, where ever required

纵然是瞬间 提交于 2020-01-21 04:17:30
问题 I am trying to calculate the sum of squares of values in the list. Below are three variations which all calculates the required value. I want to know which one is the most efficient. I am expecting the third one to be more efficient as auto boxing is only done once. // sum of squares int sum = list.stream().map(x -> x * x).reduce((x, y) -> x + y).get(); System.out.println("sum of squares: " + sum); sum = list.stream().mapToInt(x -> x * x).sum(); System.out.println("sum of squares: " + sum);

Why is the “Variable used in Lambda expression must be final or effectively final” warning ignored for instance variables [duplicate]

大城市里の小女人 提交于 2020-01-21 03:45:08
问题 This question already has answers here : Variable used in lambda expression should be final or effectively final (6 answers) Lambdas: local variables need final, instance variables don't (10 answers) Closed 7 months ago . I'm studying java 8 streams and the only problem I have is understanding lambdas is why the effectively final warning in lambdas is ignored for instance(and static) variables. I can't seem to find any reference to it online, as most pages will just talk about the definition

Optional.of(null)will throw NPE, I need to verify null before call the method?

你离开我真会死。 提交于 2020-01-20 22:05:42
问题 Java 8 introduces Optional to deal with NPE. In practical application,I can‘t understand a problem. I have the method A public void doSomethingA(String para) { Optional<String> name = Optional.of(para); if (name.isPresent()) { //do } } But if para = null, it will throw NPE. method B public void doSomethingB(String para) { if (para != null) { //do } } if I check para is not null, What's the difference between A and B. Where is the meaning of Optional. 回答1: Use Optional.ofNullable if you are

Sort map by value using lambdas and streams

时光怂恿深爱的人放手 提交于 2020-01-19 07:13:47
问题 I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8. HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 10); map.put("b", 30); map.put("c", 50); map.put("d", 40); map.put("e", 100); map.put("f", 60); map.put("g", 110); map.put("h", 50); map.put("i", 90); map.put("k", 70); map.put("L", 80); I know before Java 8, we can sort as this link: https:/

Sort map by value using lambdas and streams

南楼画角 提交于 2020-01-19 07:11:39
问题 I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8. HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 10); map.put("b", 30); map.put("c", 50); map.put("d", 40); map.put("e", 100); map.put("f", 60); map.put("g", 110); map.put("h", 50); map.put("i", 90); map.put("k", 70); map.put("L", 80); I know before Java 8, we can sort as this link: https:/

Sort map by value using lambdas and streams

◇◆丶佛笑我妖孽 提交于 2020-01-19 07:11:29
问题 I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8. HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 10); map.put("b", 30); map.put("c", 50); map.put("d", 40); map.put("e", 100); map.put("f", 60); map.put("g", 110); map.put("h", 50); map.put("i", 90); map.put("k", 70); map.put("L", 80); I know before Java 8, we can sort as this link: https:/

javafx @font-face css error “loadStyleSheetUnprivileged”

时光总嘲笑我的痴心妄想 提交于 2020-01-17 13:59:25
问题 I'm trying to load a custom font in JavaFx css using this method @font-face { font-family: 'Roboto'; src: url('fonts/Roboto-Medium.ttf'); } I've done every thing proper with the correct path but I'm getting this error Nov 28, 2015 4:49:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not load @font-face font [file:/C:/Users/RootUser/Desktop/Java8%20projects/RemoteViewer/out/production/JavaFxApplication/application/fonts/Roboto-Medium.ttf] This is my project

javafx @font-face css error “loadStyleSheetUnprivileged”

梦想的初衷 提交于 2020-01-17 13:59:24
问题 I'm trying to load a custom font in JavaFx css using this method @font-face { font-family: 'Roboto'; src: url('fonts/Roboto-Medium.ttf'); } I've done every thing proper with the correct path but I'm getting this error Nov 28, 2015 4:49:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not load @font-face font [file:/C:/Users/RootUser/Desktop/Java8%20projects/RemoteViewer/out/production/JavaFxApplication/application/fonts/Roboto-Medium.ttf] This is my project

javafx @font-face css error “loadStyleSheetUnprivileged”

匆匆过客 提交于 2020-01-17 13:59:07
问题 I'm trying to load a custom font in JavaFx css using this method @font-face { font-family: 'Roboto'; src: url('fonts/Roboto-Medium.ttf'); } I've done every thing proper with the correct path but I'm getting this error Nov 28, 2015 4:49:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not load @font-face font [file:/C:/Users/RootUser/Desktop/Java8%20projects/RemoteViewer/out/production/JavaFxApplication/application/fonts/Roboto-Medium.ttf] This is my project

Do I need different version of mysql-connector for jdk 8

。_饼干妹妹 提交于 2020-01-17 08:15:08
问题 I'm using mysql-connector-java-5.1.6-bin with jdk 6 currently but I'm moving my project to jdk 8 do I need to download different version of this jar file? 回答1: If you want use JDK 8, you need the latest version of mysql connector, Connector/J 8.0 From the What's New in Connector/J 8.0? page: It is a MySQL driver for the Java 8 platform. For Java 7 or earlier, use Connector/J 5.1 instead. 回答2: You don't require Connector/J 8.0 to run on JRE 8. Connector/J 5.1 works too but 5.1 is getting much