java-stream

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>

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

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

你。 提交于 2020-07-28 06:21:49
问题 It's easy to convert List<V> into Map<K, List<V>> . For example: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.groupingBy(String::length)); } But I want to do it with my own List and Map suppliers . I have come up with this: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.toMap( String::length, item -> {List<String> list = new ArrayList<>(); list.add(item); return list;}

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

倖福魔咒の 提交于 2020-07-28 06:21:05
问题 It's easy to convert List<V> into Map<K, List<V>> . For example: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.groupingBy(String::length)); } But I want to do it with my own List and Map suppliers . I have come up with this: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.toMap( String::length, item -> {List<String> list = new ArrayList<>(); list.add(item); return list;}

grouping words by first character

依然范特西╮ 提交于 2020-07-18 18:03:08
问题 What I have: A text-file which is read line by line. Each String contains a line. What I want: Group ALL words by first character using Java Streams. What I have so far: public static Map<Character, List<String>> groupByFirstChar(String fileName) throws IOException { return Files.lines(Paths.get(PATH)). flatMap(s -> Stream.of(s.split("[^a-zA-Z]"))). map(s -> s.toLowerCase()). sorted((s1, s2) -> s1.compareTo(s2)). collect(Collectors.groupingBy(s -> s.charAt(0))); } Problem: I get an Exception

Java 8 int array to map

拟墨画扇 提交于 2020-07-18 06:07:09
问题 I want to convert int array to Map<Integer,Integer> using Java 8 stream api int[] nums={2, 7, 11, 15, 2, 11, 2}; Map<Integer,Integer> map=Arrays .stream(nums) .collect(Collectors.toMap(e->e,1)); I want to get a map like below, key will be integer value, value will be total count of each key map={2->3, 7->1, 11->2, 15->1} compiler complains " no instance(s) of type variable(s) T, U exist so that Integer confirms to Function " appreciate any pointers to resolve this 回答1: You need to box the

Get Index while iterating list with stream [duplicate]

柔情痞子 提交于 2020-07-18 04:28:19
问题 This question already has answers here : Is there a concise way to iterate over a stream with indices in Java 8? (22 answers) Closed 2 years ago . List<Rate> rateList = guestList.stream() .map(guest -> buildRate(ageRate, guestRate, guest)) .collect(Collectors.toList()); class Rate { protected int index; protected AgeRate ageRate; protected GuestRate guestRate; protected int age; } In the above code, is it possible to pass index of guestList inside buildRate method. I need to pass index also

summaryStatistics Method of IntSummaryStatistics

╄→гoц情女王★ 提交于 2020-07-16 06:41:43
问题 Why summaryStatistics() method on an empty IntStream returns max and min value of integer as the maximum and minimum int value present in the stream? IntStream intStream = IntStream.of(); IntSummaryStatistics stat = intStream.summaryStatistics(); System.out.println(stat); Output : IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648} What is the point of returning these values? Should not it considered the wrong result? 回答1: See IntSummaryStatistics.getMax()

Parallelism with Streams created from Iterators

时光毁灭记忆、已成空白 提交于 2020-07-05 09:57:35
问题 Experimenting with streams I ran into the following behavior which I don't quite understand. I created a parallel stream from an iterator and I noticed that it did not seem to be exhibiting parallelism. In the below example I've printed a counter to the console for two parallel streams, one created from an iterator and the other from a list. The stream created from the list exhibited the behavior I expected which was to print the counter in non-sequential order but the stream created from the