Category notes summary (to be continued)

xiaoxiao2021-03-06  53

Class set interface:

Collection enables you to operate the object group, the top-level list extension of the class set hierarchy (list of objects) SET extension Collection to process set, set must contain unique elements SortedSet extension SET degeneration sort collection

Method Summary: add () method to add objects to the class set. Note add () with an Object type parameter. Because Object is a superclass of all classes, any type of object can be stored in a classset. However, the original type may not work. For example, a class set cannot directly store values ​​of type int, char, double, and the like. Of course, if you want to store these objects, you can also use one of the original type wrappers introduced in Chapter 14. The addall () method adds the entire contents of a class set to another class. The remove () method deletes an object. In order to delete a set of objects, the removeAll () method can be called. Calling a retainAll () method can delete all elements except a set of specified elements. To empty the class set, you can call the clear () method. By calling the contacts () method, you can determine if a class set contains a specified object. In order to determine if a class set contains all the elements of another class set, the contAinsAll () method can be called. When a class set is empty, you can confirm by calling the iSempty () method. Call the SIZE () method to get the number of current elements in class. The ToArray () method returns an array that contains elements stored in the call class. This method is more important than what it looks better than it. It is often advantageous to use the class number syntax to handle the content of the class set. By providing a path between the class set and arrays, the advantages of both can be taken out. Calling the equals () method can compare if the two class sets are equal. The accurate meaning of "equal" can be different from class sets to class sets. For example, an equals () method can be executed to compare the value of the element stored in the class set, in other words, the equals () method can compare references to the elements. A more important way is Iterator (), which returns an iteration program for class sets. As you will see, when using a class set framework, iterative procedures are critical to successful programming. List interface: List interface extends the Collection and declares the characteristics of the stored set of elements. Using a zero-based subscript, the element can be inserted and accessed by their position in the list. List method Abstract: For add () and addall () methods defined by Collection, List adds Method Add (int, object), and addall (int, collection). These methods are inserted into the elements at the specified subscript. The semantics defined by Collection and Addall (Collection) are also changed by List so that they add elements at the end of the list. In order to obtain an object stored in the specified location, the GET () method can be called with the subscript of the object. In order to assign a value to one of the elements in the class, the SET () method can be called to specify the subscript of the changed object. Call indexof () or lastIndexOf () can get the subscript of an object. By calling the sublist () method, you can get a list of subscripts to start the subscript and the end subscript. As you can imagine, the sublist () method makes the column processing very convenient. SortedSet interface: The sortedset interface extends the set (SET extension collection) and illustrates the characteristics of the collections arranged in ascending order. In addition to those methods defined by the SET, the methods illustrated by the sortedset interface are listed in Table 15-3. When no items are included in the call collection, several methods are triggered in the NosuChelementexception. When the object is incompatible with the elements in the call collection, ClassCastException is triggered. If you try to use the Null object, the collections do not allow NULL to trigger NullPointerException exception.

Standard class Collection Collection classes summarized as follows: Most AbstractCollection implement AbstractList Collection interface and implements most extended AbstractCollection AbstractSequentialList List interface to be used and extended AbstractList clusters, a continuous set of class rather than random access manner by which expansion elements LinkedList AbstractSequentialList To implement link table ArrayList to implement dynamic array AbstractList to implement Dynamic array AbstractSet extension AbstractCollection and implement Most SET Interface HashSet intended to extend the AbstractSetTreeSet implementation in the tree in order to use the hash table. Extending AbstractSet Note: In addition to the Collection class, there are several classes left from previous versions, such as Vector, Stack, and HashTable are redesigned into the form of support class sets. ArrayList class: ArrayList class extends AbstractList and performs a list interface. ArrayList supports a dynamic array that can grow as needed. In Java, the standard array is fixed. After the array is created, they cannot be extended or shortened, which means that you have to know how much elements can be accommodated in advance. However, you can know how much arrays need until running. In order to solve this problem, the class set framework defines ArrayList. Essentially, ArrayList is an object-referenced array. That is, ArrayList can dynamically increase or decrease their size. An array list is created with an original size. When it exceeds its size, the class set is automatically increased. When the object is deleted, the array can be reduced. Note: The dynamic array is also supported by the class VECTOR left by the previous version. ArrayList has the following constructor: ArrayList () where the first constructor creates an empty array list. ArrayList (Collection C) The second constructor establishes an array list that is initialized by the elements in the class set C. ArrayList (int Capacity) The third constructor establishes an array list that has the specified initial capacity (Capacity). Capacity is the size of the basic array of storage elements. When the element is added to the array list, the capacity will automatically increase the HashSet class Hashset extension AbstractSet and implements the SET interface. It creates a class set that stores with a hash table. As most readers are likely to know, the hash table stores information by using a mechanism called hash method. In hashing, the information content of a keyword is used to determine a unique value called a Hashcode. The hash code is used as a storage subscript of data connected to the keyword. The conversion of the keyword to its hash code is automatically executed - you can't see the hash code itself. Your program code cannot directly index a list.

The advantage of the Hash Law is that even for large collection, it allows some basic operations such as add (), contains (), REMOVE (), and SIZE () methods to remain unchanged. Hashset does not define any other methods that exceed its superclass and interface. Importantly, paying attention to the aquaries and does not ensure the order of their elements, because the process of the hash method usually does not allow yourself to participate in the creation of the collections. If you need sorting storage, another class set -Treset will be a better choice. The TREESET class Treeset provides a tool for the SET interface that uses the tree to store, and the object is stored in an ascending order. Access and retrieval is very fast. The Treeset is a good choice if the sort information of the quick retrieval is stored. Access class sets through an iterative function typically want to loop through the elements in class. For example, you may want to display each element. So far, the easiest way to handle this problem is to use Iterator, Iterator is an object that implements Iterator or implementing a listiterator interface. Iterator can complete the loop through class set to obtain or delete an element. Listiterator extends Iterator, allows two-way traversal list, and can modify units. The method description of the Iterator interface is summarized in Table 15-4. The method description of the Listiterator interface is summarized in Table 15-5. Methods defined by Iterator: Boolean Hasnext () Returns true if there is more elements, otherwise returns falseObject next () Returns the next element. If there is no next element, the NosuChelementException exception void remove () deletes the current element. If you try to call the Remove () method after calling the next () method, trigger the IllegalStateException exception Use iterative functions before accessing the class. You must get an iterative function. Each Collection class provides an item () function that returns an iterative function for the class header. By using this iterative function object, you can access each element in the classset, one element. Typically, use the iterative function loop through the content of the class set, the steps are as follows: 1. Get an iterative function of the class head head by calling the type of ITATOR () method. 2. Create a loop that calls the HASNext () method, as long as HasNext () returns TRUE, loop iteration. 3. Inside the circulation, each element is obtained by calling the next () method.

Routine:. // Demonstrate iterators.import java.util *; class IteratorDemo {public static void main (String args []) {// create an array listArrayList al = new ArrayList (); // add elements to the array listal .add ("c"); al.add ("a"); Al.Add ("e"); al.add ("b"); al.add ("d"); al.add ("F "); // use itrator to display contents of alSystem.out.print (" Original Contents of Al: "); item ITR = Al.ITerator (); while (itr.hasnext ()) {Object Element = iTr.next (); System.out.print (Element ");} Processing Map is as mentioned in this chapter, in addition to class set, Java 2 also adds mapping in java.util. Mapping (MAP) is an object that stores keywords and values ​​or objects for keywords / values. Given a keyword, you can get its value. Keywords and values ​​are objects. The keyword must be unique. But the value can be copied. Some mappings can receive NULL keywords and NULL values. And some can't. The mapping interface map defines the characteristics and essence of the mapping. The following interface supports mapping: MAP mapping unique keywords to values ​​Map.Entry describe elements in the map (keyword / value pair). This is an internal class SortedMap extension Map of MAP to keep the keyword to keep all keywords / values ​​from the MAP definition by MAP. The key of the keyword is returned; otherwise returns falseBoolean ContainSValue (Object V) If the map contains V values, returns true; otherwise, returns false uTrySet () returns a collection (SET) containing items in the map. This collection contains the object of type Map.Entry.

This method provides a collection "View" Boolean Equals (Object Obj) for call mapping, if the OBJ is a map and contains the same input, returns falseObject Get (Object K) Returns the value associated with the keyword k INT hashcode () Returns the hash code for call mapping () If the call mapping is empty, return true; otherwise returns falseset keyset () Returns a collection of keywords in the call mapping (SET). This method provides a collection "View" Object Put (Object K, Object V) to call the mapping keyword to overwrite the value associated with the keyword. Keywords and values ​​are k and v. If the keyword is no longer existed, return null; otherwise, return the value Void Putall (MAP M) that is originally associated with the keyword to send all the input to the input to call Object Remove (Object K) delete keywords equals K Entering int size () Returns the number of keywords / values ​​in the mapping. Collection Values ​​() returns a class set that contains the values ​​in the map. This method provides a class set "View" mapping cycle using the values ​​in the mapping: Get () and PUT (). Using the PUT () method can add a value specified for the keyword and value to the mapping. In order to obtain a value, the GET () method can be invoked by using the keyword as a parameter. Call returns this value. As mentioned earlier, the mapping is not a class set, but it can get the map "view". To achieve this function, an entryset () method can be used, which returns a collection of elements in the mapping (SET). In order to get the classset "view" of the keyword, you can use the keyset () method. To get the value of the value of the value, you can use the value of the value. The classset "View" is a means to integrate the mapping into the class set framework. The SortedMap interface SortedMap interface extends Map, which ensures that each item is sorted as sequential in keyword. Method defined by sortedMap: Comparator Comparator () returns a comparison function that calls the sorted mapping.

转载请注明原文地址:https://www.9cbs.com/read-91255.html

New Post(0)