Chapter 12

Using Collections and Streams (When Arrays Aren't Good Enough)


check Facing the limitations of arrays

check Dealing with a bunch of objects at once

check Using Java's cool functional programming features

check 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 one of the vacant rooms.

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.

Understanding the Limitations of Arrays

Arrays are nice, but they have some serious 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 ...

Get Java For Dummies, 7th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.