官术网_书友最值得收藏!

Working with collections versus working with streams

Everyone working with Java is aware of collections. We use collections in an imperative way: we tell the program how to do what it's supposed to do. Let's take the following example in which we instantiate a collection of 10 integers, from 1 to 10:

List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
list.add(i);
}

Now, we will create another collection in which we will filter in only the odd numbers:

List<Integer> odds = new ArrayList<Integer>();
for (int val : list)
{
if (val % 2 == 0)
odds.add(val);
}

At the end, we want to print the results:

for (int val : odds)
{
System.out.print(val);
}

As you can see, we wrote quite a bit of code to perform three basic operations: to create a collection of numbers, to filter the odd numbers, and then to print the results. Of course, we could do all the operations in only one loop, but what if we could do it without using a loop at all? After all, using a loop means we tell the program how to do its task. From Java 8 onwards, we have been able to use streams to do the same things in a single line of code:

IntStream
.range(0, 10)
.filter(i -> i % 2 == 0)
.forEach( System.out::print );

Streams are defined in the java.util.stream package, and are used to manage streams of objects on which functional-style operations can be performed. Streams are the functional correspondent of collections, and provide support for map-reduce operations.

We will further discuss streams and functional programming support in Java in later chapters.

主站蜘蛛池模板: 定边县| 恭城| 隆德县| 南投县| 满城县| 中超| 中江县| 兰州市| 八宿县| 大化| 仁化县| 马龙县| 澄城县| 鹤山市| 靖宇县| 夹江县| 徐州市| 安平县| 黔江区| 乐山市| 兴安盟| 宣恩县| 于田县| 阜阳市| 喜德县| 错那县| 锡林浩特市| 河源市| 桐柏县| 金门县| 财经| 榆林市| 黔西| 股票| 隆回县| 麻城市| 广丰县| 富锦市| 安平县| 石泉县| 肇庆市|