java-stream

Nested forEach in Java 8

馋奶兔 提交于 2019-12-25 08:29:34
问题 I want to rewrite this piece of code in terms of streams from Java 8 for (Medium medium : sortierteMedien) { System.out.println("Fuenf aehnlichste Medien fuer " + medium.getClass() + " mit dem Titel " + medium.getTitel() + ":\n"); for (Medium medium1 : bibliothek.medienSim(medium)) { System.out.println(medium1.toString()); } System.out.println(); } The difficulty here is, that there is a print statement before and after each inner for loop. Is it possible to rewrite this with streams? 回答1:

use stream to sum all values from array stored in map

早过忘川 提交于 2019-12-24 17:43:30
问题 I have a map which looks like this: Map<String,String[]> urlFormEncoded = new HashMap<String,String[]>(); and I would like to sum all values stored in String[] arrays as a double value I tried something like this: double requestAmount = urlFormEncoded.entrySet().stream().mapToDouble(k -> Arrays.stream(k.getValue()).).sum(); but unfortunately I don't know how to convert this String[] to value :( I would like to do this using streams and lambda expressions 回答1: The first step would be to

How to map strings to URIs?

青春壹個敷衍的年華 提交于 2019-12-24 12:08:10
问题 Let me say that I have a single string concatenated uris. http://...[sp]http://...[sp]http://... I'm trying to convert each split string to URIs. Stream.of(string.split("\\s")).map(uri -> URI::new); Compiler complains. Cannot infer type-variable(s) R What did I do wrong? 回答1: Stream.of(s.split("//s")).map(URI::new); click here for an example 来源: https://stackoverflow.com/questions/27613957/how-to-map-strings-to-uris

Java 8 Stream utilities for input data

两盒软妹~` 提交于 2019-12-24 10:38:36
问题 Imagine having some sort of incoming data either by callback or an InputStream that you need to continuously convert into a Java 8 Stream . We don't know when the incoming data-stream stops, but we know that it can stop. So far I've seen two ways of getting around this problem and I'm interested in the best practices on how to achieve this. Mainly because I this must be something someone has faced before. There must be a simpler way of doing it than the ideas below. 1) The most simple way is

Converting Stream into IntStream

做~自己de王妃 提交于 2019-12-24 08:44:23
问题 I am setting the id for each list item where primary address for two list are equal. Server POJO public class Server { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") public Integer id; @OneToMany (mappedBy = "server",cascade = CascadeType.ALL, orphanRemoval = true) private List<IPAddress> ipaddresses; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setIpaddresses(List<IPAddress> ipaddresses) { this.ipaddresses =

How to properly submit and get several Futures in the same Java stream?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 06:38:33
问题 I try to submit and get 10 Future s in the same stream. Each one takes 1 second to process and I would like to run them in parallel. My first try is takes_10_sec() which runs sequentially and takes 10s. My second try is takes_1_sec() which runs in parallel and takes 1s. However it uses an intermediate .collect(Collectors.toList()).stream() which I don't think is a good way to do it. Is there another recommended way? public class FutureStream { private ExecutorService executor = Executors

How can I sort an IntStream in ascending order?

南笙酒味 提交于 2019-12-24 05:42:33
问题 I've converted a 2D int array into a Stream: IntStream dataStream = Arrays.stream(data).flatMapToInt(x -> Arrays.stream(x)); Now, I want to sort the list into ascending order. I've tried this: dataStream.sorted().collect(Collectors.toList()); but I get the compile time error I'm confused about this, because on the examples I've seen, similar things are done without errors. 回答1: Try with dataStream.sorted().boxed().collect(Collectors.toList()); because collect(Collectors.toList()) does not

How can I sort an IntStream in ascending order?

孤人 提交于 2019-12-24 05:40:13
问题 I've converted a 2D int array into a Stream: IntStream dataStream = Arrays.stream(data).flatMapToInt(x -> Arrays.stream(x)); Now, I want to sort the list into ascending order. I've tried this: dataStream.sorted().collect(Collectors.toList()); but I get the compile time error I'm confused about this, because on the examples I've seen, similar things are done without errors. 回答1: Try with dataStream.sorted().boxed().collect(Collectors.toList()); because collect(Collectors.toList()) does not

Java stream that is distinct by more than one property

自古美人都是妖i 提交于 2019-12-24 03:14:10
问题 I have following objects in the stream: class Foo{ String a; String b; int c; } I would like to filter a stream based on following criteria: eg. Having entries in stream: foo1 and foo2 : foo1 and foo2 have same values for a and b , but they differ in c property. I would like to get rid of entries that have c higher in such case. 回答1: Semantically equivalent to Eugene’s answer, but a bit simpler: List<Foo> foos = Stream.of(new Foo("a", "b", 1), new Foo("a", "b", 2), new Foo("a", "b", 3), new

How to reduce a stream into another stream in Java8?

我的梦境 提交于 2019-12-24 02:55:06
问题 As an example, I want to create an infinite stream of Groups of tens like this: 0=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 1=[10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 2=[20, 21, 22, 23, 24, 25, 26, 27, 28, 29] ... I want to use an inifinte stream of ints as an Input, which should then be grouped. If the first stream iterates 10 times the resulting stream should have iterated only once. My working, but not very elegant code looks like this: // create a stream from 0 (inclusive) to 100 (exclusive)