Chapter 12

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

In This Chapter

arrow Facing the limitations of arrays

arrow Dealing with a bunch of objects at once

arrow Using Java's cool functional programming features

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

Get Java For Dummies, 6th 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.