Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data structures and algorithms in the collection framework 1.

Similar presentations


Presentation on theme: "Data structures and algorithms in the collection framework 1."— Presentation transcript:

1 Data structures and algorithms in the collection framework 1

2 2 Elements of the collection framework Interfaces: Abstract data types –Specifications Classes: Data types –Implementations Exceptions: A few exceptions Algorithms –Working on the abstract data types.

3 Data structures and algorithms in the collection framework 3 Interfaces Collection –Methods common to all collections (except map) List –An ordered collection. Set –No duplicate elements. –equals(Object obj) method on the elements is important. Queue –Normally implemented as FIFO (first-in-first-out) –New in Java 5.0 SortedSet –Set that guarantees that elements are traversed in order Map –Contains pairs (key, value) –Values are retrieved by the key: value = map.get(key) SortedMap –Map that guarantees that elements are sorted according to the key.

4 Data structures and algorithms in the collection framework 4 Object ordering SortedSet and SortedMap rely on object ordering. –The elements in SortedSet (keys in SortedMap) must implement the Comparable interface public interface Comparable { public int compareTo(Object o); } –Or, you must specify a Comparator when constructing the Sorted collection public interface Comparator { int compare(Object o1, Object o2); }

5 Data structures and algorithms in the collection framework 5 Some implementations List –LinkedList, ArrayList –Vector (old implementation) Set –HashSet, TreeSet (TreeSet is an OrderedSet) Queue –LinkedList, PriorityQueue Map –HashMap, TreeMap (TreeMap is an OrderedMap) –Hashtable (old implementation) The above shows only a few implementations. More (special purpose) implementations exist in Java 5.0.

6 Data structures and algorithms in the collection framework 6 Implementations overview General purpose implementations Interfaces Hash table Resizable array Balanced tree Linked List SetHashSetTreeSet ListArrayListLinkedList MapHashMapTreeMap

7 Data structures and algorithms in the collection framework 7 List implementations LinkedList –Performance: Delete operation is fast O(1) if you are iterating through the List ArrayList –Performance: Good get(index)O(1) –Generally faster than LinkedList –Tuning parameter: Initial capacity of array. Set in constructor. Vector –Old implementation (before the collection framework was introduced in Java) –Has been retrofitted to suite the collection framework. –Synchronized

8 Data structures and algorithms in the collection framework 8 Set and Map implementations HashSet –Is fastest –Tuning parameter: Initial capacity Set in constructor TreeSet –Offers ordering HashMap –Fastest –Tuning parameter: Initial capacity Set in constructor TreeMap –Offers ordering by key

9 Data structures and algorithms in the collection framework 9 Queue implementations LinkedList –implements the Queue interface PriorityQueue –Orders elements according to the natural order (Comparable) or a specific Comparator. –Head of queue is the “smallest” element. –Internal data structure is a “heap”.

10 Data structures and algorithms in the collection framework 10 Iterator You want to access the individual data items in your collections –List: no problem get(int index) –Collection and Set has no order Get-by-index does not make sense –You access the data items using an iterator Collection set = new HashSet (); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Integer i = iterator.next(); doSomething(i); }

11 Data structures and algorithms in the collection framework 11 Iterator pattern Iterator is an interface –You don’t need to know the name the implementing class, since you will never need “new SomeIterator()”. Design pattern: Iterator –Iterator is such a general phenomenon that is has been termed a “design pattern” –Collection creates an iterator (known only by interface) –The iterator fetches the individual objects from the collection. –You may use the iterator pattern with you own classes Idea: BorrowerCatalog creates a BorrowerIterator

12 Data structures and algorithms in the collection framework 12 For-each loop in Java 5.0 –List myList = new ArrayList (); –for (String str : myList) { doSomething(str); } –You probably don’t need to use iterators very often. –Works with any class implementing interface java.lang.Iterable. Your collection/catalog classes might implement Iterable. Works with arrays as well –int[] numbers = new int[10]; –for (int number : numbers) { doSomething(number); } For-each loop is just syntactic sugar –It adds nothing new to the Java language, it just makes things a little easier for the programmer (and program reader).

13 Data structures and algorithms in the collection framework 13 Iterating a map The general Map interface cannot create an iterator. If you want to iterate a map, you must get a set of keys, and then iterate the keys Set keys = map.keySet(); for (int key : keys) { System.out.println(key + ": " + map.get(key)); } Or use the entry set of the map For (Map.Entry entry : map.entrySet()) { System.out.println(“entry.getKey + “: “ + entry.getValue(); }


Download ppt "Data structures and algorithms in the collection framework 1."

Similar presentations


Ads by Google