---
title: "Use streams for terminating loop over collection conditionally"  
description: "Use streams for terminating loop over collection conditionally"  
author: "marcel ethan"  
published: 2015-12-20  
updated: 2015-12-21  
canonical: https://www.mindstick.com/forum/33760/use-streams-for-terminating-loop-over-collection-conditionally  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Use streams for terminating loop over collection conditionally

How to use streams to achieve something like what the following [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) snippet [shows](https://yourviews.mindstick.com/view/81380/new-york-violent-shooting-shows-people-are-not-safe-in-night-life) ? Basically, I need to terminate a loop [returning](https://www.mindstick.com/news/2477/returning-ceo-bob-iger-dismisses-the-disney-apple-sale-as-pure-speculation) one [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) based on a [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition), or returning another value again, based on a condition.\

```
enum Day {  SUNDAY, MONDAY, TUESDAY, WEDNESDAY} class MyObj {  Day d;  public Day getDay();}List<MyObj> myObjList;Day Myfunc () {// If atleast one obj belongs to SUNDAY or MONDAY, return.Day myDay = null;for(MyObj myObj : myObjList) {  if(myObj.getDay() == Day.SUNDAY || myObj.getDay() == Day.MONDAY) {    return myObj.getDay();  }  else if (myObj.getDay() == Day.TUESDAY) {    myDay = myObj.getDay();  } }return myDay;}
```

## Replies

### Reply by Anonymous User

I understand the logic of your function as the following:\
If there's SUNDAY or MONDAY in the input, return the first one of them.Otherwise if there's TUESDAY, return it.Otherwise return null.You can implement this logic in the Java-8 style iterating the list twice and using **Optional.orElseGet**:\

```
myObjList.stream().map(MyObj::getDay).filter(d -> d == Day.SUNDAY || d == Day.MONDAY)    .findFirst()    .orElseGet(() -> myObjList        .stream().map(MyObj::getDay).anyMatch(d -> d == Day.TUESDAY) ? Day.TUESDAY : null);
```

Looks kinda ugly, so better to stick with the original imperative code.\
An alternative one-pass solution would be to introduce the days priority and use it:\

```
int priority(Day d) {    switch(d) {    case SUNDAY:    case MONDAY:        return 10; // max priority    case TUESDAY:        return 5;    default:        return 0;    }}
```

Now you can use Stream.max:\

```
return myObjList.stream().map(MyObj::getDay).max(Comparator.comparingInt(this::priority))        .filter(day -> priority(day) > 0).orElse(null);
```

Though it's longer than original code, such solution looks more flexible: you can easily add more cases tweaking the priority method.


---

Original Source: https://www.mindstick.com/forum/33760/use-streams-for-terminating-loop-over-collection-conditionally

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
