java-stream

How are lazy streams implemented in Java 8?

徘徊边缘 提交于 2019-12-03 06:55:44
问题 I am reading Java 8, specifically the "Streams API". I wanted to know how streams can be lazy? I believe streams are just added as a library and there are no changes done to the language to support laziness. Also, I will be shocked if somebody tells me it's achieved through reflection. 回答1: Why would you need reflection to get laziness? For example, consider this class: class LazySeq<T> { private final List<T> list; private Predicate<? super T> predicate; public LazySeq(List<T> input) { this

What is the difference between Streams and Collections in Java 8 [closed]

一曲冷凌霜 提交于 2019-12-03 06:50:46
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . I'm learning about Streams in Java 8. I got confused about this concept: A collection is an in-memory data structure, which holds all the values that the data structure currently has—every element in the collection has to be computed before it can be added to the collection. In contrast, a stream

Java stream map and collect - order of resulting container

◇◆丶佛笑我妖孽 提交于 2019-12-03 06:28:59
问题 List<MyObject> myList = new ArrayList<>(); //populate myList here List<String> nameList = myList.stream() .map(MyObject::getName) .collect(Collectors.toList()); In above code, can I expect that order of MyObject names in nameList is always the same as the order of myList ? 回答1: Yes, you can expect this even if you are using parallel stream as long as you did not explicitly convert it into unordered() mode. The ordering never changes in sequential mode, but may change in parallel mode. The

Java 8: Preferred way to count iterations of a lambda?

我们两清 提交于 2019-12-03 06:26:45
问题 I face the same problem often. I need to count the runs of a lambda for use outside the lambda. E.g.: myStream.stream().filter(...).forEach(item->{ ... ; runCount++); System.out.println("The lambda ran "+runCount+"times"); The issue is that runCount needs to be final, so it cannot be an int. It cannot be an Integer because that's immutable. I could make it class level variable (i.e. a field) but I'll only need it in this block of code. I know there are various ways, I'm just curious what is

Java stream - Sort a List to a HashMap of Lists

非 Y 不嫁゛ 提交于 2019-12-03 06:20:46
问题 Let's say I have a Dog class. Inside it I have a Map<String,String> and one of the values is Breed . public class Dog { String id; ... public Map<String,String> } I want to get a Map of List s: HashMap<String, List<Dog>> // breed to a List<Dog> I'd prefer to use a Stream rather than iterating it. How can I do it? 回答1: You can do it with groupingBy . Assuming that your input is a List<Dog> , the Map member inside the Dog class is called map , and the Breed is stored for the "Breed" key : List

How do I turn a Java Enumeration into a Stream?

天涯浪子 提交于 2019-12-03 06:10:38
问题 I have a third party library that gives me an Enumeration<String> . I want to work with that enumeration lazily as a Java 8 Stream , calling things like filter , map and flatMap on it. Is there an existing library that has this in it? I am already referencing Guava and Apache Commons so if either of those have the solution that would be ideal. Alternatively, what is the best/easiest way to turn an Enumeration into a Stream while retaining the lazy nature of everything? 回答1: This answer

Java 8 Applying stream filter based on a condition

淺唱寂寞╮ 提交于 2019-12-03 06:00:17
In Java 8, is there a way to apply the filter on a stream based on a condition, example I have this stream if (isAccessDisplayEnabled) { src = (List < Source > ) sourceMeta.getAllSources.parallelStream() .filter(k - > isAccessDisplayEnabled((Source) k)) .filter(k - > containsAll((Source) k, substrings, searchString)) .collect(Collectors.toList()); } else { src = (List < Source > ) sourceMeta.getAllSources.parallelStream() .filter(k - > containsAll((Source) k, substrings, searchString)) .collect(Collectors.toList()); } I am adding the filter .filter(k - > isAccessDisplayEnabled((Source) k))) on

Java .parallelStream() with spring annotated methods

泄露秘密 提交于 2019-12-03 05:42:32
I try using the parallelStream() in DAO with Spring @Transactional annotations and get so problem: @Transactional public void processCollection(Collection<Object> objects) { objects.parallelStream() .forEach(this::processOne); //throw exception } @Transactional public void processOne(Object o) { ... } Works correct: @Transactional public void processCollection(Collection<Object> objects) { objects.stream() .forEach(this::processOne); //work correctly } @Transactional public void processOne(Object o) { ... } Exception: org.hibernate.HibernateException: No Session found for current thread org

Java 8 streams and maps worth it?

耗尽温柔 提交于 2019-12-03 05:30:57
It feels like java 8 streams and mapping functions are so verbose they aren't really an improvement. For example, I wrote some code that uses a collection to generate another, modified collection: private List<DartField> getDartFields(Class<?> model) { List<DartField> fields = new ArrayList<>(); for (Field field : model.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers())) { fields.add(DartField.getDartField(field)); } } return fields; } This seems like the ideal use case for java 8 streams and their functions, so I rewrote it like that: private List<DartField> getDartFields

How to separate a List by a condition using Java 8 streams

柔情痞子 提交于 2019-12-03 05:15:29
Consider the following code: List<Integer> odd = new ArrayList<Integer>(); List<Integer> even = null; List<Integer> myList = Arrays.asList(1,2,3,4,5,6,7,8,9,10); even = myList.stream() .filter(item -> { if(item%2 == 0) { return true;} else { odd.add(item); return false; } }) .collect(Collectors.toList()); What I am trying to do here is get the even and odd values from a list into separate lists. The stream filter() method returns true for even items and the stream collector will collect them. For the odd case, the filter will return false and the item will never reach the collector. So I am