Java Collections Framework – II Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/Books/TIJ4 Gilad Bracha, “Generics in the Java Programming Language” a.k.a “The Generics Tutorial”, Julho de 2004 cf. http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf [email protected] José Valente de Oliveira 20-1 JCF interface hierarchy 1 Interface List and its implementations Interface Queue 2 Queue [email protected] http://w3.ualg.pt/~jvo/ensino.php 20-5 Queue – implementing with an Array [email protected] http://w3.ualg.pt/~jvo/ensino.php 20-6 3 Queue – implementing with an Array [email protected] http://w3.ualg.pt/~jvo/ensino.php 20-7 Interface Map 4 Interface Map interface Map<K, V> { void clear(); // optional boolean containsKey(Object key); boolean containsValue(Object value); Set<Map.Entry<K,V>> entrySet(); boolean equals(Object o); V get(Object key); int hashCode(); boolean isEmpty(); Set<K> keySet() ; V put(K key, V value); void putAll(Map<? extends K,? extends V> m); V remove(Object key) ; int size(); Collection<V> values(); } [email protected] José Valente de Oliveira 20-9 Interface SortedMap interface SortedMap<K, V> extends Map<K, V> { Comparator<? super K> comparator(); Set<Map.Entry<K,V>> entrySet(); K firstKey(); SortedMap<K,V> headMap(K toKey); Set<K> keySet(); K lastKey(); SortedMap<K,V> subMap(K fromKey, K toKey); SortedMap<K,V> tailMap(K fromKey); Collection<V> values(); } [email protected] José Valente de Oliveira 20-10 5 How to sellect a collection? Com Com chave? chave Admite duplicados ? N N Set Collection {abstract} Chave inteira? Com chave? S List S N [email protected] Map 20-11 José Valente de Oliveira How to sellect a collection? Com Com chave? chave Admite duplicados ? S Bag N N Set Collection {abstract} Chave inteira? Com chave? S List S N [email protected] José Valente de Oliveira Map 20-12 6 Implementations Underlying data strucuture Interfaces Hash table Set Resizable array HashSet List Tree Linked list TreeSet ArrayList [email protected] LinkedHashSet LinkedList Queue Map Hash table + Linked list LinkedList HashMap TreeMap José Valente de Oliveira LinkedHashMap 20-13 Example 8: 2 of 5 code reader [email protected] To generate the bar code each bit equals to 1 is converted into a wide-bar, while each bit equals to 0 is converted into a narrow bar, a space being inserted between bars. José Valente de Oliveira 20-14 7 Example 8: 2 of 5 code reader Now, suppose that each wide bar is represented by 3 consecutive ones (111) and that each narrow bar is represented by a single one (1), the space being represented by a 0. With this bar representation, the decimal character 1, which is coded by the 5-bits 10001, has the bar representation 1110101010111. Write a program that reads a sequence of 0s and 1s in the 2 of 5 barcode format and decode it, i.e., answer the represented decimal characters. Input A string of 0s and 1s in the 2 of 5 code format with a size not exceeding 200. Output The decoded decimal characters Sample input 111010101011101110111010101 [email protected] José Valente de Oliveira Sample output 13 20-15 Example 8: 2 of 5 code reader Sample input 111 1 1 1 111 1 0 0 0 1 Sample output 1 [email protected] 111 111 1 1 1 1 1 0 0 0 3 José Valente de Oliveira 20-16 8 class CodeReader { private static Map<String, String> codes = new HashMap<String,String>(); static { codes.put("00110", "0"); codes.put("10001", "1"); codes.put("01001", "2"); codes.put("11000", "3"); codes.put("00101", "4"); codes.put("10100", "5"); codes.put("01100", "6"); codes.put("00011", "7"); codes.put("10010", "8"); codes.put("01010", "9"); } [email protected] José Valente de Oliveira 20-17 public String decimal(String s) { String[] nospaces = s.split("0"); String result = ""; String code = ""; for(int i = 0; i < nospaces.length; i++) { if (nospaces[i].compareTo("1")==0) code+="0"; else if (nospaces[i].compareTo("111")==0) code+="1"; if ((i+1)%5 ==0) { result+=codes.get(code); code=""; } } return result; } } // end of class [email protected] José Valente de Oliveira 20-18 9 public class Main { public static void main(String[] args) { Scanner sc = new Scanner (System.in); CodeReader cr = new CodeReader(); System.out.println(cr.decimal(sc.next())); } } ¾java Main <ret> 111010101011101110111010101 13 ¾ [email protected] José Valente de Oliveira 20-19 Punch list summary Core interfaces and implementations (continued) List Queue Map Map, application example [email protected] José Valente de Oliveira 20-20 10