Содержание
- 2. Agenda Data Structure What is JCF? The Collection Interfaces Collections Implementations Ordering and Sorting The Legacy
- 3. Lecture Objectives To understand the concepts of Java collections Framework To be able to implement Java
- 4. Store of Data Structure Arrays - a linear data structure and it's mainly used to store
- 5. Store of Data Structure (cont.) Linked list is a data structure consisting of a group of
- 6. Store of Data Structure (cont.) If each node has a reference to the next and previous
- 7. Store of Data Structure (cont.) Binary tree is a tree data structure in which each node
- 8. Store of Data Structure (cont.) A hash table, or a hash map, is a data structure
- 9. The Limitation of Arrays An array is a very useful type in Java but it has
- 10. What is a Collections Framework? A unified architecture for representing and manipulating collections. Includes: – Interfaces:
- 11. Hierarchy of interfaces
- 12. Exception Conventions UnsupportedOperationException ClassCastException IllegalArgumentException IllegalStateException NoSuchElementException NullPointerException IndexOutOfBoundsException
- 13. Iterators Iterator is an object that enables a programmer to traverse a container, particularly lists. public
- 14. Collection public interface Collection extends Iterable { // Basic operations int size(); boolean isEmpty(); boolean contains(Object
- 15. Set Set — a collection that cannot contain duplicate elements This interface models the mathematical set
- 16. Set (methods) public interface Set extends Collection { // Basic operations int size(); boolean isEmpty(); boolean
- 17. List List — an ordered collection (sometimes called a sequence) Lists can contain duplicate elements
- 18. List (methods) public interface List extends Collection { // Positional access E get(int index); E set(int
- 19. ListIterators A ListIterator extends Iterator to treat the collection as a list, allowing – access to
- 20. ListIterator (methods) public interface ListIterator extends Iterator { boolean hasNext(); E next(); boolean hasPrevious(); E previous();
- 21. ListIterator ListIterator it = list.listIterator(list.size()); while (it.hasPrevious()) { String obj = it.previous(); // … use obj
- 22. Queue Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection
- 23. Queue (methods) public interface Queue extends Collection { E element(); //throws E peek(); //null boolean add(E
- 24. Queue In addition to the inherited core services offered by Collection, queue offers following methods in
- 25. Deque A linear collection that supports element insertion and removal at both ends When a Deque
- 26. Deque (methods) public interface Deque extends Queue { element() add(E e) (addLast(E e)) offer(E e) (offerLast(E
- 27. SortedSet SortedSet — a Set that maintains its elements in ascending order. Several additional operations are
- 28. SortedSet (methods) public interface SortedSet extends Set { // Range-view SortedSet subSet(E fromElement, E toElement); SortedSet
- 29. Map An object that maps keys to values. A map cannot contain duplicate keys; each key
- 30. Map (methods) public interface Map { // Basic operations V put(K key, V value); V get(Object
- 31. Map (methods) void clear(); // Collection Views public Set keySet(); public Collection values(); public Set >
- 32. Map.Entry public interface Entry { K getKey(); V getValue(); V setValue(V value); } Map map =
- 33. SortedMap SortedMap — a Map that maintains its mappings in ascending key order. This is the
- 34. SortedMap (methods) public interface SortedMap extends Map { SortedMap subMap(K fromKey, K toKey); SortedMap headMap(K toKey);
- 35. Implementations JDK provides implementations of each interface. All implementations permit null elements, keys and values All
- 36. HashSet, TreeSet, LinkedHashSet The two general purpose Set implementations are HashSet and TreeSet (and LinkedHashSet which
- 37. Set Implementation Comparisons
- 38. ArrayList and LinkedList The two general purpose List implementations are ArrayList and LinkedList . ArrayList offers
- 39. HashMap, TreeMap, LinkedHashMap The two general purpose Map implementations are HashMap and TreeMap. And LinkedHashMap (similar
- 40. The Legacy Collection Types Enumeration Analogous to Iterator. Vector Analogous to ArrayList, maintains an ordered list
- 41. Note the naming convention LinkedList also implements queue and there is a PriorityQueue implementation (implemented with
- 42. Implementations Each of the implementations offers the strengths and weaknesses of the underlying data structure. What
- 43. Ordering and Sorting There are two ways to define orders on objects. Each class can define
- 44. The Comparable Interface The Comparable interface consists of a single method: public interface Comparable { public
- 45. Comparator Comparator is another interface (in addition to Comparable) provided by the Java API which can
- 46. Comparator A Comparator is an object that encapsulates an ordering. Like the Comparable interface, the Comparator
- 47. Live Code ☺. Comparable class HDTV implements Comparable { private int size; private String brand; public
- 48. Live Code ☺. Comparable public class Main { public static void main(String[] args) { HDTV tv1
- 49. Live Code ☺. Comparator class HDTV { private int size; private String brand; public HDTV(int size,
- 50. Live Code ☺. Comparator class SizeComparator implements Comparator { @Override public int compare(HDTV tv1, HDTV tv2)
- 51. Live Code ☺. Comparator public class Main { public static void main(String[] args) { HDTV tv1
- 52. Other implementations in the API Wrapper implementations delegate all their real work to a specified collection
- 53. Synchronization wrappers The synchronization wrappers add automatic synchronization (thread-safety) to an arbitrary collection. There is one
- 54. Unmodifiable wrappers Unmodifiable wrappers take away the ability to modify the collection, by intercepting all of
- 55. Unmodifiable wrappers(cont.) There is one static factory method for each of the six core collection interfaces:
- 56. Singleton static Set Collections.singleton(T e) returns an immutable set containing only the element e This is
- 57. The Collections Toolbox The collections framework also provides polymorphic versions of algorithms you can run on
- 58. Concurrent Collections ConcurrentReaderHashMap An analog of java.util.Hashtable that allows retrievals during updates. ConcurrentHashMap An analog of
- 59. How to choose which Java collection class to use? Which Java List to use?
- 60. How to choose which Java collection class to use? Which Java Set to use?
- 61. How to choose which Java collection class to use? Which Java Map to use?
- 62. Open Source Collections Libraries in Java Apache Commons Collections Package Guava-libraries (Google Collections Library) Trove high
- 64. Скачать презентацию