java-8

How to split a string into a map, grouping values by duplicate keys using streams?

北城余情 提交于 2021-02-19 07:24:27
问题 I want to convert the following String flString="view1:filedname11,view1:filedname12,view2:fieldname21"; to a Map<String,Set<String>> to get the key/value as below: view1=[filedname11,filedname12] view2=[fieldname21] I want to use Java 8 streams. I tried Arrays.stream(tokens) .map(a -> a.split(":")) .collect(Collectors.groupingBy( a -> a[0], Collectors.toList())); However the keys are also getting added to the value list. 回答1: You should use a Collectors::mapping to map the array to an

Replace for-each loop with lambda expression

纵然是瞬间 提交于 2021-02-19 06:25:09
问题 I'm just refactoring some of my old projects to use features of Java 8. int counter = 1; for (Checker checker : checkers) { if (counter < checkers.size()) { checker.setNextChecker(checkers.get(counter++)); } } Here's kinda Chain of Resp pattern. And I need to set next checker for every checker in the list, excluding the last one. Still can't find the way to use Stream API here :) 回答1: Using IntStream.range : IntStream.range(1, checkers.size()) .forEach(i -> checkers.get(i-1).setNextChecker

How to fix java.lang.NoSuchMethodError: sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;)

淺唱寂寞╮ 提交于 2021-02-19 03:52:30
问题 Trying to send an email I got NoSuchMethodError exception when executing transport.connect(...). I chose the same version JDK(1.8.0_251) for my project, module, Glassfish (payara), maven like on this post shows but I keep receiving the same exception. I tried to add the port to the connect method but also the same error. I checked the Gmail account which is sending emails has activated the option to be used by unsecured apps. I also tried to change the method that instance the session to

How to fix java.lang.NoSuchMethodError: sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;)

徘徊边缘 提交于 2021-02-19 03:52:13
问题 Trying to send an email I got NoSuchMethodError exception when executing transport.connect(...). I chose the same version JDK(1.8.0_251) for my project, module, Glassfish (payara), maven like on this post shows but I keep receiving the same exception. I tried to add the port to the connect method but also the same error. I checked the Gmail account which is sending emails has activated the option to be used by unsecured apps. I also tried to change the method that instance the session to

Localized name of ZoneId's ID

ε祈祈猫儿з 提交于 2021-02-19 03:45:08
问题 I have a list of time zones that I want the user to choose from. So, I thought I can just call java.time.ZoneId.getAvailableZoneIds() and use the method getDisplayName on them. This results in a lot of duplicate entries like Central European Time Even if I add the time zone offset they are not unqiue. However, the ID of a ZoneId distinguishes the entries but how can I localize them ? The IDs are always in English like Europe/Rome 回答1: It's possible to get a localized version of the display

ForkJoinPool performance Java 8 vs 11

故事扮演 提交于 2021-02-19 03:25:28
问题 Consider the following piece of code: package com.sarvagya; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.stream.Collectors; public class Streamer { private static final int LOOP_COUNT = 2000; public static void main(String[] args){ try{ for(int i = 0; i < LOOP_COUNT; ++i){ poolRunner(); System.out.println("done loop " + i); try{ Thread.sleep(50L); } catch (Exception e){ System.out

java.time.zone.ZoneRulesProvider for Microsoft time-zones

∥☆過路亽.° 提交于 2021-02-19 03:18:55
问题 I am coding against an external API that returns what appear to be windows time-zone descriptions as strings e.g. "Romance Standard Time", and I need to parse these into java ZoneId s or offsets. A list of these values is available here. The java.time.ZoneId documentation states: Time-zone rules are defined by governments and change frequently. There are a number of organizations, known here as groups, that monitor time-zone changes and collate them. The default group is the IANA Time Zone

Use of constructor reference where constructor has a non-empty parameter list

泪湿孤枕 提交于 2021-02-19 02:14:55
问题 Given.. List<Foo> copy(List<Foo> foos) { return foos .stream() .map(foo -> new Foo(foo)) .collect(Collectors.toList()); } IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference". I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here. 回答1: I'm aware of the Foo::new syntax for the no-arg constructor That's not what Foo::new does. This expression will expand to what is

Use of constructor reference where constructor has a non-empty parameter list

谁说胖子不能爱 提交于 2021-02-19 02:09:23
问题 Given.. List<Foo> copy(List<Foo> foos) { return foos .stream() .map(foo -> new Foo(foo)) .collect(Collectors.toList()); } IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference". I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here. 回答1: I'm aware of the Foo::new syntax for the no-arg constructor That's not what Foo::new does. This expression will expand to what is

Java 8 streams adding values from two or more lists

百般思念 提交于 2021-02-19 01:33:19
问题 I am trying to get into Java 8 and get my head around streams and lambdas to solve various problems and got stuck on this specific one which I normally use a forEach and store the values in a Map to solve. How would you write the code to get the expected list using the new features in Java 8 ? List<Integer> voterA = Arrays.asList(1,2,3,4,5); List<Integer> voterB = Arrays.asList(1,2,3,4,5); List<List<Integer>> votes = Arrays.asList(voterA, voterB); // expected list = (2,4,6,8,10) List<Integer>