java-stream

Java Streams | groupingBy same elements

只谈情不闲聊 提交于 2019-12-01 04:57:28
I have a stream of words and I would like to sort them according to the occurrence of same elements (=words). e.g.: {hello, world, hello} to Map<String, List<String>> hello, {hello, hello} world, {world} What i have so far: Map<Object, List<String>> list = streamofWords.collect(Collectors.groupingBy(???)); Problem 1: The stream seems to lose the information that he is processing Strings, therefore the compiler forces me to change the type to Object, List Problem 2: I don't know what to put inside the parentesis to group it by the same occurrence. I know that I am able to process single

Replace nested loop with Java 8 flatmap

坚强是说给别人听的谎言 提交于 2019-12-01 04:48:09
问题 I'm trying to use flatmap to make a nested loop with the Stream API, but I can't seem to figure it out. As an example, I want to recreate the following loop: List<String> xs = Arrays.asList(new String[]{ "one","two", "three"}); List<String> ys = Arrays.asList(new String[]{"four", "five"}); System.out.println("*** Nested Loop ***"); for (String x : xs) for (String y : ys) System.out.println(x + " + " + y); I can do it like this, but this seems ugly: System.out.println("*** Nested Stream ***");

stream and parallelStream

一世执手 提交于 2019-12-01 04:44:48
I have a test code like this : List<Integer> list = new ArrayList<>(1000000); for(int i=0;i<1000000;i++){ list.add(i); } List<String> values = new ArrayList<>(1000000); list.stream().forEach( i->values.add(new Date().toString()) ); System.out.println(values.size()); Running this, I got a correct output: 1000000. However, if I change the stream() to parallelStream() , as this: list.parallelStream().forEach( i->values.add(new Date().toString()) ); I got a random output, e.g.: 920821. What's wrong? Tunaki An ArrayList is not synchronized. Trying to concurrently add elements to it is not defined.

How to increment a value in Java Stream?

浪尽此生 提交于 2019-12-01 04:44:24
问题 I want to increment value of index with the each iteration by 1 . Easily to be achieved in the for-loop . The variable image is an array of ImageView . Here is my for-loop . for (Map.Entry<String, Item> entry : map.entrySet()) { image[index].setImage(entry.getValue().getImage()); index++; } In order to practise Stream, I have tried to rewrite it to the Stream : map.entrySet().stream() .forEach(e -> item[index++].setImage(e.getValue().getImage())); Causing me the error: error: local variables

Chaining Consumers Java 8

时光总嘲笑我的痴心妄想 提交于 2019-12-01 04:20:20
问题 Hello I am having the following problem. Lets say that We have object "Account" This object Account is immutable, so overtime we execute an action upon it we are actually transforming it to another state. For example Account can become ClosedAccount or NewAccount and so on. Now in some occasions my transitions are reflective Account is transformed to Account but something very small is changed like for example "modifiedDate". Because my Account is its essence immutable even the reflective

Word count with java 8

谁说我不能喝 提交于 2019-12-01 04:12:31
I am trying to implement a word count program in java 8 but I am unable to make it work. The method must take a string as parameter and returns a Map<String,Integer> . When I am doing it in old java way, everthing works fine. But when I am trying to do it in java 8, it returns a map where the keys are the empty with the correct occurrences. Here is my code in a java 8 style : public Map<String, Integer> countJava8(String input){ return Pattern.compile("(\\w+)").splitAsStream(input).collect(Collectors.groupingBy(e -> e.toLowerCase(), Collectors.reducing(0, e -> 1, Integer::sum))); } Here is the

Java 8: IntStream to Integer[]

最后都变了- 提交于 2019-12-01 04:05:40
I am writing simple program which will eventually plot the run times of various sorting algorithms written in Java. The general interface of a sorting algorithm is via a method: public void sort(Comparable[] xs) I am attempting to use Java 8's stream mechanism to generate random test cases along the following lines: public static IntStream testCase(int min, int max, int n) { Random generator = new Random(); return generator.ints(min, max).limit(n); } My question is, how can I convert an object of type IntStream to an Integer[] ? You should box the IntStream to a Stream<Integer> , then call

Java 8 primitive stream to collection mapping methods

半世苍凉 提交于 2019-12-01 03:58:42
问题 Is there any significant difference (in performance or best practices) between those two stream creation methods? int[] arr2 = {1,2,3,4,5,6}; Arrays.stream(arr2) .map((in)->in*2) .mapToObj((in) -> new Integer(in)) .collect(Collectors.toCollection(()-> new ArrayList<>())); Arrays.stream(arr2) .map(in->in*2) .boxed() .collect(Collectors.toCollection(()-> new ArrayList<>())); EDIT Thanks to Stack Community answers I can add some addons to question completeness for new readers: As many pointed

Understanding java 8 stream's filter method

喜你入骨 提交于 2019-12-01 03:57:16
问题 I recently learned about Stream s in Java 8 and saw this example: IntStream stream = IntStream.range(1, 20); Now, let's say that we want to find the first number that's divisable both by 3 and a 5. We'd probably filter twice and findFirst as follows: OptionalInt result = stream.filter(x -> x % 3 == 0) .filter(x -> x % 5 == 0) .findFirst(); That's all sounds pretty reasonable. The surprising part came when I tried to do this: OptionalInt result = stream.filter(x -> {System.out.println(x);

grouping objects java 8

五迷三道 提交于 2019-12-01 03:32:42
问题 I have something like the below : public class MyClass { private Long stackId private Long questionId } A collection of say 100, where the stackid could be duplicate with different questionIds. Its a one to many relationship between stackId and questionId Is there a streamy , java 8 way to convert to the below strcuture : public class MyOtherClass { private Long stackId private Collection<Long> questionIds } Which would be a collection of 25, with each instance having a nested collection of 4