java-stream

How create a new map from the values in an existing map

守給你的承諾、 提交于 2019-12-01 15:29:11
Having the next original map: G1=[7,8,45,6,9] G2=[3,9,34,2,1,65] G3=[6,5,9,1,67,5] Where G1, G2 and G3 are groups of people's ages, How can I create a new map like this: 45=[7,8,45,6,9] 65=[3,9,34,2,1,65] 67=[6,5,9,1,67,5] Where the new keys are the max people's age in each group. I have tried this: Map<Integer, List<Integer>> newMap = originalMap.entrySet().stream() .collect(Collectors.toMap(Collections.max(x -> x.getValue()), x -> x.getValue())); But the compiler say me: "The target type of this expression must be a functional interface" in this fragment of code: Collections.max(x -> x

Merge all list values together in a map [duplicate]

旧巷老猫 提交于 2019-12-01 14:53:49
This question already has an answer here: How to retainAll of List of Lists using stream reduce 2 answers I would like to convert a Map like: Map<String, List<String>> to List<String> where the result list is the merge of all List values. You can just have List<String> result = map.values().stream().flatMap(List::stream).collect(Collectors.toList()); This retrieves the values of the map with values() then flat maps each list into a Stream formed by its elements and collects the result into a list. Another alternative, without flat mapping each list, and thus may be more performant, is to

Is collectingAndThen method enough efficient?

巧了我就是萌 提交于 2019-12-01 13:45:57
I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks. Here is my code: System.out.println("CollectingAndThen"); Long t = System.currentTimeMillis(); String personWithMaxAge = persons.stream() .collect(Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none" )); System.out.println("personWithMaxAge - "+personWithMaxAge + " time taken = "+(System.currentTimeMillis() - t));

Best way performance wise to use limit on stream in case of multithreading

随声附和 提交于 2019-12-01 13:43:34
I watched a talk by José Paumard on InfoQ : http://www.infoq.com/fr/presentations/jdk8-lambdas-streams-collectors (French) The thing is I got stuck on this one point. To collect 1M Long using stream AND multithreading we can do it this way : Stream<Long> stream = Stream.generate(() -> ThreadLocalRandom.current().nextLong()) ; List<Long> list1 = stream.parallel().limit(10_000_000).collect(Collectors.toList()) ; But given the fact that the threads are always checking the said limit in hinders performance. In that talk we also see this second solution : Stream<Long> stream = ThreadLocalRandom

Java 8 Streams: IntStream to String [duplicate]

女生的网名这么多〃 提交于 2019-12-01 13:43:03
问题 This question already has answers here : Simplest way to print an `IntStream` as a `String` (9 answers) Closed 4 years ago . In Java 8 streams API, calling chars() on any String object returns an IntStream object containing all the characters. What would be the correct way to convert the returned IntStream object back to a String ? Calling toArray() would give me an int[] , which is not accepted by any of the String constructor. 回答1: You can use toArray() , then the String(int[], int, int)

Merge all list values together in a map [duplicate]

蹲街弑〆低调 提交于 2019-12-01 13:39:15
问题 This question already has answers here : How to retainAll of List of Lists using stream reduce (2 answers) Closed 3 years ago . I would like to convert a Map like: Map<String, List<String>> to List<String> where the result list is the merge of all List values. 回答1: You can just have List<String> result = map.values().stream().flatMap(List::stream).collect(Collectors.toList()); This retrieves the values of the map with values() then flat maps each list into a Stream formed by its elements and

Using two streams in Java lambda to compute covariance

假如想象 提交于 2019-12-01 13:27:38
Let's say I have two arrays of double. I've been experimenting with Stream from Java 8. I think I've understood the main ideas but then I realised that I'm not sure how to manipulate two Streams at the same time. For example, I want to calculate the covariance of both arrays. public class foo { public static double mean(double[] xs) { return Arrays.stream(xs).average().getAsDouble(); } public static void main(String[] args) { double[] xs = {1, 2, 3, 4, 5, 6, 7, 8, 9}; double[] ys = {1517.93, 1757.78, 1981.1, 2215.73, 2942.66, 3558.32, 4063.91, 4521.16, 5101.76, 5234.12}; System.out.println(

Refactoring nested for loop into Java 8 stream

ε祈祈猫儿з 提交于 2019-12-01 12:53:10
I have the following for loop: List<Map> mapList = new ArrayList<>(); for (Resource resource : getResources()) { for (Method method : resource.getMethods()) { mapList.add(getMap(resource,method)); } } return mapList; How could I refactor this nested loop into a Java 8 stream? Eran You can use flatMap to obtain all the Map s for all Method s of all Resource s : List<Map> mapList = getResources().stream() .flatMap(r->r.getMethods().stream().map(m->getMap(r,m))) .collect(Collectors.toList()); 来源: https://stackoverflow.com/questions/34589980/refactoring-nested-for-loop-into-java-8-stream

How to add dateTime values in a stream(java)?

妖精的绣舞 提交于 2019-12-01 12:40:33
Today I started learning Java 8, so I'm quite new to Java 8 and it's stuff. I have a list of activities. An activity has name, startTime, endTime. For startTime and endTime i'm using DateTime from joda time. I'm trying to determine a data structure of the form Map (String, DateTime) that maps for each activity the total duration computed over the monitoring period. Here is a snippet of my code: import java.util.ArrayList; import java.util.Map; import org.joda.time.DateTime; class Activity { public DateTime startTime; public DateTime endTime; public String activityLabel; public Activity

Convert List of Strings into Map using Java-8 Streams API

邮差的信 提交于 2019-12-01 12:05:50
I have List List<String> cars = Arrays.asList("Ford", "Focus", "Toyota", "Yaris","Nissan", "Micra", "Honda", "Civic"); Now, can I convert this List into Map where I get ford = focus, Toyota = yaris, Nisan = Micra, Honda = Civic using Java 8 Streams API? Here is an example on how you could do it : Map<String, String> carsMap = IntStream.iterate(0, i -> i + 2).limit(cars.size() / 2) .boxed() .collect(Collectors.toMap(i -> cars.get(i), i -> cars.get(i + 1))); Basically, just iterates over every 2 elements and maps it with the next one. Note that if the number of elements is not even, it won't