In today's lesson, we're going to talk about sets. There are many methods that Java uses to process lists and each one does a specific thing. So here's a list of the methods and their descriptions, and then some more methods and descriptions. Basically, you can create a list, insert elements, remove elements, sort them, find them based on their index, find them based on their values, find the size, find out how many items are in the list, and search for items in the list that contain certain values. Once again, these methods that we display here are used as a reference. Let's take a look at the difference between a set, a list and a map. First, a set is an unordered collection. One thing about a set is that they're all unique objects, which means that you can have no duplicates. A list orders the collection, and they're put in insertion order. Now we've come across lists before when we talked about ArrayLists and LinkedLists and there's also a Vector class. A map provides a key value, sort of like an index where we can query that index and find the values. A HashSet is a general purpose set. And just like a regular set, we can have a LinkedHashSet and a TreeSet. We're going to see these in programs coming up. For lists, there are ArrayLists and LinkedLists and as we said, a Vector list, and there are the definitions of those. How do we know which ones to use? Well, you should use a list if you need to access elements frequently using the index. The ArrayList provides faster access with an index or if you wanna store elements and you need them in the order in which they're inserted. You should use a set if you wanna create a collection of unique elements and you don't want any duplicates. TreeSet is assorted set. LinkedHashSet also maintains the insertion order. And you should use a map if you store data in the form of a key and value, and you can pick a HashTable or a HashMap. Here's a program that illustrates a set. In this program, I've decided to create two different sets. One for the retired numbers of New York Yankee players. The second is the names of the players who have their numbers retired. As you can see on the top left-hand side of the screen, I bring in the java.util library and I create an array of all of the retired numbers, and I put them in numerical order. You'll notice that there are two circles, one for the number eight and one for the number 42. Now you may not be a baseball fan, but the number eight was retired twice by the Yankees, once for Bill Dickey and once for Yogi Berra. And the number 42 was retired once by major league baseball for Jackie Robinson and once for the Yankees for Mariano Rivera. Now we mentioned that a set does not allow duplicates. So let's watch what happens. We create in lines 21 and 22, we create two sets. One of the integers of retired numbers and one a string of the players. In line 24 I use a try-catch block to make sure that I'm not inserting any duplicates. And if I do catch that error and throw an exception that doesn't abort the program. I create a loop that says let's start at zero and go through number 22. So there are 23 retired numbers for the Yankees. And I add each of the retired numbers to the set. Then I add each of the players to set two. Lines 30 through 32 print out the unsorted list. And as you can see, the numbers are in somewhat random order. It starts with 32 and then 1, 2, 3, 4, 5. Then it goes to 37, 6, 7, 8, 9, 10. Only one 42, only one eight appear in the unsorted list of numbers. Those numbers, because they are duplicates, were not inserted into the list. So line 31 prints out the unsorted list of numbers. Line 32 prints out the unsorted list of names. In line 34, I sort the numbers and I create a TreeSet called sorted numbers. And then I print out the entire New York Yankees retired numbers in the correct order. That happens in line 36. I can also call upon the first and last numbers in the list. And it tells me that the first number in the list is number one and the last number in the list is number 51. Finally, in the program, I have a catch statement that matches up with my try to catch any errors, especially the ones where I'm trying to insert a duplicate number. So now you know that a set is a collection that contains no duplicate elements. In our next lesson, we'll talk about how a list does similar work.