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

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.

主站蜘蛛池模板: 红河县| 武功县| 睢宁县| 南郑县| 铜梁县| 泸州市| 仙桃市| 北票市| 大英县| 宜宾市| 陕西省| 东乡县| 安多县| 讷河市| 汕尾市| 周至县| 怀仁县| 花垣县| 静乐县| 庆安县| 新野县| 蒙山县| 象州县| 屏山县| 华亭县| 专栏| 茌平县| 海阳市| 莱阳市| 鱼台县| 将乐县| 井冈山市| 台湾省| 周至县| 莒南县| 襄城县| 朝阳县| 华亭县| 清水县| 桐柏县| 通道|