Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.

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.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
Java's Collection Framework
CS Collection and Input/Output Classes CS 3331 Fall 2009.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
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.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
1 Concrete collections II. 2 HashSet hash codes are used to organize elements in the collections, calculated from the state of an object –hash codes are.
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.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Maps Nick Mouriski.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Collections Dwight Deugo Nesa Matic
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
More constructors 1. Constructors  recall that a public constructor is what a client uses to create an object  the purpose of a constructor is to initialize.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Using the Java Collection Libraries COMP 103 # T2
Searching.
Efficiency of in Binary Trees
CSE 1020: The Collection Framework
Searching.
Hashing in java.util
Presentation transcript:

Set, TreeSet, TreeMap, Comparable, Comparator

Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added. Objects can be removed. It can be determined if an object is contained in the set. See java.util.set interface. As in the mathematical notion of a set, no duplicate values are permitted in a set.

The java.util package contains two classes for implementing binary search trees: TreeSet and TreeMap What is the primary difference between the adt List and the adt Set? There is no ordering in a set. That is, a class that implements the Set interface does not contain a method get(index).

TreeSet Java.util.TreeSet is a class that implements the interface Set. Some of the commonly used methods in TreeSet are: boolean add(Object obj); boolean remove(Object obj); boolean contains(Object obj); int size(); Iterator iterator(); Object[] toArray();

When TreeSet.remove( ) and TreeSet.add( ) the binary search tree ordering property is preserved. The TreeSet iterator performs inorder traversals of the tree, delivering values in ascending order. This class guarantees log (n) time cost for the basic operation remove(), add() and contains(). TreeSet contains a default constructor (with no arguments) to create an empty tree that will be sorted by the natural order of objects that are comparable. TreeSet contains a constructor with one argument, Comparator c, to create an empty tree that will be sorted by the compare( ) method specified in the Comparator object, c, passed.

What is the difference between Comparable and Comparator?

Comparable is an interface that contains one required method in any class that implements it: compareTo (Object o ). This method must return a negative integer, zero, or a positive integer when this object is less than, equal to, or greater than the specified object, o. The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0. It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Example: public int compareTo(Object o) { return ((City)o.getPop( ) - getPop( ); }

Note: Defining compareTo gives one way to compare objects of your class, allowing sorting into ascending order by such library methods as Arrays.sort. However, for more general sorting, such as in descending or or alphabetically, another technique must be used: implementation of the interface Comparator.

Comparator requires two methods of its implementer: compare( ) and equals( ). Usually the equals( ) method does not need to be coded, because you can rely on the equals( ) method inherited from Object. Example of a class that implements Comparator: public class ComparatorForCities implements Comparator{ public int compare (Object o1, Object o2){ String n1 = ((City) o1).getName(); String n2 = ((City) o2).getName(); return n1.compareToIgnoreCase(name2);} }

To use a comparator object, pass it to constructors or methods that take a Comparator argument, that is any class that implements the interface Comparator. Example code fragment: ComparatorForCities cn = new ComparatorForCities( ); City [ ] capitals; … Arrays.sort (capitals, cn); … int result = cn.compare(city1, city2);

Def: A Map is an adt that associates keys with their values and finds a value for a given key. Commonly used methods in java.util.Map are: Object put (Object key, Object value); //adds key and associated value to the map, returning any value previously associated with key or null. Object get (Object key); //returns value associated with key or null. Boolean containsKey (Object key); //returns true if key is associated with some value, otherwise returns false. Int size( ); //returns the number of associated key-value pairs in the map. Set keySet( ); //returns the set of keys in the map.

Java.util.TreeMap implements java.util.Map. TreeMap implements Map as a binary search tree ordered by keys and contains a default constructor, which requires the keys to be comparable, and a one argument (Comparator) constructor. TreeMap does not contain an iterator method, but keySet( ) may be used to get the set of keys and then iterate over them. Example: TreeMap map = new TreeMap( ); SomeType key; OtherType value; Set keys = map.keySet( ); Iterator iter = keys.iterator( ); while (itr.hasNext( )) { key = (SomeType)itr.next( ); value = (OtherType)map.get(key); …}

How is a TreeSet different from a TreeMap? TreeMap is more general than TreeSet. Although both implement binary search trees, TreeSet values are compared to each other, while in TreeMap no ordering is assumed for the values and the tree is arranged according to the order of the keys. TreeSet (according to the Java API) is backed by an instance of TreeMap, which means it has a TreeMap field in it that does all the work. It is often easier to use a TreeMap