Java programming thought note 11th Chapter holding object

Source: Internet
Author: User

1. Save Object references

1. Array, array with fixed size

2. Container class, can automatically adjust the size

2. Container class
    • List Set Queue Map and common implementation classes
2.1 Collectioon (interface)
    1. Saves a separate sequence of elements and saves the object objects when generics are not used. (Independence is not different)
2.1.1 List (interface)
    • Save by Insert Order
    1. ArrayList: First when the variable size array, random access fast, insert removal slow.
    2. LinkedList: Insert removal fast, access slow.
2.1.2 Set (interface)
    • Cannot have duplicate elements
    • 1.HashSet: Fastest get element, inner element no rule sort.
    • 1.1 Linkhashset: Saves the element in the order in which it is inserted, as well as the HashSet query speed.
    • 2. TreeSet: Saving objects in ascending order
2.1.3 Queue (interface)
    • One end of the container is inserted to delete
    • 1 Priorityqueue Priority queue
2.2 Map (interface)
    • The key cannot be duplicated when the key value pair object is saved. The key is also an object, and the value is also the object, press to find. (also known as a mapping table, associative array, dictionary---object looking through objects)
    • 2.1 HashMap: Find get fast, internal irregular sort
    • 2.1.1 Linkhashmap: Save key in insert order while saving HashMap query speed
    • 2.2 TreeMap: Keys to save objects in ascending order.
3. Pre-defined Generics

3.1 When generics are not pre-defined, the object is stored inside the container, so get () is also object objects that require a specific type of object to be forced to transform

3.2 Pre-defined generic Save feature object that prevents the wrong type from being placed in the container. For example, arraylist<apple> only save the Apple type or its subtype object.

3.3 Type parameters can have multiple but cannot be such arraylist<apple,orange>.

3.4 Upward transformation as appropriate for generics, you can put Apple's subclasses in ArrayList, with Get (index) removed or the original subtype.

3. Adding a set of elements
    • Some methods in the arrays class and the collections class can add a set of elements to collection.

3.1 Arrays.aslist () accepts an array or a comma-separated list of elements (the list element type can be different) and converts it to a list object to return. Use the explicit type description arrays.< type >aslist () to determine the type of list save. Note: The bottom of this list object is an array, so it cannot be resized and cannot be added.

3.2 Collections.addall (A, B) accepts an collection object A, an array, or a comma-separated list B, to add elements from B to a.

3.3 Collection.addall (a) accepts only another Collection object A, and this method runs faster.

The 3.4 collection constructor can receive a collection to initialize itself.

    • Add Value Map.put (key,value) to Map, value map.get (key)
4. Container printing
    • Directly printable, the ToString () method is already overwritten.
5. Print an array
    • The print array must use arrays.tostring (arrays) to print an array (an array is also an object that directly prints the object reference output just memory address)
Some of the methods in 6.List

6.1 A.contains (Reference) determines whether the object to which the reference is pointing is in the list.

6.2 A.romove (Reference/index) Removes the object that the reference points to/the index

6.3 A.indexof (Reference) Find the index number that the reference points to the object

6.4 A.sublist (2,5) creates a small list from the large list a starting at index 2, including 2, to 5, excluding 5. Returns a list

6.5 A.containsall (b) Determines whether the small list of B is in a large list, regardless of the order.

6.6 A.retainall (b) A, a, and a

6.7 A.removeall (b) Removes all elements of a in B.

6.8 Set (index, a) replaces the object at the index with a object.

6.9 A.isempty () A is empty

6.10 A.clean () empty A

6.11 A.toarray () converts collection to an array with no arguments as an object array, passing in a specific array object parameter, converting to the array (type cannot be wrong), and automatically creating an array of the appropriate size if the passed-in parameter array is too small.

7.Iterator (interface) iterator

A 7.1 iterator is an object that is used to traverse and select an object in a sequence

7.2 Iterator can only be moved one way

7.3 The method used to produce an iterator object: iterator (), which belongs to the Iterater interface. A container invokes the method to return an iterator object, and the object is ready to return the first element of the container. You can think of the iterator object as a cursor, before the first element of the container sequence is created when it is finished.

// SL is an iterator, which is the equivalent of a pointer

7.4 Sl.next () gets the next element, after executing next () The current SL points to this position unchanged

7.5 Sl.remove () deletes the element that the SL points to, only after next () is executed to point to an existing element to be used,

7.6 Sl.hasnext () checks if there are any elements in the sequence, and returns True, without returning false.

8.ListIterator
    • A more powerful iterator subtype that can be used only for list access, and is available for bidirectional movement.

8.1 Previous () gets the previous element, Hasprevious () determines that the previous element exists.

The 8.2 list call Listiterator () produces an iterator that precedes the first element and, if Listierator (n), begins to point to the nth element.

8.3 The Add () and set () methods are more than the iterators produced by iterator (), and add () will be inserted before the cursor, and set () will modify what the cursor refers to.

9.foreach and Iterators

9.1 foreach is used primarily for arrays, and can be used with all collection objects. This is because the collection interface inherits the Iterable interface.

The 9.2 iterable interface contains a iterator () method that produces iterator objects, which, as long as the Iterable interface is implemented, overwrite iterator () can be used for foreach. A large number of classes have implemented the Iterable interface and overwrite the iterator () method, such as all collection, but map is all.

When you execute foreach, the iterator () method is called automatically, which produces the iterator object that calls the next () Hasnext () Remove () method.

classTr<t>Implementsiterable { PublicIterator<t>iterator () {return NewIterator<t>() {@Override Public BooleanHasnext () {//TODO auto-generated Method Stub                return false; } @Override PublicT Next () {//TODO auto-generated Method Stub                return NULL; } @Override Public voidRemove () {//TODO auto-generated Method Stub                            }

9.3 Implementation of collection must be implemented iterator () because collection inherits Iterable.

10. Adapter
    • Resolves an issue where an interface requires another interface. For example, to be able to both forward and reverse foreach a list, if rewrite iterator () then lost the forward function, the solution is to add a method, the method can produce a Iterable object, overwriting the object's iterator () method to make it reverse function. Direct foreach the list object is forward output, and the method that the Foreach object adds reverses the output.
11.LinkedList
    • The method in LinkedList can realize the effect of stack queue or double end queue.
12.Stack (Class) stack
    • Last in, first out (LIFO) container.
    • The Java-provided stack class inherits from the Vector,vector, and the performance of push and pop is greatly reduced, and the stack is best implemented using a linked list.
    • Can be implemented using LinkedList.
Set
    • Set has exactly the same interface as collection, it has no additional functions, unlike list. It can be divided into two categories, in fact set is ollection, but the behavior is different. Set to maintain element independence, is based on the value of the object to determine the attribution

13.1 Set1.contains/contiansall (Set2), used to determine whether Set2 belong to Set1.

14.Map

14.1 Map.containskey (key), test whether the map contains the key, Map.containsvalue (value), and test whether the map contains a value.

14.2 Map can return the set of its key, the value of the collection, and the set of the key-value pair.

15.Queue (interface)
    • Queue FIFO, which is important in concurrent programming.

15.1 Offer () inserts an element into the tail of the queue or returns false.

15.2 peek () and element () all return to the team header without removing it. Peek () returns Null,element when the queue is empty () throws an exception

15.3 Poll () and remove () are removed to return to the head of the team. Poll () returns Null,remove () when the queue is empty throws an exception

15.4 Priorityqueue
    • Priority queue. FIFO describes the most typical queue rules, while the priority queue declares the next pop-up element that is most needed (highest priority).

15.4.1 when the offer () method is called, the object is sorted in the queue and can be modified using the comparator provided by itself.

15.4.2 gets the highest priority when calling Peek (), poll (), and the Remove () method.

16. Queue Rules
    • FIFO is a typical queue rule, which is a rule that determines the next popup element in the case of an element in a given set of queues. The priority queue is an atypical queue.
Knowledge points

1. Most basic and reliable container: ArrayList, add () Inserts an element, get (index) accesses the element by index, and size () returns the number of elements within the container.

2. All Collection can use the foreach

3. Add multiple new A () objects to collection, which are not the same object, exist in the set, and have different indexes.

Java programming thought note 11th Chapter holding object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.