sets and maps collections of data. advanced data structures often referred to as the java...
TRANSCRIPT
SETS AND MAPSSETS AND MAPSCollections of Data
Advanced Data StructuresAdvanced Data Structures
Often referred to as the Java Collections Framework….
• Set and map data types• Hash tables• Binary trees• Heap• Priority queue
We will only discuss Set and Map
SetsSets
• Unordered collection• Fundamental operations:
– Add an element– Remove an element– Containment test (is object in set?)– List elements (in arbitrary order)
• Reject duplicates
Set ImplementationSet Implementation
<<interface>>Set
HashSet TreeSet
add(E)addAll(Collection)clear()contains(Object)containsAll(Collection)equals(Object)hashCode()isEmpty()iterator()remove(Object)removeAll(Collection)retainAll(Collection)size()toArray()toArray(T[])
UML reminder: dotted line, open triangle pointing to interface
Simple SetDemoSimple SetDemo
import java.util.*;
public class SetDemo {
private Set<String> animals;
public SetDemo() {
animals = new HashSet<String>();
}
public void printAnimals(){
for (String animal : animals)
System.out.println(animal);
}
instantiate with specificimplementation.Advantage: could easilychange implementation,rest of program stays thesame.
Nodes are notnecessarily visitedin order inserted!
Add input and main methodsAdd input and main methodspublic void getAnimals() {
Scanner in = new Scanner(System.in);
String animal = "";
do {
System.out.print("Enter an animal or Q to quit: ");
animal = in.next();
if (!(animal.equalsIgnoreCase("Q")))
animals.add(animal);
} while (!(animal.equalsIgnoreCase("Q")));
}
public static void main(String[] args) {
SetDemo demo = new SetDemo();
demo.getAnimals();
demo.printAnimals();
}
}
can use methods from interface, such as add. Also have remove, contains, clear, size and more
TRY: giraffe, bear, coyote, lion
MapsMaps• A map data type keeps associations between
keys and values• Cannot contain duplicate keys• Operations include:
– put– get– containsKey/containsValue– keySet/values – return all keys/values– size
• Java has two implementations: HashMap and TreeMap.
Map DemoMap Demoimport java.util.*;
public class MapDemo {
Map<String, Integer> sightings;
public MapDemo() {
sightings = new HashMap<String, Integer>();
}
public void loadSightings() {
sightings.put("fox", 10);
sightings.put("bear", 2);
sightings.put("deer", 60);
sightings.put("elk", 30);
}
public void printSightings() {
Set<String> keySet = sightings.keySet();
for (String key : keySet)
System.out.println(key + "->" + sightings.get(key));
}
public static void main(String[] args) {
MapDemo demo = new MapDemo();
demo.loadSightings();
demo.printSightings();
}
}
TreeSet or HashSet?TreeSet or HashSet?• With a good hash function, hashing is usually faster• Balanced trees (remember those?) can guarantee
reasonable performance, HashSet depends entirely on performance of hash function
• To use TreeSet, objects being stored must implement Comparable interface
• For TreeMap, same requirement for keys• Can supply a Comparator object to TreeSet/TreeMap
constructor (takes two objects and returns comparison)• TreeSet is preferable if want to print list of items in order
TreeSet needs Comparable itemsTreeSet needs Comparable itemspublic class Student implements Comparable<Student>{
private String name;
public Student(String name) {
super();
this.name = name;
}
// Include setters and getters
public int compareTo(Student other) {
return name.compareTo(other.name);
}
}
TreeSet ExampleTreeSet Examplepublic class StudentSet {
private Set<Student> students;
public StudentSet() {
students = new
HashSet<Student>();
}
public void addStudent(Student s) {
students.add(s);
}
public void displayStudents() {
for (Student s : students) {
System.out.println(s.getName());
}
}
}
public static void main(String[] args) {
StudentSet set = new StudentSet();
set.addStudent(new Student("Cyndi"));
set.addStudent(new Student("Bill"));
set.addStudent(new Student("Jane"));
set.displayStudents();
}
}