Handout 3.4 - Collections- The Set ADT.pdf

Handout 3.4 - Collections- The Set ADT.pdf

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Kimani Njoroge. Collections: The Set ADT Page 1 The Set ADT Consider the Set ADT: A set is an abstract data type that does not allow duplicate elements to be added. That is, there cannot be two elements e 1 and e 2 such that e 1 .equals(e 2 ). Any attempt to add duplicate elements is ignored. Sets differ from Lists in that the elements are not kept in the same order as when they were added. Sets are generally unordered, which means that the particular location of an element may change according to the particular set implementation. Typical operations for Sets are: • add(Object x) • remove(Object x) These operations work similar to that of Lists with the exception that the elements are not necessarily maintained in the same order that they were added in. Also, items are not guaranteed to be added to the Set because the add() operation will not allow any duplicate items to be added. Example: Suppose we wanted to maintain a list of DVD titles in our personal movie collection. Likely we do not want to have two or more of the same DVD. We can use a Set to avoid duplicates. In JAVA, we use the HashSet class which is in java.util package . Consider a simple Movie class as follows: public class Movie { private String title; public Movie(String t) { title = t; } public String getTitle() { return title; } public String toString() { return "Movie: \"" + title + "\""; } } Now consider the following code which simulates some inventory at a video store. The code makes use of the simple Movie object by adding 10 movies from among 5 unique titles … hence many duplicates. The code makes use of Math.random() so that the inventory is different each time we run the program. import java.util.*; public class SetTestProgram1 { public static void main(String[] args) { Movie[] dvds = { new Movie("Bolt"), new Movie("Monsters Vs. Aliens"), new Movie("Marley & Me"), new Movie("Hotel For Dogs"), new Movie("The Day the Earth Stood Still")};
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Kimani Njoroge. Collections: The Set ADT Page 2 ArrayList<Movie> inventory = new ArrayList<Movie>(); // Add 10 random movies from the list of dvds for (int i=0; i<10; i++) { inventory.add(dvds[(int)(Math.random()*5)]); } for (Movie m: inventory) System.out.println(m); } } Of course, each time that we run this code the result is different. Here is an example of what we may see: Movie: "Bolt" Movie: "The Day the Earth Stood Still" Movie: "Bolt" Movie: "Marley & Me" Movie: "Monsters Vs. Aliens" Movie: "The Day the Earth Stood Still" Movie: "The Day the Earth Stood Still" Movie: "Bolt" Movie: "Bolt" Movie: "Bolt" Can we adjust the for loop so that it only displays unique movies? No. We would have to do some extra work of making a new list with the duplicates removed. So, we could replace the for loop with the following: ArrayList<Movie> uniqueList = new ArrayList<Movie>(); for (Movie m: inventory) { if (!uniqueList.contains(m)) uniqueList.add(m); } for (Movie m: uniqueList) { System.out.println(m); } This would produce the following output (according to the earlier results): Movie: "Bolt" Movie: "The Day the Earth Stood Still" Movie: "Marley & Me" Movie: "Monsters Vs. Aliens" However, there is an easier way to do this. Consider what happens if we change the inventory from an
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern