java-8

Java 8 list to nested map

只愿长相守 提交于 2020-08-02 07:04:14
问题 I hava a list of Class A like class A { private Integer keyA; private Integer keyB; private String text; } I want to transfer aList to nested Map mapped by keyA and keyB So I create below code. Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap

Lambda in Stream.map/filter not called

陌路散爱 提交于 2020-07-30 05:09:54
问题 I'm trying to find separate the duplicates and non-duplicates in a List by adding them to a Set and List while using Stream.filter and Stream.map List<String> strings = Arrays.asList("foo", "bar", "foo", "baz", "foo", "bar"); Set<String> distinct = new HashSet<>(); List<String> extras = new ArrayList<>(); strings .stream() .filter(x -> !distinct.add(x)) .map(extra -> extras.add(extra)); At the end of this, I expect distinct to be [foo, bar, baz] and extras to be [foo, foo, bar] , since there

Is possible to know the size of a stream without using a terminal operation

生来就可爱ヽ(ⅴ<●) 提交于 2020-07-29 17:06:09
问题 I have 3 interfaces public interface IGhOrg { int getId(); String getLogin(); String getName(); String getLocation(); Stream<IGhRepo> getRepos(); } public interface IGhRepo { int getId(); int getSize(); int getWatchersCount(); String getLanguage(); Stream<IGhUser> getContributors(); } public interface IGhUser { int getId(); String getLogin(); String getName(); String getCompany(); Stream<IGhOrg> getOrgs(); } and I need to implement Optional<IGhRepo> highestContributors(Stream<IGhOrg>

UnsupportedTemporalTypeException: Unsupported field: InstantSeconds

一个人想着一个人 提交于 2020-07-29 10:50:09
问题 I have this code which is producing a timestamp and then parsing. DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyyMMdd kk:HH:ss.SSSZ") .withLocale(Locale.getDefault()) .withZone(ZoneId.systemDefault()); Instant now = Instant.now(); String formatted = formatter.format(now); Instant parsed = formatter.parse(formatted, Instant::from); When it runs, the last line produces an exception: java.time.format.DateTimeParseException: Text '20180123 12:12:45.648-0500' could not be parsed:

UnsupportedTemporalTypeException: Unsupported field: InstantSeconds

社会主义新天地 提交于 2020-07-29 10:50:09
问题 I have this code which is producing a timestamp and then parsing. DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyyMMdd kk:HH:ss.SSSZ") .withLocale(Locale.getDefault()) .withZone(ZoneId.systemDefault()); Instant now = Instant.now(); String formatted = formatter.format(now); Instant parsed = formatter.parse(formatted, Instant::from); When it runs, the last line produces an exception: java.time.format.DateTimeParseException: Text '20180123 12:12:45.648-0500' could not be parsed:

UnsupportedTemporalTypeException: Unsupported field: InstantSeconds

你。 提交于 2020-07-29 10:49:30
问题 I have this code which is producing a timestamp and then parsing. DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyyMMdd kk:HH:ss.SSSZ") .withLocale(Locale.getDefault()) .withZone(ZoneId.systemDefault()); Instant now = Instant.now(); String formatted = formatter.format(now); Instant parsed = formatter.parse(formatted, Instant::from); When it runs, the last line produces an exception: java.time.format.DateTimeParseException: Text '20180123 12:12:45.648-0500' could not be parsed:

How to get the first value for each distinct keys using Java 8 streams?

喜欢而已 提交于 2020-07-29 06:37:57
问题 For example: I have a list of user objects with e-mails. I would like to collect the list of users with distinct e-mails (because two users with the same e-mail would be probably the same person, so I do not care about the differences). In other words, for each distinct key (e-mail), I want to grab the first value (user) found with that key and ignore the rest. This is a code I have came up with: public List<User> getUsersToInvite(List<User> users) { return users .stream() // group users by

Create array of incremental int using Stream instead of for loop

别来无恙 提交于 2020-07-29 06:14:11
问题 I want to create a function that builds an array of incremental numbers. For example, I want to obtain something like: int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, ..., 1000000}; The function will receive two parameters: start number (inclusive) and the final length of the array: public int[] buildIncrementalArray(int start, int length) { ... } I know how to do it using a for loop: public int[] buildIncrementalArray(int start, int length) { int[] result = new int[length]; for(int i = 0 ; i

Find standard time name for a region using Java

匆匆过客 提交于 2020-07-23 06:23:45
问题 Is there anyway to find the standard time that comes under time zone regions, for example America/New_York and America/NewJersey -> EST I am developing a REST service where the request will have region-based timezone(America/NewJersey) as a parameter and I need to find the standard time it comes under and pass it to legacy API, which only accepts 3 digit timezone(EST/MST/CST/AET). I am using Java 8 and I checked Time API, but it doesn't have any such feature/functionality. As a workaround I

Find standard time name for a region using Java

≯℡__Kan透↙ 提交于 2020-07-23 06:22:29
问题 Is there anyway to find the standard time that comes under time zone regions, for example America/New_York and America/NewJersey -> EST I am developing a REST service where the request will have region-based timezone(America/NewJersey) as a parameter and I need to find the standard time it comes under and pass it to legacy API, which only accepts 3 digit timezone(EST/MST/CST/AET). I am using Java 8 and I checked Time API, but it doesn't have any such feature/functionality. As a workaround I