java-8

Java 8 modify stream elements

筅森魡賤 提交于 2020-01-30 19:59:52
问题 I wanted to write pure function with Java 8 that would take a collection as an argument, apply some change to every object of that collection and return a new collection after the update. I want to follow FP principles so I dont want to update/modify the collection that was passed as an argument. Is there any way of doing that with Stream API without creating a copy of the original collection first (and then using forEach or 'normal' for loop)? Sample object below and lets assume that I want

Java 8 method reference to class instance method NPE

依然范特西╮ 提交于 2020-01-30 13:27:12
问题 import java.util.function.Function; public class Playground { public static void main (String[] args) { Object o = null; System.out.println(o); Function<Object, String> toStringFunc = Object::toString; String s = toStringFunc.apply(o); System.out.println(s); } } This code will result in a NullPointerException being thrown, reported at the line containing toStringFunc.apply(o) . This is a trivial example, so it's easy to see that o == null , but how in general can we understand why this line

Using Java 8 stream methods to get a max value

爱⌒轻易说出口 提交于 2020-01-30 07:45:49
问题 I would like to get the max value out of a list using java 8 stream methods. The structure is the following: I read a csv file and store the data of every line in a separate object of type Round . all these Round objects are stored in an ArrayList called arrRound all Round objects have a field: List<Hit> hits a Hit consists of 2 fields: int numberOfGames and int prizeAmount public class Round{ private List<Hits> hits; } public class Hits{ private int numberOfGames; private int prizeAmount; }

Java Runtime Engine support for SonarQube 7.x

不问归期 提交于 2020-01-30 06:46:27
问题 On the SonarQube requirements page you can read that JRE 8 is a requirement and newer Java runtime versions are not supported. Looking at the Oracle Java SE Support Roadmap the public update support for version 8 could be stopped on January 2019. Does anyone know how Sonarsource will update their JRE usage (web application, plugins and scanner) in the coming period? Update (07-06-2018) Jira item SONAR-10574 describes the upgrade of Elasticsearch to 5.6.8 in a 7.x version although looking at

Java Runtime Engine support for SonarQube 7.x

本小妞迷上赌 提交于 2020-01-30 06:46:25
问题 On the SonarQube requirements page you can read that JRE 8 is a requirement and newer Java runtime versions are not supported. Looking at the Oracle Java SE Support Roadmap the public update support for version 8 could be stopped on January 2019. Does anyone know how Sonarsource will update their JRE usage (web application, plugins and scanner) in the coming period? Update (07-06-2018) Jira item SONAR-10574 describes the upgrade of Elasticsearch to 5.6.8 in a 7.x version although looking at

Java 8 Date API - Get total number of weeks in a month

空扰寡人 提交于 2020-01-30 05:39:53
问题 I have a Kotlin function to get the total number of weeks in a month Code fun getTotalWeeksInMonth(instant: Instant): Int { val calendar = Calendar.getInstance() calendar.time = Date.from(instant) return calendar.getActualMaximum(Calendar.WEEK_OF_MONTH) } However this is using a mix of the old Java date/time APIs ( Date and Calendar ) and the new APIs ( Instant ) How would I achieve the same result, using just the new APIs? 回答1: You can try something like this pair of lines: YearMonth

Additional brackets in Java8 stream map() function

馋奶兔 提交于 2020-01-30 02:56:30
问题 I'm working/testing streams in Java8 and come across very frustrating issue. I've got the code which compiles well: List<String> words = Arrays.asList("Oracle", "Java", "Magazine"); List<String> wordLengths = words.stream().map((x) -> x.toUpperCase()) .collect(Collectors.toList()); And second one (nearly the same) which throw a warnings: List<String> words = Arrays.asList("Oracle", "Java", "Magazine"); List<String> wordLengths = words.stream().map((x) -> { x.toUpperCase(); }).collect

How to use thenComparing in java stream

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 02:53:12
问题 I have a map with strings as values. I want to sort it firstly by length, and if length of the strings is the same, i want to sort it alphabetic. I wrote those code : String out = outMap.values().stream() .sorted(Comparator.comparing(e -> e.length()).thenComparing()...) .collect(Collectors.joining()); The problem is, when i am writing thenComparing, I couldn't use e.length() anymore. How can i fix it? EDIT : Map<Character, String> . I want to sort the strings and make one string in output by

Show the default value chosen for XX:ParallelGCThreads [duplicate]

自古美人都是妖i 提交于 2020-01-29 12:49:30
问题 This question already has an answer here : How do i know which default settings are enabled for Sun JVM? (1 answer) Closed 2 years ago . I'm tuning the JVM of Java 8 and I'm trying to know what the value was defined for the parameter -XX:ParallelGCThreads . The documentation says: -XX:ParallelGCThreads : Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running. I would like to know what value was

Show the default value chosen for XX:ParallelGCThreads [duplicate]

僤鯓⒐⒋嵵緔 提交于 2020-01-29 12:49:06
问题 This question already has an answer here : How do i know which default settings are enabled for Sun JVM? (1 answer) Closed 2 years ago . I'm tuning the JVM of Java 8 and I'm trying to know what the value was defined for the parameter -XX:ParallelGCThreads . The documentation says: -XX:ParallelGCThreads : Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running. I would like to know what value was