java-8

converting for loop to java 8 stream

旧城冷巷雨未停 提交于 2020-01-03 13:03:11
问题 I was playing around with Java 8. I had some trouble converting this for loop into Java 8 Stream. for (int y = 0; y < 5; y ++) { for (int x = y; x < 10; x += 2) { System.out.println(x+y); } } Please help! 回答1: The canonical way of converting nested loops is to use flatMap on a stream, e.g. IntStream.range(0, 5).flatMap(i->IntStream.range(i, 10)) .forEach(System.out::println); The tricky part on your task is the increment by two as this has no direct equivalent in the stream API. There are two

Does reduce on an ordered stream reduce in order?

百般思念 提交于 2020-01-03 09:05:43
问题 I have a List of A, B, C. C reduce A reduce B != A reduce B reduce C (however, A reduce (B reduce C) is OK). In other words, my reduction operation is associative but not commutative. Does java enforce on an ordered sequential stream (such as the default one from a list) that reduction will always happen according to the encounter order? That is to say, will java reorder reductions (such that B reduce A instead of A reduce B)? (Hopefully this is clear enough). edit to add a little demo and

Java 8 BufferedReader lines() method prints different count number

被刻印的时光 ゝ 提交于 2020-01-03 08:28:12
问题 I have faced a problem to count line number using lines() method of BufferedReader . Following is the content of test.txt file. 1 Career 2 Filmography 3 Awards 4 References 5 External Here is the source code to count line number twice. BufferedReader br=new BufferedReader(new FileReader(new File("test.txt"))); long lineNo=br.lines().count(); long lineNo2=br.lines().count(); System.out.println(lineNo); // 5 System.out.println(lineNo2);// 0 Here, I have question why second line of lineNo2 print

Is there a way to combine Java8 Optional returning a value with printing a message on null?

不问归期 提交于 2020-01-03 06:42:09
问题 I'm starting with this code: String startingValue = getMyValue(); String finishingValue = ""; if (startingValue != null) { finishingValue = startingValue; } else { System.out.println("Value was null"); } I want to transform it using Java 8 options to something like this: Optional<String> startingOptional = getMyOptional(); String finishingValue = startingOptional .map(value -> value) .orElse(System.out.println("value not found")); My question is: Is there a way to combine Java8 Optional

Create an array of string with an element from a list [closed]

怎甘沉沦 提交于 2020-01-03 05:37:42
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 9 days ago . There is a list with four string in it --> employee details ( IDs, name, manager IDs, and so on -- all of string type). First need to filter the list with distinct IDs. From which need to take the ID from each object in the list and store it as an array of IDs. This would just be all the distinct ID

Sort list of maps using multiple keys in Java8

青春壹個敷衍的年華 提交于 2020-01-03 04:49:06
问题 I have list of maps and I want to sort the maps inside the list using the keys. As of now I am achieving this using the below Collections.sort method.. Collections.sort(listOfMaps, new Comparator<Map<String, String>>() { @Override public int compare(Map<String, String> o1, Map<String, String> o2) { //return o1.get("cm_order_x").compareTo(o2.get("cm_order_x")); String x1 = o1.get(Key1); String x2 = o2.get(Key1); String x3 = o1.get(Key2); String x4 = o2.get(Key2); int sComp = x1.compareTo(x2);

local variables must be final or effectively final

我是研究僧i 提交于 2020-01-03 04:30:07
问题 I have an async operation inside of Java 8 which return an onError callback or an onSuccess callback. I need to return inside of my method if the operation was a success or not. So I am returning a boolean to state this information. The problem I am facing is I get the following compilation error: error: local variables referenced from an inner class must be final or effectively final Googling the error I can see you this type of operation is not allowed, but then how can I return if the

Get the method name from a java.util.function.Function

六眼飞鱼酱① 提交于 2020-01-03 04:14:06
问题 Is it possible to get the method name of a java.util.function.Function. I would like to log each time the name of the method being used. The following example prints the Lambda object but I have not found an easy way to get the method name: public class Example { public static void main(String[] args) { Example ex = new Example(); ex.callService(Integer::getInteger, "123"); } private Integer callService(Function<String, Integer> sampleMethod, String input) { Integer output = sampleMethod

Parallel message unmarshalling from a token delimited input stream with Java8 stream API

我的梦境 提交于 2020-01-03 03:14:45
问题 Is it possible with Java8 stream API to create interface like Message pullNext() plumbed on top of a delimited input stream with the logical steps as below? Delimited tokens are read from the character input stream Tokens fed to parallel unmarshaller (one token -> one Message) Messages are reordered back to retain the original incoming order Messages are pullable with aforementioned pullNext() Somewhat a disruptor with unmarshal stage served by concurrent pool. Similar to this, maybe

What is the maximum number of processes that can be used in parallel Stream

人盡茶涼 提交于 2020-01-03 02:43:05
问题 I know that we can use -Djava.util.concurrent.ForkJoinPool.common.parallelism to set the parallelism, but is there any upper limit to it? 回答1: By looking into the source code of ForkJoinPool , I find the definition of MAX_CAP = 0x7fff; // max #workers - 1 which is enforced when using the constructor ForkJoinPool(int) by throwing an exception when you attempt to specify more. However, when using the system property, java.util.concurrent.ForkJoinPool.common.parallelism to configure the