Big Java - Colorado School of Mines

Download Report

Transcript Big Java - Colorado School of Mines

Collections of Data
SETS AND MAPS
Advanced 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
Sets
• Unordered collection
• Fundamental operations:
–
–
–
–
Add an element
Remove an element
Containment test (is object in set?)
List elements (in arbitrary order)
• Reject duplicates
Set Implementation
<<interface>>
Set
HashSet
TreeSet
UML reminder: dotted line, open
triangle pointing to interface
add(E)
addAll(Collection)
clear()
contains(Object)
containsAll(Collection)
equals(Object)
hashCode()
isEmpty()
iterator()
remove(Object)
removeAll(Collection)
retainAll(Collection)
size()
toArray()
toArray(T[])
Simple SetDemo
import java.util.*;
public class SetDemo {
instantiate with specific
private Set<String> animals; implementation.
Advantage: could easily
change implementation,
rest of program stays the
same.
public SetDemo() {
animals = new HashSet<String>();
}
public void printAnimals(){
for (String animal : animals)
System.out.println(animal);
Nodes are not
}
necessarily visited
in order inserted!
Add input and main methods
can use methods from
public void getAnimals() {
interface, such as add. Also
Scanner in = new Scanner(System.in);
have remove, contains,
String animal = "";
clear, size and more
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();
}
}
TRY: giraffe, bear, coyote, lion
Maps
• 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 Demo
import 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?
• 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 items
public 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 Example
public 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();
}
}