java-stream

Using Streams instead of for loop in java 8

让人想犯罪 __ 提交于 2020-06-09 10:15:48
问题 int [] numbers = {1,2,3,4,5,6,7,8}; int [] doubleNumbers = new int[numbers.length]; int [] tripleNumbers = new int[numbers.length]; for(int index = 0; index < numbers.length; index++) { doubleNumbers[index] = numbers[index] * 2; tripleNumbers[index] = numbers[index] * 3; } System.out.println("Double Numbers"); Arrays.stream(doubleNumbers).forEach(System.out::println); System.out.println("Triple Numbers"); Arrays.stream(tripleNumbers).forEach(System.out::println); I have above code where I

Creating an immutable list from an existing list using streams

泪湿孤枕 提交于 2020-06-08 07:08:49
问题 There is a list of Person objects. List<Person> persons = generatePersons(); An unmodifiableList is created with it. List<Person> unmodifiableList = Collections.unmodifiableList(persons); I understand that unmodifiableList doesn't support add/remove/set operations. At the same time it is not immutable since it has a reference to an existing modifiable list persons and whenever changes are made to the persons list, the changes are reflected in unmodifiableList too. An immutable list is created

How to aggregate based on certain value and create nested map given a list of objects?

自古美人都是妖i 提交于 2020-05-30 07:31:48
问题 I have a List<SingleDay> where SingleDay is: class SingleDay{ private Date date; private String County; // otherstuff } I would like to convert this into a Map<LocalDate, Map<String,SingleDay>> by the following transformation: Key: 2020-05-27, Value: {'County1'=SingleDay [date=2020-05-27, County='County1'], 'County2'=SingleDay [date=2020-05-27, County='County2'], 'County3'=SingleDay [date=2020-05-27, County='County3'] } Key: 2020-05-28, Value: {'County4'=SingleDay [date=2020-05-28, County=

Grouping the custom object list by properties using Java 8

假装没事ソ 提交于 2020-05-29 05:59:17
问题 I am learning many new features of lambda and wondering how can I group by my custom object list based on certain properties as key? For example, I have list of object like this in json. [{ "account" : "checking", "source" : "BOA" }, { "account" : "checking", "source" : "TD" }, { "account" : "saving", "source" : "WS" } ] I am looking for way to group using java 8 feature to get output like this (grouping source as comma separated for same account. [{ "account" : "checking", "source" : "BOA,

Using Java streams to merge a pair of `int` arrays [duplicate]

强颜欢笑 提交于 2020-05-27 11:52:39
问题 This question already has answers here : Concatenating two int[] (3 answers) Combine two integer arrays [duplicate] (9 answers) Closed last month . This page shows how to combine two arrays of Integer objects into an array of Object objects. Integer[] firstArray = new Integer[] { 10 , 20 , 30 , 40 }; Integer[] secondArray = new Integer[] { 50 , 60 , 70 , 80 }; Object[] merged = Stream .of( firstArray , secondArray ) .flatMap( Stream :: of ) .toArray() ; Arrays.toString( merged ): [10, 20, 30,

Using Java streams to merge a pair of `int` arrays [duplicate]

只谈情不闲聊 提交于 2020-05-27 11:51:51
问题 This question already has answers here : Concatenating two int[] (3 answers) Combine two integer arrays [duplicate] (9 answers) Closed last month . This page shows how to combine two arrays of Integer objects into an array of Object objects. Integer[] firstArray = new Integer[] { 10 , 20 , 30 , 40 }; Integer[] secondArray = new Integer[] { 50 , 60 , 70 , 80 }; Object[] merged = Stream .of( firstArray , secondArray ) .flatMap( Stream :: of ) .toArray() ; Arrays.toString( merged ): [10, 20, 30,

Splitting Strings in a stream in Java?

别来无恙 提交于 2020-05-26 10:02:13
问题 I have an assignment where we're reading textfiles and counting the occurrences of each word (ignoring punctuation). We don't have to use streams but I want to practice using them. So far I am able to read a text file and put each line in a string, and all the strings in a list using this: try (Stream<String> p = Files.lines(FOLDER_OF_TEXT_FILES)) { list = p.map(line -> line.replaceAll("[^A-Za-z0-9 ]", "")) .collect(Collectors.toList()); } However, so far, it simply makes all the lines a

Print unique values from collection using Stream API [duplicate]

帅比萌擦擦* 提交于 2020-05-26 09:26:09
问题 This question already has answers here : Java 8 Streams : get non repeated counts (5 answers) Closed 2 months ago . I need to get only unique values from the collection( values which do not have any dublicates in collection). For example, this code: ArrayList<Integer> g = new ArrayList<>(Arrays.asList(1,1,2,2,3,4,5,5,5,6,6)); System.out.println(Arrays.toString(g.stream().mapToInt(Integer::intValue).distinct().toArray())); gives me this result: [1, 2, 3, 4, 5, 6] However I want the result: [3,

Print unique values from collection using Stream API [duplicate]

我的未来我决定 提交于 2020-05-26 09:26:06
问题 This question already has answers here : Java 8 Streams : get non repeated counts (5 answers) Closed 2 months ago . I need to get only unique values from the collection( values which do not have any dublicates in collection). For example, this code: ArrayList<Integer> g = new ArrayList<>(Arrays.asList(1,1,2,2,3,4,5,5,5,6,6)); System.out.println(Arrays.toString(g.stream().mapToInt(Integer::intValue).distinct().toArray())); gives me this result: [1, 2, 3, 4, 5, 6] However I want the result: [3,

Java Streams .max() and .min() lag in performance?

拈花ヽ惹草 提交于 2020-05-25 08:47:14
问题 Consider Below 2 examples. 1 With Streams myList.stream().map(this::getInt).max(Integer::compareTo); 2 Old way int max = Integer.MIN_VALUE; for (MyItem item : myList) { max = Math.max(max, getInt(item)); } Above getInt method accepts a MyItem argument and returns an int result. Here, #2 gives me a much lower latency compared to #1. Does anyone have an idea why or anything going wrong for me? 回答1: myList.stream().mapToInt(this::getInt).max() Try mapping to an IntStream. An IntStream works with