java-stream

How knows Stream.toArray(Book[]::new); how many elements the array has and where is the implementation of “Book[]::new”

落爺英雄遲暮 提交于 2021-02-20 04:26:27
问题 I'm triying to understand method references and I don't know how with this "Book[]::new" it can create an array with the right number of elements. Book[] arrayBook = stBooks.toArray(Book[]::new); In order to create the array when I use the second and third option I have to specify a Functional Interface that Receive an Int and return a new Array. But, in the first option I don't know where is the implementation and where you specify the number or elements that is going to have the array. I

Is my understanding of Java Stream.flatMap correct?

試著忘記壹切 提交于 2021-02-20 03:41:25
问题 I was trying to answer this question, but I did not because I don't understand Streams well enough. Please tell me if my explanation is correct or not. My answer : import java.util.Arrays; import java.util.stream.Stream; public class Temp { public static void main(String [] args){ String [] input = {"1,2", "3,4", "5"}; String [] expected = {"1", "2", "3", "4", "5"}; String [] actual = Stream.of(input) .flatMap(s -> Arrays.stream(s.split(","))) .toArray(String [] :: new); //Testing - Runs only

Converting Nested For Loops To Streams

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-19 07:45:26
问题 I'm having some trouble understanding streams. I've looked around and can't seem to find an example that matches my use case. I have an existing nested for loop: List<ObjectB> objectBs = new ArrayList<ObjectB>(); for (ObjectA objA: objectAList) { for (ObjectB objB: objA.getObjectBList()) { if (objB.getNumber() != 2) { objectBs.add(objB); } } } Alot of exampls show how to add objB.getNumber() to a list but not objB . 回答1: You can use flatMap to obtain a Stream<ObjectB> of all the ObjectB

Count Elements in a stream and return Integer insted of long

浪子不回头ぞ 提交于 2021-02-19 07:35:08
问题 I need to count Elements in a Stream and assign it to an Integer without casting. .count() does return long thought about the .collect(Collectors.reducing(..)) but cant figure it out. I feel like there is something simple I don't get. My Try: Stream<String> s = Stream.of("Hallo ", "Test", "String"); Integer count = s.filter(e -> (e.length() >= lb && e.length() <= ub && !e.contains(" "))) .map(e -> e.toUpperCase()) .distinct() .collect(Collectors.reducing(0, e -> 1, Integer::sum))); System.out

How to split a string into a map, grouping values by duplicate keys using streams?

北城余情 提交于 2021-02-19 07:24:27
问题 I want to convert the following String flString="view1:filedname11,view1:filedname12,view2:fieldname21"; to a Map<String,Set<String>> to get the key/value as below: view1=[filedname11,filedname12] view2=[fieldname21] I want to use Java 8 streams. I tried Arrays.stream(tokens) .map(a -> a.split(":")) .collect(Collectors.groupingBy( a -> a[0], Collectors.toList())); However the keys are also getting added to the value list. 回答1: You should use a Collectors::mapping to map the array to an

Replace for-each loop with lambda expression

纵然是瞬间 提交于 2021-02-19 06:25:09
问题 I'm just refactoring some of my old projects to use features of Java 8. int counter = 1; for (Checker checker : checkers) { if (counter < checkers.size()) { checker.setNextChecker(checkers.get(counter++)); } } Here's kinda Chain of Resp pattern. And I need to set next checker for every checker in the list, excluding the last one. Still can't find the way to use Stream API here :) 回答1: Using IntStream.range : IntStream.range(1, checkers.size()) .forEach(i -> checkers.get(i-1).setNextChecker

Map to 2d array with Streams

十年热恋 提交于 2021-02-19 04:17:26
问题 I am trying to create a 2d array of String using Streams: String[] fruit1DArray; String[][] fruit2DArray; Map<String, String> fruitMap = new HashMap<>(); fruitMap.put("apple", "red"); fruitMap.put("pear", "green"); fruitMap.put("orange", "orange"); fruit1DArray = fruitMap.entrySet() .stream() .map(key -> key.getKey()) .toArray(size -> new String[size]); fruit2DArray = fruitMap.entrySet() .stream() .map(entry-> new String[]{entry.getKey()}) .toArray(size -> new String[size][1]); System.out

Map to 2d array with Streams

半腔热情 提交于 2021-02-19 04:10:46
问题 I am trying to create a 2d array of String using Streams: String[] fruit1DArray; String[][] fruit2DArray; Map<String, String> fruitMap = new HashMap<>(); fruitMap.put("apple", "red"); fruitMap.put("pear", "green"); fruitMap.put("orange", "orange"); fruit1DArray = fruitMap.entrySet() .stream() .map(key -> key.getKey()) .toArray(size -> new String[size]); fruit2DArray = fruitMap.entrySet() .stream() .map(entry-> new String[]{entry.getKey()}) .toArray(size -> new String[size][1]); System.out

Use of constructor reference where constructor has a non-empty parameter list

泪湿孤枕 提交于 2021-02-19 02:14:55
问题 Given.. List<Foo> copy(List<Foo> foos) { return foos .stream() .map(foo -> new Foo(foo)) .collect(Collectors.toList()); } IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference". I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here. 回答1: I'm aware of the Foo::new syntax for the no-arg constructor That's not what Foo::new does. This expression will expand to what is

Use of constructor reference where constructor has a non-empty parameter list

谁说胖子不能爱 提交于 2021-02-19 02:09:23
问题 Given.. List<Foo> copy(List<Foo> foos) { return foos .stream() .map(foo -> new Foo(foo)) .collect(Collectors.toList()); } IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference". I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here. 回答1: I'm aware of the Foo::new syntax for the no-arg constructor That's not what Foo::new does. This expression will expand to what is