Collection framework from getting started (1)

xiaoxiao2021-03-06  57

The collection and collection frame collection is a common name for an object. This object represents a set of objects together in some way; it is a combination of multiple elements into a unit, for storage, retrieval, manipulating, and transfer data. When we talk about the collection of objects, we refer to the collection of object references rather than the collection of objects. Only store references in the Java collection, the object is outside the collection. The collection framework provides interfaces and classes for managing object collections, including interface, implementation, and algorithms. Three important collection types in Java: Sets set, sequence sequence and mapping map. Set set does not press any specific way: Concentration Each object is unique; delete objects by reference to the concentrated object; set variant (Set can be sorted) Requirements to define a class of storage objects to implement applications for object comparisons.

Sequence Sequence is called list list, and the object is stored in linear mode, there is no specific order, only one start and one end (ordinary array is actually a list). Since the list is linear, you can only add a new object at the beginning or end of the list, or insert the object behind the object specified in the sequence. Retrieving (deleting) objects in the list is very flexible: you can select the first or last object, you can get the object of the specified location (like the index array), or all of the previous or back sequential search sequences. Objects, identify the same object as given object, and use iterative mode to access each of the sequences in the sequence forward or backward order (sequence can save different copies of the same object). The list appears in the data structure as: arrays and vector, linked lists, stacks, queues. The stack is also a list of storage mechanisms, is also a list; the queue uses advanced first storage mechanism, which is also seen as a list; the list can work like a stack, which is very understanding because The method of adding and deleting an object is the same as the stack; also, insert the object only in the linked list, the linked list can be worked like a queue.

Map map

Keywords (key) determines the storage location of the object in the map; it can be any type of object; it must be unique in one map; and it determines the location of the keyword / object stores in the mapping, essentially Handling keywords by hashing, generating an integer value called hashcode, is actually a bias having a starting position relative to the mapping memory area. The HashCode () method defined in Object is an object to generate an INT scatter code.

The iterator is in the Java language, an iterator is an interface that can be implemented by a collection class. Any collection object can create an object type object, which encapsulates all objects in the original collection (package pair) The original collection reference is referenced), and the object can be accessed using the Iterator interface definition. In other words, the iterator provides a simple method that gets all objects in the collection one time.

(1) Boolean Hasnext (): Judging whether there is another accessible element Object next (): Returns the next element to be accessed. If the end of the collection is reached, the NosuchelementException is thrown. (2) Void remove (): Delete the object returned last time. This method must be followed after access to an element. If the collection has been modified last time, the method will throw an IllegalStateException.

Iterator it = mycollection.iterator (); myclass item;

While (item.hsanext ()) {item = (myclass) .next ();

// Do Something with item

} "Deleting operations in iTerator also have an impact on the underlying Collection." Iterator is fault-fast. This means that when another thread modifies the underlying collection, if you are using the Iterator traversal collection, Iterator will throw ConcurrentModificationException (another RuntimeException exception) exception and immediately failed. "Java language also provides a mechanism called counter (Enumerator), the functionality provided by ENUMerator is basically the same as Iterator, but it is recommended to use the item to use Eumerator when processing a collection."

List iterator Listiterator

The ListIterator interface illustrates some methods that can override the collection object forward or backward. Listiterator does not have a current location, between the cursor is between the values ​​returned by the previous and next method. A list of a length N, N 1 effective index value:

(1) Void Add (Object O): Adds Object O to the front Void Set (Object O) of the current location: instead of the previous or Previous method accessed by the object O. If the list structure is modified last time, ILLEGALSTATEEXCEPTION will throw an unlegation. (2) Boolean Hasprevious (): During the rear iteration, there is an element to access object previous (): Return to the previous object int nextIndex (): Returns the index of the elements returned next time, if the ListIterator object is located The tail of the list is returned to the number of elements in the list. INT previousIndex (): Returns the index of the elements returned next time, if the listiterator is located at the header of the list, returns -1. "Under normal circumstances, do not use Listiterator to change the direction of the collection element - forward or back. Although it can be implemented in technology, next () immediately calls next (), returning the same element. Put the call next to NEXT The order in which previous () is reversed, and the result is the same. "" We also need to explain the add () operation slightly. Adding an element will cause the new element to be added to the front of the implicit cursor. Therefore, add an element Call previous () will return new elements, and call next () does not work, return to the next element before adding operations. "

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

New Post(0)