Chapter14 Java Collection Architecture Collection interface and Abstract Collection class Rule set Set Comparator interface Linear Table List Map Java高级程序设计(上) 1 brief introduction Several common data structures: Link list, stack, queue, heap, priority queue, etc. Collection: Container objects that store a set of objects. The Java Collection Architecture supports three types of collections: Rule Set, Linear Table, Map Set: Stores a set of distinct elements. List: Stores a sequence of elements. Map: Stores a set of objects with an associated key. Note: Classes in the Java collection architecture implement Cloneable and Serilizable interfaces. The classes learned in this chapter are in the java.util package. Emphasis: How to use interfaces and classes in the collection architecture. Java高级程序设计(上) 2 Diagram of main interfaces and classes SortedSet TreeSet Set Collection AbstractSet HashSet LinkedHashSet Vector Stack AbstractCollection List AbstractList ArrayList AbstractSequentialList Interfaces LinkedList Concrete Classes Abstract Classes Java高级程序设计(上) 3 Diagram of main interfaces and classes SortedMap Map TreeMap AbstractMap Interfaces Abstract Classes Java高级程序设计(上) HashMap LinkedHashMap Concrete Classes 4 Collection interface and AbstrctCollection interface Collection +add(o: Object): boolean +addAll(c: Collection): boolean +clear(): void +contains(o: Object): boolean +containsAll(c: Collection):boolean +equals(o: Object): boolean +iterator(): Iterator +remove(o: Object): boolean +removeAll(c: Collection): boolean +retainAll(c: Collection): boolean +size(): int 如果集合包含元素 o,返回 true 如果该集合包含 c 中的所有元素,返回 true 如果该集合与另一个对象 o 相等,返回 true 返回该集合中元素的迭代器 从集合中删除元素 o 从该集合删除所有 c 中的元素 保留即在 c 中又在该集合中的元素 返回该集合中的元素数目 +toArray(): Object[] 返回包含该集合中元素的 object 数组 Iterator +remove(): void 删除集合中的所有元素 如果该集合不包含元素,返回 true +isEmpty(): boolean +next(): Object 将集合 C 中的所有元素添加到此集合中 返回集合的散列码 +hashcode(): int +hasNext(): boolean 向集合中添加新的元素 O 如果迭代器还有要遍历的元素,返回 true 从迭代器返回下一个元素 删除使用 next 方法获得的最后一个元素 Removes all the elements from this collection Java高级程序设计(上) 5 Hash sets (HashSet classes) Three: Hash Set class, Chained hash set (LinkedHashSet class) TreeSet class. Hash sets (HashSet classes): Objects added to the hash set must implement the hashCode () method. Example: Create a hash set to store parsed words in text and iterate through each word in t he set using an iterator. Note: 1. Adding multiple identical elements to the rule set, only one is stored. 2. The elements in a collection are not stored in the order in which they were inse rted (the disorder of the collection). 3. Elements added to a collection must be of the same type as those declared by t he collection. 4. For (Object element: set) statements that traverse each element in a collection. Java高级程序设计(上) 6 Linked HashSet and TreeSet LinkedHashSet: With linked list implementation, access can be made in the order in wh ich elements are inserted. Example: A chain hash set is constructed with strings extracted from text files an d elements are accessed with a foreach statement. Program: ex16_2. Java Tip: If you don't need to store elements in the order in which they are i nserted, you should use HashSet, which is more efficient than Linked HashSet. TreeSet: You can sort the elements that are added to it (ascending or descendin g) Several methods: First (), last (), headSet (toElement), tailSet (fromElement) Note: Elements added to the tree set must be comparable. Java高级程序设计(上) 7 Linked HashSet and TreeSet Method of achieving object comparison: Method 1: Implement Comparable interface. Example: Use the TreeSet class to display words alphabetically. Program: ex16_3. Java Using TreeSet (Collection c) can improve efficiency, sorting only once. Note: When updating a collection, you need to keep it in order, use a tree set, otherwise u se a hash set. Method 2: Implement Comparator interface. Different types of elements that implement the Comparator interface can be added to the tree set. Two methods are implemented: Public intcompare (Object element1, Object element2) Public Boolean equals (Object elements) Example: Area comparison of geometric figures.Different geometric figures are added to t he tree set.Then output the added elements. Program: ex16_4. Java Note: Comparable is used to compare two objects of the same type. Comparator can com pare two objects of different types. Java高级程序设计(上) 8 Linear table Using a linear tab

pptDoc Java Collection Architecture

Professional Docs > Common > Other > Preview
18 Pages 0 Downloads 266 Views 3.0 Score
Tips: Current document can only be previewed at most page8,If the total number of pages in the document exceeds page 8,please download the document。
Uploaded by admin on 2019-10-16 00:28:33
You can enter 255 characters
What is my domain?( )
  • No comments yet