Chapter 12
Using Collections and Streams (When Arrays Aren't Good Enough)
IN THIS CHAPTER
Facing the limitations of arrays
Dealing with a bunch of objects at once
Using Java's cool functional programming features
Developing code for multicore processors
Chapter 11 is about arrays. With an array, you can manage a bunch of things all at once. In a hotel management program, you can keep track of all the rooms. You can quickly find the number of people in a room or find a vacant room.
However, arrays don’t always fit the bill. In this chapter, you find out where arrays fall short and how collections can save the day.
Arrays Have Limitations
Imagine that you store customer names in some predetermined order. Your code contains an array, and the array has space for 100 names:
String[] name = new String[100];for (int i = 0; i < 100; i++) { name[i] = new String();}
All is well until, one day, customer number 101 shows up. As your program runs, you enter data for customer 101, hoping desperately that the array with 100 components can expand to fit your growing needs.
No such luck. Arrays ...
Get Java For Dummies, 8th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.