1

I have transformed a regular for loop code into java 8 streams. I tried a few, but i am still learning this and running out of ideas, please suggest ideas. can this be further simplified ? Other than using forEach, I am not able to change much. Also, why do I have to typecast the eid to String in getERecordFromId((String)eid)

Stream <String>eIdsStream = getEidStream();

final HashSet<String> declinedRecords = new HashSet<>();

eIdsStream.forEach (eid ->  {
        ERecord eRecord = getERecordFromId((String)eid);
        if(eRecord.getEHash() != null &&  Status.DECLINED == eRecord.getStatus()) {
            declineRecords.add(eRecord.getEHash());
        }
    }
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Roma
  • 333
  • 3
  • 11
  • [Replacing your for-each loops with Stream.forEach might not be a great idea](https://stackoverflow.com/questions/16635398/java-8-iterable-foreach-vs-foreach-loop). – Jiri Tousek May 24 '19 at 06:32
  • Now that you changed your variable to a `Stream`, you don't need the casting to `String`, so at least that part of your question is no longer relevant (perhaps you should edit it out). – Eran May 24 '19 at 07:03

1 Answers1

6

The casting is required since you use a raw Stream variable. Assuming getEidStream() returns a Stream<String>, you should have assigned it to a Stream<String> variable, or not assigned it to a variable at all.

Using forEach defeats the purpose of using Streams in the first place.

You should use filter and map to transform the Stream to hold the required elements, and then collect to a Set.

Set<String> declinedRecords =
    getEidStream().map(eid -> getERecordFromId(eid))
                  .filter(eRecord -> eRecord.getEHash() != null &&  Status.DECLINED == eRecord.getStatus())
                  .map(ERecord::getEHash)
                  .collect(Collectors.toSet());
Eran
  • 387,369
  • 54
  • 702
  • 768
  • And to answer your question about casting `eid`, it's because you have defined a `Stream` without specifying the generic parameter `Stream`. – priyank-sriv May 24 '19 at 06:28
  • ok.. this makes sense. but I get compile error for eRecord.getEHash() and eRecord.getStatus() --- cannot resolve method and also ERecord::getEHash -- cannot access non static method as static – Roma May 24 '19 at 06:37
  • @Roma what kind of method is `getEHash`? Is it an instance method of `ERecord` or a static method of that class? The `eRecord.getEHash()` call in your question suggests it's an instance method. You can always replace it with a lambda expression if the method reference doesn't work (`eRecord -> eRecord.getEHash()`) – Eran May 24 '19 at 06:40
  • it is an instance method of ERecord. – Roma May 24 '19 at 06:46
  • @Roma Does the code in your question pass compilation? – Eran May 24 '19 at 06:47
  • @Roma does `getEidStream()` return a `Stream` or a raw `Stream`? Tat could be the problem. – Eran May 24 '19 at 06:48
  • @Eran, It does return a Stream – Roma May 24 '19 at 06:53
  • @Roma did you try the exact code posted in my answer? Assuming that the code in your question passed compilation, the code in my answer should work. – Eran May 24 '19 at 07:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193869/discussion-between-roma-and-eran). – Roma May 24 '19 at 07:06