Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's wrong with generics here?

  ListCondi<ObjType> myValue = gateway.callThatApi(...)

  foreach(tmp as Condi in myVlue)
  { 
    if  (tmp.worked) { foo(tmp.Generic);}
    else {bar(tmp.Generic);}
  }
PS: Sorry, have not touched java in like 10 years, but I assume you can have that foreach as a map.


In my case above, there's just one object, maybe (or maybe there isn't?).

In the case you're presenting, one might consider:

  Stream<Foo> myVals = gateway.callThatApi(...)
  return myVals.forEach(val -> getFooOrBar(val).apply(val))
Not quite as nice as Scala would let me do it, but closer, simpler.

Streams also let you add filters to the "list" of values, reduce them to a single value with a reducer, or use a nice set of "collectors" that reduce to Java generic collection types. (Collectors.groupBy is so handy).

Streams are also lazy, which I think is more good than bad.

The thing I tell everyone to do right now, today, is to open their Java code and search for "return null", then replace it with "return Optional.empty()". It'll break your code in lots of places, but things will be better when you fix all those things. Often you'll find lots of places that didn't handle the null possiblity at all!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: