Stop java stream computations based on previous computation results -
how break stream computation based on previous results? if it's obvious stream.filter(...).count() less number - how stop stream computation?
i have following code checks if sampledata passes predicate test:
// sampledata.size() may greater 10.000.000 set<string> sampledata = downloadfromweb(); return sampledata.stream().filter(predicate::test).count() > sampledata.size() * coefficient; i have thousands of sampledata. problem code ineffective. example, if coefficient equals 0.5, sampledata.size() = 10_000_000, , first 5_000_000 elements fails predicate::test - there no reason validate last 5_000_000 elements (count() never greater 5_000_000).
zhekakozlov’s answer heading right direction, lacks negation. matches larger threshold, number of non matching elements must smaller “size - threshold”. if test nonmatching elements smaller, can apply limit stop once become larger:
set<string> sampledata = downloadfromweb(); final long threshold = sampledata.size()-(long)(sampledata.size() * coefficient); return sampledata.stream() .filter(predicate.negate()).limit(threshold+1).count() < threshold; there is, way, no reason create method reference test method of existing predicate predicate::test. pass predicate filter method. code above uses predicate.negate() instead of predicate.negate()::test…
Comments
Post a Comment