• MagicShel@programming.dev
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    1 month ago

    I try to prefer .findAny() over .findFirst() because it will perform better in some cases (it will have to resolve whether they are other matches and which one is actually first before they can terminate - more relevant for parallel streams I think. findAny short circuits that) but otherwise I like the first. I’d probably go with some sort of composed predicate for the second, to be able to easily add new criteria. But I could be over engineering.

    I mostly just posted because I think not enough people are aware of the reasons to use findAny as a default unless findFirst is needed.

    • magic_lobster_party@fedia.io
      link
      fedilink
      arrow-up
      5
      ·
      1 month ago

      For me I have the habit of doing findFirst because determinism is important where I work. But I agree with you if determinism is not of importance.