java-8

Error when collecting IntStream to map

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-26 06:36:02
问题 The following code String[] values = ... .... Map<String, Object> map = new HashMap<>(); for (int i = 0; i < values.length; i++) { map.put("X" + i, values[i]); } is converted by IntelliJ to: Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i], (a, b) -> b)); which can be shortened to Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i])); The 2 stream versions don't

Error when collecting IntStream to map

柔情痞子 提交于 2020-12-26 06:32:06
问题 The following code String[] values = ... .... Map<String, Object> map = new HashMap<>(); for (int i = 0; i < values.length; i++) { map.put("X" + i, values[i]); } is converted by IntelliJ to: Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i], (a, b) -> b)); which can be shortened to Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i])); The 2 stream versions don't

Error when collecting IntStream to map

不问归期 提交于 2020-12-26 06:31:29
问题 The following code String[] values = ... .... Map<String, Object> map = new HashMap<>(); for (int i = 0; i < values.length; i++) { map.put("X" + i, values[i]); } is converted by IntelliJ to: Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i], (a, b) -> b)); which can be shortened to Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i])); The 2 stream versions don't

Java 8 Date time for calculating age in decimals [duplicate]

梦想的初衷 提交于 2020-12-26 05:07:04
问题 This question already has answers here : Java Time period in decimal number of years (3 answers) Closed 3 years ago . I am new in using Java 8 date time API and was wondering how can i able to calculate the age in decimals which returns the double value like 30.5 which means 30 years and 6 months? For example the below sample code gets me the output as 30.0 but not 30.5 which probably am trying for. LocalDate startDate = LocalDate.of(1984, Month.AUGUST, 10); LocalDate endDate = LocalDate.of

How to apply sorting and limiting after groupby using Java streams

纵然是瞬间 提交于 2020-12-26 04:24:11
问题 I have the following list of Employee data which I need to group based on the employee department and then I want to find the 2 highest-paid employees in each department. public class Employee { private int id; private String name; private String dept; private int salary; //setters and getters } List<Employee> listOfEmp = new ArrayList<>(); listOfEmp.add(new Employee (1, "A", "IT",100)); listOfEmp.add(new Employee (2, "B", "IT",200)); listOfEmp.add(new Employee (3, "C", "SUPPORT",100));

How to apply sorting and limiting after groupby using Java streams

老子叫甜甜 提交于 2020-12-26 04:23:50
问题 I have the following list of Employee data which I need to group based on the employee department and then I want to find the 2 highest-paid employees in each department. public class Employee { private int id; private String name; private String dept; private int salary; //setters and getters } List<Employee> listOfEmp = new ArrayList<>(); listOfEmp.add(new Employee (1, "A", "IT",100)); listOfEmp.add(new Employee (2, "B", "IT",200)); listOfEmp.add(new Employee (3, "C", "SUPPORT",100));

Java stream order of processing

可紊 提交于 2020-12-25 10:44:38
问题 I have the following: LinkedList<Integer> ints = new LinkedList(); //fill it with some ints Stream<Integer> stream = ints.stream(); //process the stream in some way My question is if it's guaranteed that the order of processing of the stream is as of the underlying LinkedList ? I read the documentation but there was no any info about the ordering. In my case it's critically to preserve the order. 回答1: From the documentation: Streams may or may not have a defined encounter order. Whether or

Convert int stream to map

不羁岁月 提交于 2020-12-25 07:01:31
问题 I have an int stream and want for each element of that stream to do some calculations and return them as Map where keys are int values and values are result of that computations. I wrote following piece of code: IntStream.range(0,10).collect(Collectors.toMap(Function.identity(), i -> computeSmth(i))); where computeSmth(Integer a) . I got next compiler error method collect in interface java.util.stream.IntStream cannot be applied to given types; required: java.util.function.Supplier<R>,java

Use Java 8 Optional in existing Java 7 code

会有一股神秘感。 提交于 2020-12-25 04:39:45
问题 I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List<VehicleMake> loadMatching(Region region, String nameStartsWith, VehicleLoader loader) { if ((nameStartsWith == null) || (region == null) || (loader == null)) { throw new IllegalArgumentException("The VehicleLoader and both region and nameStartsWith are required when loading VehicleMake matches"); } List<VehicleMake

Use Java 8 Optional in existing Java 7 code

十年热恋 提交于 2020-12-25 04:39:05
问题 I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List<VehicleMake> loadMatching(Region region, String nameStartsWith, VehicleLoader loader) { if ((nameStartsWith == null) || (region == null) || (loader == null)) { throw new IllegalArgumentException("The VehicleLoader and both region and nameStartsWith are required when loading VehicleMake matches"); } List<VehicleMake