-2

I recently started checking new Java 8 features.

I've come across this forEach iterator-which iterates over the Collection.

Let's take I've one ArrayList of type <Integer> having values= {1,2,3,4,5}

list.forEach(i -> System.out.println(i));

This statement iteates over a list and prints the values inside it.

I'd like to know How am I going to specify that I want it to iterate over some specific values only.

Like, I want it to start from 2nd value and iterate it till 2nd last value. or something like that- or on alternate elements.

How am I going to do that?

inityk
  • 476
  • 1
  • 9
  • 18

2 Answers2

5

To iterate on a section of the original list, use the subList method:

list.subList(1, list.length()-1)
    .stream() // This line is optional since List already has a foreach method taking a Consumer as parameter
    .forEach(...);
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Joni
  • 108,737
  • 14
  • 143
  • 193
  • oh it seems great. but what if I want to choose every other element from the list, like 1, 3, 5, ... For that @BoristheSpider's answer is appropriate? – inityk May 22 '16 at 14:15
  • 1
    @inityk for that I'd do `IntStream.iterate(0, i -> i+2).limit(list.size()/2).map(list::get)`. But that only works for `List` implementations that support `RandomAccess`. – Boris the Spider May 22 '16 at 14:17
  • @Joni, just a small correction, `list.size()` would come instead of `list.length()` Apart from this small change, answer is perfect! – inityk May 22 '16 at 15:59
  • @BoristheSpider, based on your comment, `list::get` how would it choose `list.get(index)`? would you please explain that part? – inityk May 22 '16 at 16:12
  • @inityk because it's an `IntStream`. – Boris the Spider May 22 '16 at 17:34
  • 2
    No need for `stream`, just use `foreach` – Yassin Hajaj May 22 '16 at 21:25
  • `.stream()` is not “optional”, it causes to perform an entire different operation. So unless you explicitly want to perform that different operation, don’t insert it. To name a consequence `stream().forEach(…)` is not guaranteed to maintain the order of elements. – Holger May 23 '16 at 10:25
1

This is the concept of streams. After one operation, the results of that operation become the input for the next.

So for your specific example, you can follow @Joni's command. But if you're asking in general, then you can create a filter to only get the values you want to loop over.

For example, if you only wanted to print the even numbers, you could create a filter on the streams before you forEached them. Like this:

List<Integer> intList = Arrays.asList(1,2,3,4,5);

intList.stream()
       .filter(e -> (e & 1) == 0)
       .forEach(System.out::println);

You can similarly pick out the stuff you want to loop over before reaching your terminal operation (in your case the forEach) on the stream. I suggest you read this stream tutorial to get a better idea of how they work: http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/

Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44
  • Sure. There are other stream tutorials, but this one is the easiest to follow, in my opinion. If my answer answered your question, please consider accepting it, rather than just upvoting it ;) – Somaiah Kumbera May 22 '16 at 17:26
  • Yes, sure I'd do this- What I'm asking is I'd like to loop over even or odd index. `for(int i = 0;i – inityk May 22 '16 at 17:47
  • What youre asking for now is a different question :) Streams don't come with indexes, but if you really really want an index, you can look at this question: http://stackoverflow.com/questions/22793006/java-8-foreach-with-index?lq=1 – Somaiah Kumbera May 22 '16 at 18:01
  • 1
    But really in such cases where you need to manipulate indexes, its cleaner code to just use a good old fashioned for loop. Streams are not meant to completely replace for loops – Somaiah Kumbera May 22 '16 at 18:09
  • Oh really? It sounds like great advice – inityk May 22 '16 at 18:18