JAVA SETS AND MAPS EXPERIMENTAL ANALYSIS EXPERIMENTAL ANALYSIS

  • Slides: 12
Download presentation
JAVA SETS AND MAPS

JAVA SETS AND MAPS

EXPERIMENTAL ANALYSIS •

EXPERIMENTAL ANALYSIS •

EXPERIMENTAL ANALYSIS MAP ADT IMPLEMENTATIONS 1, 00 E+01 1, 00 E+00 1, 00 E-01

EXPERIMENTAL ANALYSIS MAP ADT IMPLEMENTATIONS 1, 00 E+01 1, 00 E+00 1, 00 E-01 2 8 32 128 512 2048 8192 32768 131072 524288 2097152 Time 1, 00 E-02 1, 00 E-03 1, 00 E-04 1, 00 E-05 1, 00 E-06 1, 00 E-07 1, 00 E-08 Size Unsorted. Table. Map Probe. Hash. Map Chain. Hash. Map Java. Hash. Map

EXPERIMENTAL ANALYSIS SORTED MAP ADT IMPLEMENTATIONS 1, 00 E+00 2 8 32 128 512

EXPERIMENTAL ANALYSIS SORTED MAP ADT IMPLEMENTATIONS 1, 00 E+00 2 8 32 128 512 2048 8192 1, 00 E-01 Time 1, 00 E-02 1, 00 E-03 1, 00 E-04 1, 00 E-05 1, 00 E-06 1, 00 E-07 Size Sorted. Table. Map Tree. Map Java. Tree. Map 32768 131072 524288

SUMMARY OF CLASSES (SETS) • Enum. Set<E> - Specialized set for enumerated types. Implemented

SUMMARY OF CLASSES (SETS) • Enum. Set<E> - Specialized set for enumerated types. Implemented as bitvector. • Hash. Set<E> - Set implemented with chained hash table – no guaranteed iteration order • • Linked. Hash. Set<E> - Same, but with guaranteed iteration order. No complexity overhead E should override both hash. Code() and equals()! Default hash. Code() hashes memory address of object (typically, not always) and default equals() compares memory address • Tree. Set<E> - Set implemeneted with red-black tree. Needs Comparator<E> or E should implement Comparable<E> • There are no multisets in the Java library. Some external libraries have them. • To find how to use them, go to the Java API!

Interfaces Classes Iterable<E> Collection<E> Object Set<E> Abstract. Collection<E> Sorted. Set<E> Abstract. Set<E> Navigable. Set<E>

Interfaces Classes Iterable<E> Collection<E> Object Set<E> Abstract. Collection<E> Sorted. Set<E> Abstract. Set<E> Navigable. Set<E> Enum. Set<E> Hash. Set<E> Linked. Hash. Set<E> Tree. Set<E>

EXAMPLE OF USING SET<E> 1. Scanner s = new Scanner(new File(“numbers. txt”)); 2. Hash.

EXAMPLE OF USING SET<E> 1. Scanner s = new Scanner(new File(“numbers. txt”)); 2. Hash. Set<Integer> numbers = new Hash. Set<>(); 3. while(s. has. Next. Int()) 4. numbers. add(s. next. Int()); 5. …elsewhere… 6. int sum. Of. Unique = 0; 7. for(Integer i : numbers) 8. sum += i;

EXAMPLE OF OVERRIDING HASCODE() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

EXAMPLE OF OVERRIDING HASCODE() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Really great stackoverflow answer public class Student { about creating hash codes. String id; //unique! String name; List<Courses> courses; …stuff… public boolean equals(Object obj) { if(obj != null && obj instanceof Student) return id. equals(((Student)obj). id); return false; } public int hash. Code() { return id. hash. Code(); } }

SUMMARY OF CLASSES (MAPS) • Enum. Map<K, V> - Specialized set for enumerated types.

SUMMARY OF CLASSES (MAPS) • Enum. Map<K, V> - Specialized set for enumerated types. Implemented as bitvector. • Hash. Map<K, V> - Set implemented with chained hash table – no guaranteed iteration order • • Linked. Hash. Map<K, V> - Same, but with guaranteed iteration order. No complexity overhead K should override both hash. Code() and equals()! Default hash. Code() hashes memory address of object (typically, not always) and default equals() compares memory address • Tree. Map<K, V> - Set implemeneted with red-black tree. Needs Comparator<K> or K should implement Comparable<K> • Identity. Hash. Map<K, V> - hash map specifically for objects that use reference equality instead of object equality • Weak. Hash. Map<K, V> - Like a hash map, but does not prevent garbage collector from removing keys • There are no multisets in the Java library. Can fake with Map<K,

Interfaces Classes Map<K, V> Object Sorted. Map<E> Abstract. Map<K, V> Navigable. Map<E> Enum. Map<K,

Interfaces Classes Map<K, V> Object Sorted. Map<E> Abstract. Map<K, V> Navigable. Map<E> Enum. Map<K, V> Hash. Map<K, V> Tree. Map<K, V> Linked. Hash. Map<K, V> Identity. Hash. Map<K, V> Weak. Hash. Map<K, V>

EXAMPLE OF USING MAP<K, V> 1. Scanner s = new Scanner(new File(“numbers. txt”)); 2.

EXAMPLE OF USING MAP<K, V> 1. Scanner s = new Scanner(new File(“numbers. txt”)); 2. Hash. Map<Integer, String> numbers = new Hash. Map<>(); 3. while(s. has. Next. Int()) 4. numbers. put(s. next. Int(), s. next()); 5. …elsewhere… 6. System. out. println(numbers. get(5)); //get a value

PROBLEM • Design a barcode scanner for a super market • You are given

PROBLEM • Design a barcode scanner for a super market • You are given an inventory (item barcode, per unit cost, and quantity per item) of the store (file online) • Let a customer come with a shopping list (you design file format) • • Look up in the inventory database for each items availability If possible to purchase subtract the quantity from the inventory If the item is purchased, add the price to the total Print a receipt for the purchase • What will be the underlying container for your database?