Chapter 10 Generic Programming and Collection Classes | Introduction to Programming Using Java

Chapter 10  Generic Programming and Collection Classes | Introduction to Programming Using Java

 

 10  Generic Programming and Collection Classes

 

How to avoid reinventing the wheel? Many data structures and algorithms, such as those from Chapter 9, have been studied, programmed, and re-programmed by generations of computer science students. This is a valuable learning experience. Unfortunately, they have also been programmed and re-programmed by generations of working computer professionals, taking up time that could be devoted to new, more creative work.

 

Chapter 10  Generic Programming and Collection Classes | Introduction to Programming Using Java

 

A programmer who needs a list or a binary tree shouldn’t have to re-code these data structures from scratch. They are well-understood and have been programmed thousands of times before. The problem is how to make pre-written, robust data structures available to programmers. In this chapter, we’ll look at Java’s attempt to address this problem.

 

10.1  Generic Programming

 

Generic programming refers to writing code that will work for many types of data. We encountered the term in Section 7.3, where we looked at dynamic arrays of integers. The source code presented there for working with dynamic arrays of integers works only for data of type int.

But the source code for dynamic arrays of double, String, JButton, or any other type would be almost identical, except for the substitution of one type name for another. It seems silly to write essentially the same code over and over. As we saw in Subsection 7.3.3, Java goes some distance towards solving this problem by providing the ArrayList class. An ArrayList is essentially a dynamic array of values of type Object.

 

Chapter 10 Generic Programming and Collection Classes | Introduction to Programming Using Java

 

Since every class is a subclass of Object, objects of any type can be stored in an ArrayList. Java goes even further by providing “parameterized types,” which were introduced in Subsection 7.3.4. There we saw that the ArrayList type can be parameterized, as in “ArrayList<String>”, to limit the values that can be stored in the list to objects of a specified type. Parameterized types extend Java’s basic philosophy of type-safe programming to generic programming.

The ArrayList class is just one of several standard classes that are used for generic programming in Java. We will spend the next few sections looking at these classes and how they are used, and we’ll see that there are also generic methods and generic interfaces (see Subsection 5.7.1). All the classes and interfaces discussed in these sections are defined in the package java.

 

Chapter 10 Generic Programming and Collection Classes | Introduction to Programming Using Java

 

util, and you will need an import statement at the beginning of your program to get access to them. (Before you start putting “import java.util.*” at the beginning of every program, you should know that some things in java.util have names that are the same as

485

things in other packages. For example, both java.util.List and java.awt.List exist, so it is often better to import the individual classes that you need.)

In the final section of this chapter, we will see that it is possible to define new generic classes, interfaces, and methods. Until then, we will stick to using the generics that are predefined in Java’s standard library.

 

Chapter 10 Generic Programming and Collection Classes | Introduction to Programming Using Java

 

It is no easy task to design a library for generic programming. Java’s solution has many nice features but is certainly not the only possible approach. It is almost certainly not the best, and has a few features that in my opinion can only be called bizarre, but in the context of the overall design of Java, it might be close to optimal. To get some perspective on generic programming in general, it might be useful to look very briefly at generic programming in two other languages.

 

SEE MORE:

 

Leave a Comment