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 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 > Set HashSetTreeSet 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 SetDemo import java.util.*; public class SetDemo { private Set animals; public SetDemo() { animals = new HashSet (); } public void printAnimals(){ for (String animal : animals) System.out.println(animal); } instantiate with specific implementation. Advantage: could easily change implementation, rest of program stays the same. Nodes are not necessarily visited in order inserted!
Add input and main methods public 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
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 sightings; public MapDemo() { sightings = new HashMap (); } public void loadSightings() { sightings.put("fox", 10); sightings.put("bear", 2); sightings.put("deer", 60); sightings.put("elk", 30); } public void printSightings() { Set 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 { 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 students; public StudentSet() { students = new HashSet (); } 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(); }