Java 8 stream for-loop

2015-03-30T17:28:51

Im new to Java 8 Streams and would like to convert following code-block to Java 8's Stream way of doing the same thing.

Edit: Updates the class-names to be less confusing. (Removed Foo, Bar, Baz...)

ArrayList<PriceList> priceLists = new ArrayList<PriceList>();

// I'm casting to a type-safe List from getObjects() 
// -which is a function I dont have access to. Is there a nice 
// solution to embed this in the stream-syntax?
List<PriceListGroup> distObjects = (List<PriceListGroup>) objects.get(1).getObjects();

for(PriceListGroup group : distObjects) {
    Set<Affiliate> affiliates = group.getAffiliates();
    for(Affiliate affiliate : affiliates) {
        priceLists.add(affiliate.getPriceList());
    }
}

All help & explanation appreciated

Copyright License:
Author:「henrik」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/29342659/java-8-stream-for-loop

About “Java 8 stream for-loop” questions

Im new to Java 8 Streams and would like to convert following code-block to Java 8's Stream way of doing the same thing. Edit: Updates the class-names to be less confusing. (Removed Foo, Bar, Baz.....
guys. I was doing my project for now, and then making service layers for this codes of reading select query, i have a question now. i am pretty new web developer, so I am kinda confused of JAVA8
I just started to learn streams in java8. I am trying to convert a basic for each loop into a stream that does the exact same thing. I have checked around here and found this: Convert For-Loop into...
Using the basic for-loop, I have written the following : Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;(); for (int i=0; i &lt; list.size(); i++) { int id = list.get(i).getId();
How can I convert this code to Java 8 stream? String getFirst(String key) { for (Param p : params) { if (key.equals(p.getKey())) { if (!p.getValues().isEmpty()) { ...
I'm new to Java 8 Streams and I'm currently trying to convert a for loop into a java 8 stream. Could I get some help? for (Subscription sub : sellerSubscriptions) { if (orders.get(Product).tes...
Just for practice purposes, I want to use Java 8's stream functionality to implement code, where I am looking for a HTTP header name and returning the value associated with it. For example, just say
While working with Java 8 Streams, I some times find that Stream doesn't have a particular method that I desire (e.g. takeWhile(), dropWhile(), skipLast()). How do I create my own stream class whi...
I am trying to compute Prime numbers with Java8 streams, but I get into an IllegalStateException : stream has already been operated upon or closed. This is my code : package experimentations.chap...
I have a List&lt;Foo&gt; and want a Multimap&lt;String, Foo&gt; where we've grouped the Foo's by their getId() function. I am using Java 8 and its almost awesome in that you can do: List&lt;Foo&gt;...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.