java-stream

Is it discouraged using Java 8 parallel streams inside a Java EE container?

让人想犯罪 __ 提交于 2019-12-17 15:42:16
问题 Given that spawning threads in Java EE containers are discouraged. Would using the Java 8 parallel streams, which may spawn threads, inside Java EE be discouraged too? 回答1: EDIT See alternate answer from andrepnh . The below may have been the plan, but it doesn't appear to have played out that way in practice. The way I read it from the lambda-dev mailing list discussion mentioned in the comments: it's not discouraged the way spawning threads is - but won't do anything much for you in a Java

Why can't I map integers to strings when streaming from an array?

荒凉一梦 提交于 2019-12-17 15:35:36
问题 This code works (taken in the Javadoc): List<Integer> numbers = Arrays.asList(1, 2, 3, 4); String commaSeparatedNumbers = numbers.stream() .map(i -> i.toString()) .collect(Collectors.joining(", ")); This one can't be compiled: int[] numbers = {1, 2, 3, 4}; String commaSeparatedNumbers = Arrays.stream(numbers) .map((Integer i) -> i.toString()) .collect(Collectors.joining(", ")); IDEA tells me I have an "incompatible return type String in lambda expression". Why ? And how to fix that ? 回答1:

Java8 Streaming a class hierarchy [duplicate]

断了今生、忘了曾经 提交于 2019-12-17 14:21:50
问题 This question already has answers here : Java 8 Stream of Super Classes, Parent Files, Component Parents, linked list, etc (2 answers) Closed 3 years ago . I am slowly learning the new Java 8 features and I am trying to find a way to process a class hierarchy (from child to parent) as a stream. For instance find an annotation on a class or it's parents. Before Java 8, I would have done it this way : public static <T extends Annotation> T getAnnonationOn(Class<?> type, Class<T> annType) {

Java Streams: Replacing groupingBy and reducing by toMap [closed]

你。 提交于 2019-12-17 14:13:31
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 months ago . I've asked a question before about enhancing some code, here. @Holger gives me the right response and he said that: Whenever you find yourself using the reducing collector with groupingBy, you should check whether toMap isn’t more appropriate It seems like a pattern ! and what he

Stream.max(Integer::max) : Unexpected result [duplicate]

大憨熊 提交于 2019-12-17 13:56:15
问题 This question already has answers here : Java 8 stream's .min() and .max(): why does this compile? (5 answers) Closed 3 years ago . I'm studying for 1z0-809 : Java SE 8 Programmer II using Enthuware's mocktests. Encountering this question. List<Integer> ls = Arrays.asList(3,4,6,9,2,5,7); System.out.println(ls.stream().reduce(Integer.MIN_VALUE, (a, b)->a>b?a:b)); //1 System.out.println(ls.stream().max(Integer::max).get()); //2 System.out.println(ls.stream().max(Integer::compare).get()); //3

Why does this stream return no element?

删除回忆录丶 提交于 2019-12-17 13:02:35
问题 I tried to write the following code as a stream: AbstractDevice myDevice = null; for (AbstractDevice device : session.getWorkplace().getDevices()) { if (device.getPluginconfig().getPluginType().getId() == 1) { myDevice = device; } } this code works fine. But when I rewrite it like this it doesn't work anymore: myDevice = session.getWorkplace().getDevices().stream() .filter(s -> s.getPluginconfig().getPluginType().getId() == 1) .findFirst().get(); The Optional which I get back from the stream

Is flatMap guaranteed to be lazy? [duplicate]

假如想象 提交于 2019-12-17 12:03:12
问题 This question already has answers here : Why filter() after flatMap() is “not completely” lazy in Java streams? (7 answers) Closed 5 months ago . Consider the following code: urls.stream() .flatMap(url -> fetchDataFromInternet(url).stream()) .filter(...) .findFirst() .get(); Will fetchDataFromInternet be called for second url when the first one was enough? I tried with a smaller example and it looks like working as expected. i.e processes data one by one but can this behavior be relied on? If

How to implement a Java stream?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 10:58:18
问题 I want to implement a Stream<T> . I don't want to just use implements Stream<T> , because I would have to implement a ton of methods. Can this be avoided? To be more concrete, how can I stream t1 , t2 and t3 for example: class Foo<T> { T t1, t2, t3; Foo(T t1, T t2, T t3) { this.t1 = t1; this.t2 = t2; this.t3 = t3; } } 回答1: The JDK's standard implementation of Stream is the internal class java.util.stream.ReferencePipeline , you cannot instantiate it directly. Instead you can use java.util

Fetch first element which matches criteria

こ雲淡風輕ζ 提交于 2019-12-17 10:39:11
问题 How to get first element that matches a criteria in a stream? I've tried this but doesn't work this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name)); That criteria is not working, the filter method is invoked in an other class than Stop. public class Train { private final String name; private final SortedSet<Stop> stops; public Train(String name) { this.name = name; this.stops = new TreeSet<Stop>(); } public void addStop(Stop stop) { this.stops.add(stop); } public Stop

In Java 8 how do I transform a Map<K,V> to another Map<K,V> using a lambda?

一曲冷凌霜 提交于 2019-12-17 10:25:43
问题 I've just started looking at Java 8 and to try out lambdas I thought I'd try to rewrite a very simple thing I wrote recently. I need to turn a Map of String to Column into another Map of String to Column where the Column in the new Map is a defensive copy of the Column in the first Map. Column has a copy constructor. The closest I've got so far is: Map<String, Column> newColumnMap= new HashMap<>(); originalColumnMap.entrySet().stream().forEach(x -> newColumnMap.put(x.getKey(), new Column(x