- Design Patterns and Best Practices in Java
- Kamalmeet Singh Adrian Ianculescu LUCIAN PAUL TORJE
- 255字
- 2021-06-25 20:52:28
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.
- Mastering Concurrency Programming with Java 8
- Java范例大全
- 零基礎搭建量化投資系統(tǒng):以Python為工具
- Learning SAP Analytics Cloud
- Java Web程序設計
- Java性能權威指南(第2版)
- Python編程與幾何圖形
- Keras深度學習實戰(zhàn)
- 基于SpringBoot實現(xiàn):Java分布式中間件開發(fā)入門與實戰(zhàn)
- Spring+Spring MVC+MyBatis從零開始學
- Raspberry Pi Robotic Blueprints
- Solutions Architect's Handbook
- 奔跑吧 Linux內核
- Mastering PowerCLI
- XML程序設計(第二版)