java-stream

Java 8 - fill ArrayList

扶醉桌前 提交于 2019-12-29 07:46:25
问题 Is there a better way to fill an ArrayList like this (I have done it like this in Java 7): List<ScheduleIntervalContainer> scheduleIntervalContainers = new ArrayList<>(); scheduleIntervalContainers.add(scheduleIntervalContainer); 回答1: To fill a List , it is possible to generate an infinite Stream using Stream.generate(s) and then limit the number of results with limit(maxSize). For example, to fill a List of 10 new ScheduleIntervalContainer objects: List<ScheduleIntervalContainer>

How to convert a collection/ array into JSONArray using stream in java 8

回眸只為那壹抹淺笑 提交于 2019-12-29 07:05:09
问题 Im having a double array , I need to convert the array into a JSONArray using java streams. I tried using forEach (shared mutability) which leads to loss of data. public static JSONArray arrayToJson(double[] array) throws JSONException{ JSONArray jsonArray = new JSONArray(); Arrays.stream(array) .forEach(jsonArray::put); return jsonArray; } Is there any way I could to create JSONArray using streams? 回答1: Your code works, but you can write something like this: return Arrays.stream(array)

Is there a way to force parallelStream() to go parallel?

北战南征 提交于 2019-12-29 06:59:47
问题 If the input size is too small the library automatically serializes the execution of the maps in the stream, but this automation doesn't and can't take in account how heavy is the map operation. Is there a way to force parallelStream() to actually parallelize CPU heavy maps? 回答1: There seems to be a fundamental misunderstanding. The linked Q&A discusses that the stream apparently doesn’t work in parallel, due to the OP not seeing the expected speedup. The conclusion is that there is no

Is there a way to force parallelStream() to go parallel?

℡╲_俬逩灬. 提交于 2019-12-29 06:59:14
问题 If the input size is too small the library automatically serializes the execution of the maps in the stream, but this automation doesn't and can't take in account how heavy is the map operation. Is there a way to force parallelStream() to actually parallelize CPU heavy maps? 回答1: There seems to be a fundamental misunderstanding. The linked Q&A discusses that the stream apparently doesn’t work in parallel, due to the OP not seeing the expected speedup. The conclusion is that there is no

How to sort a LinkedHashMap by value in decreasing order in java stream?

偶尔善良 提交于 2019-12-29 06:40:08
问题 To sort it int ascending order I can use: myMap.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); How can I do it in decreasing order? 回答1: To sort in reverse order, pass Comparator.reverseOrder() as parameter to comparingByValue . To get a LinkedHashMap , you must specifically request one with the 4-argument toMap() . If you don't specify what kind of a map you want, you will get whatever the default is, which currently

How to convert List to Map with indexes using stream - Java 8?

♀尐吖头ヾ 提交于 2019-12-29 06:38:29
问题 I've created method whih numerating each character of alphabet. I'm learning streams(functional programming) and try to use them as often as possible, but I don't know how to do it in this case: private Map<Character, Integer> numerateAlphabet(List<Character> alphabet) { Map<Character, Integer> m = new HashMap<>(); for (int i = 0; i < alphabet.size(); i++) m.put(alphabet.get(i), i); return m; } So, how to rewrite it using streams of Java 8? 回答1: Avoid stateful index counters like the

Stream Filter of 1 list based on another list

可紊 提交于 2019-12-29 04:29:12
问题 I am posting my query after having searched in this forum & google, but was unable to resolve the same. eg: Link1 Link2 Link3 I am trying to filter List 2 (multi column) based on the values in List 1. List1: - [Datsun] - [Volvo] - [BMW] - [Mercedes] List2: - [1-Jun-1995, Audi, 25.3, 500.4, 300] - [7-Apr-1996, BMW, 35.3, 250.2, 500] - [3-May-1996, Porsche, 45.3, 750.8, 200] - [2-Nov-1998, Volvo, 75.3, 150.2, 100] - [7-Dec-1999, BMW, 95.3, 850.2, 900] expected o/p: - [7-Apr-1996, BMW, 35.3, 250

Java Streams: group a List into a Map of Maps

假如想象 提交于 2019-12-29 03:57:26
问题 How could I do the following with Java Streams? Let's say I have the following classes: class Foo { Bar b; } class Bar { String id; String date; } I have a List<Foo> and I want to convert it to a Map <Foo.b.id, Map<Foo.b.date, Foo> . I.e: group first by the Foo.b.id and then by Foo.b.date . I'm struggling with the following 2-step approach, but the second one doesn't even compile: Map<String, List<Foo>> groupById = myList .stream() .collect( Collectors.groupingBy( foo -> foo.getBar().getId()

Take every nth element from a Java 8 stream

拥有回忆 提交于 2019-12-29 03:32:11
问题 Suppose I have a list like this : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Is it possible to use a Java 8 stream to take every second element from this list to obtain the following? [1, 3, 5, 7, 9] Or maybe even every third element? [1, 4, 7, 10] Basically, I'm looking for a function to take every nth element of a stream: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> list2 = list.stream().takenth(3).collect(Collectors.toList()); System.out.println(list2); // => [1, 4

when i use Java 8 Stream.of primitive type, the result is confused

て烟熏妆下的殇ゞ 提交于 2019-12-29 00:44:14
问题 byte[] a = {1,2,3}; System.out.println(Stream.of(a).count()); Byte[] b = {1,2,3}; System.out.println(Stream.of(b).count()); the result is 1 and 3, why? 回答1: Stream.of only accepts Objects as its arguments. A byte isn't an Object, but a byte array is. If a is an array of byte , then Stream.of(a) can only mean "stream of this one object, which is an array". If you have a Byte[] array, then each element of the array is an object, so the compiler can guess that's what you mean. There is