L 16 Chapter 22 Java Collections Framework 2
- Slides: 21
L 16 (Chapter 22) Java Collections Framework 2 1
The Vector and Stack Classes The Java Collections Framework was introduced with Java 2. Several data structures were supported prior to Java 2. Among them are the Vector class and the Stack class. These classes were redesigned to fit into the Java Collections Framework, but their old-style methods are retained for compatibility. This section introduces the Vector class and the Stack class. 2
The Vector Class In Java 2, Vector is the same as Array. List, except that Vector contains the synchronized methods for accessing and modifying the vector. None of the new collection data structures introduced so far are synchronized. If synchronization is required, you can use the synchronized versions of the collection classes. These classes are introduced later in the section, “The Collections Class. ” 3
The Vector Class, cont. 4
The Stack Class The Stack class represents a last-in-firstout stack of objects. The elements are accessed only from the top of the stack. You can retrieve, insert, or remove an element from the top of the stack. 5
Example: Using the Vector Class Listing 4. 1, Prime. Number. java, determines whether a number n is prime by checking whether 2, 3, 4, 5, 6, . . . , n/2 is a divisor. If a divisor is found, n is not prime. A more efficient approach to determine whether n is prime is to check if any of the prime numbers less than or equal to can divide n evenly. If not, n is prime. Write a program that finds all the prime numbers less than 250. 6
Example: Using the Vector Class, cont. The program stores the prime numbers in a vector. Initially, the vector is empty. For n = 2, 3, 4, 5, . . . , 250, the program determines whether n is prime by checking if any prime number less than or equal to in the vector is a divisor for n. If not, n is prime and add n to the vector. The program that uses a vector is given below. Find. Prime. Using. Vector Run 7
Example: Using the Stack Class Write a program that reads a positive integer and displays all its distinct prime factors in decreasing order. For example, if the input integer is 6, its distinct prime factors displayed are 3, 2; if the input integer is 12, the distinct prime factors are also 3 and 2. 8
Example: Using the Stack Class, cont. The program uses a stack to store all the distinct prime factors. Initially, the stack is empty. To find all the distinct prime factors for an integer n, use the following algorithm: Find. Prime. Factor. Using. Stack Run 9
The Map Interface The Map interface maps keys to the elements. The keys are like indexes. In List, the indexes are integer. In Map, the keys can be any objects. 10
The Map Interface UML Diagram 11
Hash. Map and Tree. Map The Hash. Map and Tree. Map classes are two concrete implementations of the Map interface. The Hash. Map class is efficient for locating a value, inserting a mapping, and deleting a mapping. The Tree. Map class, implementing Sorted. Map, is efficient for traversing the keys in a sorted order. 12
Linked. Hash. Map was introduced in JDK 1. 4. It extends Hash. Map with a linked list implementation that supports an ordering of the entries in the map. The entries in a Hash. Map are not ordered, but the entries in a Linked. Hash. Map can be retrieved in the order in which they were inserted into the map (known as the insertion order), or the order in which they were last accessed, from least recently accessed to most recently (access order). The noarg constructor constructs a Linked. Hash. Map with the insertion order. To construct a Linked. Hash. Map with the access order, use the Linked. Hash. Map(initial. Capacity, load. Factor, true). 13
Example: Using Hash. Map and Tree. Map This example creates a hash map that maps borrowers to mortgages. The program first creates a hash map with the borrower’s name as its key and mortgage as its value. The program then creates a tree map from the hash map, and displays the mappings in ascending order of the keys. Test. Map Run 14
Example: Counting the Occurrences of Words in a Text This program counts the occurrences of words in a text and displays the words and their occurrences in ascending order of the words. The program uses a hash map to store a pair consisting of a word and its count. For each word, check whether it is already a key in the map. If not, add the key and value 1 to the map. Otherwise, increase the value for the word (key) by 1 in the map. To sort the map, convert it to a tree map. Count. Occurrence. Of. Words Run 15
The Collections Class The Collections class contains various static methods for operating on collections and maps, for creating synchronized collection classes, and for creating readonly collection classes. 16
The Collections Class UML Diagram 17
Example: Using the Collections Class This example demonstrates using the methods in the Collections class. The example creates a list, sorts it, and searches for an element. The example wraps the list into a synchronized and read-only list. Test. Collections Run 18
The Arrays Class The Arrays class contains various static methods for sorting and searching arrays, for comparing arrays, and for filling array elements. It also contains a method for converting an array to a list. 19
The Arrays Class UML Diagram 20
Example: Using the Arrays Class This example demonstrates using the methods in the Arrays class. The example creates an array of int values, fills part of the array with 50, sorts it, searches for an element, and compares the array with another array. Test. Arrays Run 21
- Java collections framework diagram
- Minimax algorithm java
- Collections overview in java
- Collection hierarchy java
- Dot java
- Google collections java
- Chapter 20 patient collections and financial management
- System collections generic
- Pseg credit and collections
- Collections trust spectrum
- Cert 3 in pathology collection
- Huge collections of stars
- Static collection
- Object model and collections in dhtml
- Ctech collects
- Collection management sap
- Financial data systems debt collector
- Fungsi movie task pane
- Abstract data type (adt)
- Collection of specialized cells and cell products
- System.collections.generic namespace
- Musnad collections of hadith