Logo

Programming-Idioms

History of Idiom 57 > diff from v44 to v45

Edit summary for version 45 by dscholl:
[Java] Updated to use streams

Version 44

2019-07-13, 14:58:52

Version 45

2019-09-26, 15:33:42

Idiom #57 Filter list

Create list y containing items from list x satisfying predicate p. Respect original ordering. Don't modify x in-place.

Idiom #57 Filter list

Create list y containing items from list x satisfying predicate p. Respect original ordering. Don't modify x in-place.

Code
List<T> y = new ArrayList<T>();
for (T v: x)
	if (p(v))
		y.add(v);
Code
var y = x.stream().filter(p).collect(Collectors.toList());
Comments bubble
Here p is just a method, not an object.
Origin
http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection
Origin
http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection