Sets Set is an unordered list of items

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
15-Jun-15 Lists in Java Part of the Collections Framework.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java's Collection Framework
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CompSci 100E Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Big Java Chapter 16.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
CompSci 100e2.1 Today’s Topics l Reading:  Sedgewick & Wayne: , l Topics  APTs  Basic Collections: ArrayLists: the expandable array.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS2005 Week 7 Lectures Set Abstract Data Type.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
Jeff Forbes Owen Astrachan September 8, 2017
OOP Tirgul 5.
Using the Java Collection Libraries COMP 103 # T2
Java Arrays and ArrayLists COMP T1 #5
Interfaces in Java’s Collection Framework
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Java Collections Overview
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
TCSS 342, Winter 2006 Lecture Notes
Introduction to Collections
Introduction to Collections
Collections A First Glimpse.
Part of the Collections Framework
Collections James Brucker.
CS2110: Software Development Methods
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
slides created by Marty Stepp
Collections A First Glimpse 16-Apr-19.
Data Structures II AP Computer Science
Web Design & Development Lecture 6
Part of the Collections Framework
Java Basics – Arrays Should be a very familiar idea
Presentation transcript:

Sets Set is an unordered list of items Items are unique! Only one copy of each item in set! We will use two different implementations of sets TreeSet A TreeSet is backed up by a tree structure (future topic) Keeps items sorted (+) Slower than HashSets (-) HashSet A HashSet is backed up by a hashing scheme (future topic) Items not sorted – should seem to be in random order (-) Faster than TreeSets (+)

Sets Basic Operations: Look at API ! Create a set Add an item to a set Check if item is in a set Is set empty? Remove item from set Look at API ! What other goodies are there?

Example – Create and add to Set TreeSet<String> firstnames = new TreeSet<String>(); firstnames.add("John"); firstnames.add("Emily"); firstnames.add("Alex"); firstnames.add("Mike"); Alex Mike John Emily

Example – Is object in set? if (firstnames.contains("Zed")) System.out.println("Zed is in the set."); else System.out.println("Zed is not in the set."); if (firstnames.contains("Mike")) System.out.println("Mike is in the set."); System.out.println("Mike is not in the set.");

Iterator – Look at each element in a Set Can get an iterator to look at each element in the set (as covered recently) May not know the order of the elements Guaranteed to give you all of the elements in the set – one at a time // You must first get an iterator for set Iterator<String> nameIter = firstnames.iterator(); // Print elements in set while (nameIter.hasNext()) { String name = nameIter.next(); System.out.println(name); }

Iterate over elements in Set firstnames With for-each loop, iterator is automatically invoked for you! // Print elements in set for (String name: firstnames) { System.out.println(name); }

Other Basic Operations on Sets size() – returns size of set System.out.println("Size of set is " + firstnames.size()); remove(object) – remove object from set if there isEmpty() – return true if set is empty See “Sets” and “Iterator” on Java API page Note that Set is an interface, not a class

Set Operations Union of two sets Intersection of two sets all the elements from both sets Intersection of two sets the elements that are in both sets Difference of two sets (A – B) the elements in A that are not in B A B x

How do we? Implement set operations for two sets Union, intersection, difference Implement set operations for array of sets Union, intersection

Sets You should have discovered by looking up the Set API: Almost everything has been done for you! Union of two sets – Use:  boolean addAll(Collection c) Intersection of two sets – Use: boolean retainAll(Collection c) Difference of two sets (A – B) – Use: boolean removeAll(Collection c)

Collections: ArrayList vs Set directly access an item keep items ordered (as entered) Can have duplicates of items What are operations on an ArrayList? Sets Keeps items unordered (Unless using TreeSet: sorted) No duplicate items Easily remove duplicates What are operations on a Set?

Using Both ArrayList and Sets You may want to use a set to get rid of duplicates, then put the items in an ArrayList and sort them! Problem: Often data comes in the form of an array How do we go from array to ArrayList or TreeSet? Often we are required to return an array How do we go from a Collection such as an ArrayList or TreeSet to an array? Can do it the “hard” way with loops or iterators: one item at a time OR:

Converting from array to Collection For arrays of objects (such as Strings) use the asList method in the Arrays class. This returns a fixed-size list backed by the specified array Pass this into the constructor of your ArrayList or set Example String[] words = String[N]; . . . TreeSet<String> wordset = new TreeSet<String>(Arrays.asList(words));

Converting from Collection to array Collections such as ArrayLists and TreeSets have a toArray method This returns an array Syntax a bit awkward Example TreeSet<String> wordset = new TreeSet<String>(); . . . String[] words = (String[]) wordset.toArray(new String[0]); or return (String[]) wordset.toArray(new String[0]);