This MOOC teaches you how to program core features and classes from the Java programming language that are used in Android, which is the dominant platform for developing and deploying mobile device apps.
In particular, this MOOC covers key Java programming language features that control the flow of execution through an app (such as Java’s various looping constructs and conditional statements), enable access to structured data (such as Java's built-in arrays and common classes in the Java Collections Framework, such as ArrayList and HashMap), group related operations and data into classes and interfaces (such as Java's primitive and user-defined types, fields, methods, generic parameters, and exceptions), customize the behavior of existing classes via inheritance and polymorphism (such as subclassing and overriding virtual methods). Learners will apply these Java features in the context of core Android components (such as Activities and basic UI elements) by applying common tools (such as Android Studio) needed to develop Java programs and useful Android apps.
Learners will work on several hands-on projects throughout the MOOC, i.e., each week will require learners to write solutions to programming assignments that reinforce the material covered in the lecture videos. There will be roughly 4-6 hours of student engagement time per week, including video lectures, quizzes, and programming assignments.

Dr. Douglas C. Schmidt

Transcript

[MUSIC] Welcome to the first part of our next lesson on structured data in Java which will expand your knowledge of the Java Collections Framework. After completing this part of the lesson, you will understand the purpose, importance, and benefits of this framework. You will also know how it creates a powerful package of useful and reusable data structures by leveraging various Java features such as interfaces and generic types. They are covered by upcoming modules in this MOOC. Finally, you will recognize the key interfaces defined in the Java collections framework that are implemented by classes we cover in the second part of this lesson. [MUSIC] The Java collections framework is a set of interfaces in classes that allow us to store a bunch of data in a single object, similar to how an array can hold a bunch of data for us. But the collection framework gives us so much more than what arrays can that they are now the go to mechanism for storage needs by most Java programmers. Even though arrays are great at storing a collection data for us, they have many limitations. The first limitation is that once an array is created, it's size is set and it cannot be changed, thus, once an array is full, we can not add anymore data to it unless we allocate an entirely new array and copy all the existing data over. One might be tempted to say that, well, I will always allocate an array that is bigger than what I might ever need. But then we have much unused and wasted space, if we only have a few data elements to store. Another limitation is that we have to manage exactly what all the data was going to be stored in the array. Choosing the index where a data items will be placed when added to the array and explicitly stating where data items would be moved if we had to shuffle data items around the array. Arrays also did not have many built-in functions that assisted us in managing the data that was store. And for some applications, arrays simply do not provide an efficient solution. Consider using an array to represent a DNA strand. If we ever cleaved a section of nucleotides from the middle of the strand, we would need to shift all the subsequent data down in the array to fill the vacated elements. And that can be a lot of work if our DNA stands were very long. To address these shortcomings, Java has what is known as the Java Collections Framework. Here, a Collection, with a capital C, can simply be thought of as a container that holds element of the same type in a single object. In that sense, a Collection is similar to an array, but it is how we view the collection and the elements it stores that can vary from how we view an array. We could have a container that holds a list of students that is ordered by their student ID. Or we could have a set of playing cards in a card game. Or we could have a group of name and phone numbers that allow us to map a name to it's corresponding number. Notice how these applications are addressed in terms of a list, a set, or a map. In the Collection Framework does a wonderful job in presenting a unified methodology for storing, retrieving and manipulating the data regardless of whether we consider the data as a list, a set, or a map. This collections framework has many benefits. First, and foremost, it provides us with several standard ways of storing data. This frees the programmer from having to write such support code in dealing with such low level issues, and thus it allows the programmer to concentrate on high level problem solving issues and program functionality. Second the size of the container is not static, but rather it can grow and shrink as needed while you're using it. And it is all done for you automatically. You can choose a container that is a better fit for the problem you are solving, thus resulting in more efficient code. And you can rest assured that the container that you use has been highly optimized to run quickly. And, as we will see later, the framework has it's foundation on a set of interfaces that share common methods. This helps in two ways. First, by raising the level of extraction so that we are not worried about implementation details. And second, it allows us to easily move to a different implementation and in later date, if we saw desire. And finally, anyone collection is very easy to learn. And your knowledge is applicable to other collections in the framework. To summarize, some programmers consider the collections framework to be the best part of Java since they're elegant, they're uniform, and they're flexible. These allow us, the Java programmer, to be much more productive when writing apps for execution on an Android device. Thus, they are very important for us to understand a bit about how they work and how to use them. This lesson will just cover the basics and we'll have two later lessons that will explore two containers in more detail. Before we get in to details, we need to define some terms, some of which we'll be discussed in more detail in a later module of this class. In Java, a collection is an object that will hold a group of other objects. Note, they only hold objects which precludes primitives types. But Java does provide what is known as wrapper classes for our primitive types. And Java defines a framework as a unified architecture for representing and manipulating collections. That simply means that we can treat most collections very similarly, even though they act very differently. Continuing with definitions, Java defines an interface as a set of method headers that do not contain implementations. Since there is no implementation of the methods, we call it an abstract class. It is useful for specifying what methods will be available, and how to call them, and what they will return, but not how they are actually implemented. And finally, a generic type is a class or an interface that is parameterized. In this case, the parameter is type information, which specifies the type of data that will actually be held in the container. This parameter will receive a tag when we create or instantiate an actual container object. Before continuing, let's talk for a moment about the importance of interfaces and generics in terms of the collections framework. When the various containers in the collections framework use the same interface, that means they all supply the same set of basic methods. They may also supply additional methods, but we know at a minimum that all the methods specified in the interface exist. This makes the classes uniform, and once you know how to use one, you know how to use the others as well. And more importantly, you can decide to use one of the other containers without changing your code that operates on that container. Additionally, this'll allow programmers who are working on different parts of a large system to share structures with each other, since they are all using the same common interface. Again, generics are important since they allow us to define a container in a type independent manner, while still allowing us to ensure type compatibility at compile time. We will see lots of examples of interfaces in generic classes as we move forward. So once again, a collection is an object that can store data for us. Or, it's what we also call a data structure. We will refer to the objects stored in the collection as elements. Now some collections maintain their elements in some type of order, while others are unordered. Some allow duplicate elements, while others do not. A few of the typical operations that we perform on a collection is to add a new element to the collection, to remove an element from the collection, to clear all elements from a collection, to search a collection to see if it contains a specific element, and to get the size of the collection. Example implementations found in Java include ArrayLists, LinkedLists, HashMaps, and TreeSets. And there are more. We will examine ArrayList and HashMaps more closely in later lectures. The Java collection framework is contained in the java.util package, and so to use it, you must include the statement import java.util.* near the top of your program. Now recall that our collections are generic types. Again, this allows the collection to be defined once independent of the type of data to be stored. But it also means that we are required to provide the type of data we're going to store when we construct or instantiate the collections object. The syntax for creating a collection object is to specify the collection name, which is immediately followed by the element type in angle brackets. You then give the object a name and initialize it by constructing the set collection via the new operation, which requires a set of parentheses. For example, we can specify that we want an array list of strings, and name it words, and then create the object by specifying that we want a new array list of strings with empty prints in a semicolon. After the object has been created, we can now call any of the methods from the collections interface on it. In the example code, we can see that we can add two strings to the array list called words. Finally, it is time for us to dive into the details just a bit but we will not cover everything in great detail. There is just not enough time for that in this lecture. So here is a view of the hierarchy of the interfaces in the Core Collections Framework which is very generic in nature and provides a standard interface to all operations that can be performed. At the top, we have the Collection interface which is just a collection of objects. This interface is often used in declarations and method signatures. So that any type of collection can be passed around. The set is a collection interface that can not take any duplicate elements. A sorted set is an ordered version of the set interface. A list is an ordered collection interface that allows duplicate elements and provides object retrieval based on an integer index. A queue follows the standard first in-first out principles and offers a variety of implementations. A map is in it's own hierarchy, since rather than dealing with a single element type, it supports a collection of key and value pairs, and we must specify the type of both. The design of the map does not allow duplicate keys. And finally, a sorted map is a map that maintains its keys in ascending order. The collection interface at the top of the hierarchy specifies that these methods must exist, an add method to add a new element to the collection. The contains method, which will search the collection to see if the parameter object is there. The remove method, which you guessed, removes an object from the collection if it is present. The isEmpty method, which will tell us if the collection is empty or not. The size method will tell us how many elements are in the collection and the iterator method will return an iterator over the elements in the collection. We'll discuss iterators later. Finally, if you plan to use any of the classes in the collections framework, it is important to learn all the methods in the collection interface. The set interface, which extends the collection interface provides for unordered collection of elements, which is not allowed duplicates. Thus, it basically models the mathematical concept of a set. It provides the same methods to find the collection interface with the added restriction that prevents duplicates. The list interface provides for an ordered sequence of elements. Because it is ordered, the user can access elements by their integer index. The user of this interface has precise control over where in the list an element is inserted. Lists, like job arrays, use zero base indexing. Besides providing all the methods of the collection interface, the list interface also provides methods to add or remove an element at a specific index. In which case, the other elements in the list will be shifted up to make room for the new element or shifted down to fill the vacated spot. You can set or get an element at a given index. You can search the index for an object from the front or the rear of the list, and you can get a special iterator that allows you to move over list in both directions. The map interface provides an object and maps keys to values. This interface is not associated with a collection interface, as it deals with pairs of elements rather than a single element type. A map cannot contain duplicate keys thus each key can map to at most one value. This is similar to how an array maps an index to a value, except now you can use any object as a key rather than a sequence of integers. Two important methods of a map allow you to add or change a key-value pair in the map and to retrieve a value for the map that is associated with a given key. Additionally, maps allow you to retrieve the set of all the keys or a collection of all the values contained in the map. This lecture gave you an introduction to the concept of the Collections Framework. Next, we will only to get into how they work and how to use them, we will cover that in the next lecture. [MUSIC] This part of the lesson expanded your knowledge of the Java Collections Framework. Now that you have completed the first part of this lesson, you should understand the purpose, importance, and benefits of this framework. You should also know this framework creates a powerful package of useful, reusable, data structures by leveraging various java features, such as interfaces and generic types, that are covered by upcoming modules in this MOOC. Finally, you should recognize the key interfaces in the Java collections framework that are implemented by classes we cover next in the second part of this lesson. [MUSIC]

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.