O'Reilly logo

Java For Dummies, 7th Edition by Barry A. Burd

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 12

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

IN THIS CHAPTER

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required